How Can I Create TinyURL Links using Google Sheets?

How Can I Create TinyURL Links using Google Sheets?

You can integrate the TinyURL API with Google Sheets using Apps Script and an API token. Follow these instructions to set it up:

  1. Register and set up your account: Create a TinyURL account if you don’t have one. Then, generate an API token with “Create TinyURL” permissions and store it in a secure location.
  1. Open your Google Sheet: Navigate to the Google Sheet where you want to generate TinyURLs from destination URLs.
  1. Access Apps Script: Go to Extensions > Apps Script. A new window will open.
  1. Add the script: Copy the provided script into the editor window. On line 3, replace yourTokenValue with the API token from step 1 (e.g., var token = "abc123yourActualToken";).
  1. Prepare your data: Enter a destination URL in a cell, such as J6.
  1. Generate TinyURLs: In another cell (e.g., K6), enter the formula =shortenUrl(J6) to automatically create a TinyURL. Drag the formula down to apply it to additional URLs.
function shortenUrl(longUrl) {
  var apiUrl = "https://api.tinyurl.com/create";
  var token = "yourTokenValue"; // replace with your real token

  var payload = {
    "url": longUrl
  };

  var options = {
    method: "post",
    headers: {
      "Authorization": "Bearer " + token,
      "Accept": "application/json",
      "Content-Type": "application/json"
    },
    payload: JSON.stringify(payload),
    muteHttpExceptions: true
  };

  var response = UrlFetchApp.fetch(apiUrl, options);
  var json = JSON.parse(response.getContentText());

  // TinyURL API returns { data: { tiny_url: "..." } }
  if (json.data && json.data.tiny_url) {
    return json.data.tiny_url;
  } else {
    return "Error: " + response.getContentText();
  }
}

Explore More Articles

Explore More Topics

Explore More Topics