# Bit.ly action button

**URL:** <https://community.fibery.io/t/bit-ly-action-button/1430>\
**Category:** API & Programming\
**Created:** [March 6, 2021, 11:03pm UTC](https://community.fibery.io/t/bit-ly-action-button/1430 "2021-03-06T23:03:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![cannibalflea](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/cannibalflea/32/318_2.png) [@cannibalflea](https://community.fibery.io/u/cannibalflea)\
**Post date:** [March 6, 2021, 11:03pm UTC](https://community.fibery.io/t/bit-ly-action-button/1430/1 "2021-03-06T23:03:40Z")

</div>

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 🙂
