Bulk Shorten URLs with your Bitly API (v4) and Google App Script using google sheets (working in 2022)

This is a working code for Bitly using v4 API to shorten a long URL. You can also create a copy of this sheet and bulk create bitly links using the function bitlyShortenUrl in the 3rd column- https://docs.google.com/spreadsheets/d/1TS-aQcDu3rRv-LexEoIqDiX0ydv69ijkXQP-nuQdBz4/edit#gid=0

function bitlyShortenUrl(longLink, accessToken) {
  var form = { 
    "domain": "bit.ly",
    "long_url": longLink
  };
  var url = "https://api-ssl.bitly.com/v4/shorten";

  var options = {
      "headers": { Authorization: `Bearer ${accessToken}` },
      "method": "POST",
      "contentType" : "application/json",
      "payload": JSON.stringify(form)
  };
  var response = UrlFetchApp.fetch(url, options);
  var responseParsed = JSON.parse(response.getContentText());
  return(responseParsed["link"]);
}

PS: Bitly access token details given here https://support.bitly.com/hc/en-us/articles/230647907-How-do-I-generate-an-OAuth-access-token-for-the-Bitly-API-

Leave a Reply

Your email address will not be published.