Hi everyone,
This isn’t very complicated, but I thought I share the code for an action button which takes a URL field and shortens it through bit.ly’s API:
// this action button uses the bit.ly API to shorten a URL
// Fibery API and HTTP services
const fibery = context.getService('fibery');
const http = context.getService('http');
for (const entity of args.currentEntities) {
// get the URL field
const linkURL = entity['{{YOUR-URL-FIELD-NAME}}'];
// check to see if the field is empty
if (linkURL) {
// check to see if the link has already been shortened
if (linkURL.search("https://bit.ly/") < 0) {
// call bitly API to shorten the URL
const strBitly = await http.postAsync('https://api-ssl.bitly.com/v4/shorten', {
body: {
"domain": "bit.ly",
"long_url": linkURL
},
headers: {
'Content-type': 'application/json',
'Authorization': 'Bearer {{YOUR_BITLY_API_KEY_HERE}}'
}
});
// parse the response string
const objBitly = JSON.parse(strBitly)
// extract the shortend URL
const shortURL = objBitly["link"];
// update the entity link field with the shortened URL
await fibery.updateEntity(entity.type, entity.id, {
'{{YOUR-URL-FIELD-NAME}}': shortURL
});
}
}
}
You will need to replace the following in the code:
{{YOUR-URL-FIELD-NAME}} : replace with field name for the URL field
{{YOUR_BITLY_API_KEY_HERE}}: replace with your bit.ly API key
Hope this is helpful to someone else. If anyone has any improvements please let me know