Home > database >  External API rate limits are hit by Google Sheets UrlFetchApp
External API rate limits are hit by Google Sheets UrlFetchApp

Time:11-29

I have a Google Sheets spreadsheet with data that is retrieved from an external API via URLFetchApp:

function callExternalAPI(endpoint) {
  const url = baseUrl   endpoint;
  const response = UrlFetchApp.fetch(url, {
    "muteHttpExceptions": true,
    "headers": {
      "X-Schema-Version": "latest",
      "Authorization": "Bearer "   apiKey
    }
  });

  return response;
}

This function is called a couple of times whenever the user manually presses a refresh button that I put in a custom menu:

Preview of external menu on Google Sheets

In total, each refresh should perform no more than a dozen or so API calls.

Important note: The API I'm using has a rate limit of 600 requests per IP per minute.

When I (based in Malta) refresh the API data myself, everything works fine. However, when a different user (based in the Phillipines) tries to do the same thing, the external API responds with an error message stating that the rate limit is hit.

I do not have access to the server-side configuration of the external API.

Is there any way to work around this? The user is not able to use the spreadsheet at all because of the rate limit.

CodePudding user response:

All requests made from UrlFetchApp.fetch() originate from Google's servers. Google leverages a fixed set of IP addresses per region for Apps Script. So, if a lot of scripts from different user accounts (that are restricted to the same pool of IP addresses) in the same region are hitting that API, chances are the 600 request IP quota will be exhausted.

I imagine the Phillipines to be one of the more active locations, with a high volume of devs in the region. So there is unlikely to be an App-Script based solution for your issue.

However, if you can afford it (both in terms of cost and time/effort to learn), you can leverage different cloud technologies, such as Google Cloud Functions. They typically have dynamic IPs but you can get a static IP address unique to a Cloud Function, so you won't run into any issues with exhausting the API quota.

  • Related