Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Created June 18, 2022 02:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaikech/e439cbaadf363380fe026b380dd4de42 to your computer and use it in GitHub Desktop.
Save tanaikech/e439cbaadf363380fe026b380dd4de42 to your computer and use it in GitHub Desktop.
Shortening a Long URL using Firebase Dynamic Links API with Google Apps Script

Shortening a Long URL using Firebase Dynamic Links API with Google Apps Script

This is a sample script for shortening a long URL using Firebase Dynamic Links API with Google Apps Script.

IMPORTANT

Before you use this script, please create a new Firebase project and link it to your Google Cloud Platform Project. Ref And, please enable Firebase Dynamic Links API at the API console. And then, please create your API key from your Google Cloud Platform Project.

Sample script

const apiKey = "###"; // Please set your API key.
const longUrl = "###"; // Please set the long URL you want to shorten.
const yourDynamicLinkDomain = "###"; // Please set your dynamic link domain.

const url =
  "https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=" + apiKey;
const options = {
  payload: JSON.stringify({
    dynamicLinkInfo: {
      dynamicLinkDomain: yourDynamicLinkDomain,
      link: longUrl,
    },
  }),
  contentType: "application/json",
};
const res = UrlFetchApp.fetch(url, options);
const { shortLink } = JSON.parse(res.getContentText());

console.log(shortLink);
  • When this script is run, longUrl is shortened.

Note

If the warning of Setup URL patterns to whitelist in the Firebase Dynamic Links console. is shown, please include the URL to the whitelist to "Allowlist URL pattern". Ref By this, the warning can be removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment