(+91)7275894867 sales@outrightcrm.com
OutRightCRM Login
📅 Published on August 16, 2024 ✏️ Updated on April 22, 2025

Bulk URL Shortener In Google sheet using Bitly API & Apps Script

Author
Editorial Team

Bulk URL Shortener In Google sheet using Bitly API & Apps Script

Create a bulk URL shortener in Google Sheets using the Bitly API and Google Apps Script. Add long URLs in a column, then use a script to send each to Bitly’s API, retrieve the shortened version, and display it in another column—perfect for managing multiple links efficiently.
  1. Step 1: Click on the Copy button to copy the code snippet.
  2. Step 2: Paste the copied code into your project’s script editor.

Apps Scripts Blog

Read Blog

Apps Scripts Code

function shortenUrls() {
  var sheet = SpreadsheetApp.getActive().getSheetByName("Sheet1"); // Replace "Sheet1" with the name of your sheet
  var dataRange = sheet.getDataRange();
  var data = dataRange.getValues();
  var urlColumn = 1; // Replace "1" with the column number of the URLs you want to shorten
  
  for (var i = 0; i < data.length; i++) {
    var url = data[i][urlColumn - 1];
    var apiUrl = "https://api-ssl.bitly.com/v4/shorten";
    var headers = {
      "Authorization": "Your API Key",
      "Content-Type": "application/json"
    };
    var payload = {
      "long_url": url
    };
    var options = {
      "method": "post",
      "headers": headers,
      "payload": JSON.stringify(payload)
    };
    var response = UrlFetchApp.fetch(apiUrl, options);
    var json = JSON.parse(response.getContentText());
    var shortenedUrl = json.link;
    sheet.getRange(i + 1, urlColumn + 1).setValue(shortenedUrl); // Replace "+ 1" with the column number where you want to put the shortened URLs
  }
}