I have a Salesforce Marketing Cloud issue with respect to shortening a URL with AMPscript using the Bitly API.

Context: I want to direct users to a very long URL (i.e. including several URL parameters after "?") that is dynamically added in the SMS send based on a Data Extension attribute. I want to shorten the URL using Bit.ly, however, I have trouble getting the desired outcome as the shortened URL ends up redirecting to the wrong destination URL.

The outcome should be a shortened bit.ly link, redirecting to a destination URL such as https://www.website.com?user_id=XX&parameter1=XX&parameterN=YY&utm_source=XX&utm_medium=YY&utm_campaign=XY

However, the below code does not give the desired outcome:

SET @URL = TRIM(HTTPGet(Concat("https://api-ssl.bitly.com/v3/shorten?access_token=[here goes my access token]&format=txt&longUrl=",applicationFormUrl)))


--> where applicationFormUrl pulls dynamic data from a data extension including the destination URL.

Hope you will be able to solve the problem. I wonder if the issue is related to the encoding of the URL string. Tell me if you need more info.

  • What is the difference between the link being generated and your desired redirect location? If you want to see the final URL that a bitlylink has shortened, simply add the "+" character to the end of your bitly link and it will output the final URL. That should aid in testing to confirm where the disconnect is happening. Also, Bitly v3 is no longer supported as of Sept. 2020 so it might be wise to migrate to v4 to ensure the best results. May 7, 2021 at 14:04

1 Answer 1


Regarding your code - applicationFormUrl does not have @, not sure if that is a typo or mistake in your code that prevents it from working as intended. Also, you need to URLEndcode your link + parameters you want to shorten.

In any case, Bit.ly API v3 is no more available and you have to use v4 from now on. Below you can find a Code Snippet that you can adjust based on your needs and add needed parameters.

<script language="javascript" runat="server">
Platform.Load("Core", "1");
    VAR @Link, @utm_campaign, @utm_source, @utm_medium, @utm_id, @sfmc_id, @Parameters, @FullURL, @AccessToken, @Payload, @PostRequest, @Response, @ResponseRows, @json, @ShortenURL

    /* Set the link and parameters you want to track */
    SET @Link = ""

    SET @utm_campaign = ""
    SET @utm_source   = ""
    SET @utm_medium   = ""
    SET @utm_id       = ""
    SET @sfmc_id      = ""

    /* Prepare your full link */
        IF EMPTY(@utm_campaign) AND EMPTY(@utm_source) AND EMPTY(@utm_medium) AND EMPTY(@utm_id) AND EMPTY(@sfmc_id) THEN
            SET @FullURL = URLEncode(CONCAT('"', @Link, '"'))
            SET @Parameters = CONCAT("?utm_campaign=", @utm_campaign, "&utm_source=", @utm_source, "&utm_medium=", @utm_medium, "@utm_id=", @utm_id, "&sfmc_id=", @sfmc_id)
            SET @FullURL = URLEncode(CONCAT('"', @Link, @Parameters, '"'))

    /* Change 'xxx' to your personal access token */
    SET @AccessToken = 'xxx'

    /* Change 'xxx' to the Group ID this link would be associated with and 'yyy' to either default 'bit.ly' domain or your custom one */
    SET @Payload     = CONCAT('{"group_guid": "xxx", "domain": "yyy", "long_url": ', @FullURL, '}') 
    SET @PostRequest = HTTPPost2("https://api-ssl.bitly.com/v4/shorten", "application/json", @Payload, False, @Response, @ResponseRows, "Authorization", CONCAT("Bearer ", @AccessToken))

    IF @PostRequest == '200' THEN
        SET @json = @Response

<script language="javascript" runat="server">
    /* Receieving json from POST request and parsing it to get the link */
    var json       = Variable.GetValue("@json");
    var ParseJSON  = Platform.Function.ParseJSON(json);
    var ShortenURL = ParseJSON.link;
    Variable.SetValue("@ShortenURL", ShortenURL);


However, to make it work, you would need to get in advance several things:

Link address you want to shorten Access token that you need to generate in your Bit.ly admin panel Ask Bit.ly to create several Groups if you would want to differentiate your links by brand or region, for example (optional) Retrieve guid (= Group ID) by doing an API call (optional) Only once those values are set, you would be able to output the result like - %%=v(@ShortenURL)=%% Other points to consider:

Check official API documentation, where you can find other things like customizing your links Set more variables to match the custom field in SMS SendLog for better tracking Set better error handling, since SSJS is already in place Add some UTM parameters and concatenate them to the link to improve personalization and tracking Bring the whole batch of code to the SMS by saving the code in the Code Snippet in the Content Builder and then using %%=ContentBlockByID(...)=%% in the SMS text

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .