Service invoked too many times in a short time: urlfetch

So, you’re working with Google Apps Script. Perhaps you’re working in Google Sheets. You’ve got a long list of URLs for which you’re trying to get response data. And you’re trying to use the fetchAll method of the UrlFetchApp class: UrlFetchApp.fetchAll(). You’re passing your array of hundreds of URLs to the fetchAll method and you’re getting the following error:

Service invoked too many times in a short time: urlfetch. Try Utilities.sleep(1000) between calls

You’re thinking, ‘How the hell am I supposed to use the “sleep” method between calls when I’m only calling fetchAll once?’

The solution: split your array into chunks of, say, 100 URLs. You pass a set of 100 URLs at a time to the fetchAll method. And between each call to fetchAll, you call Utilities.sleep(1000). And voilà! You get back the desired responses for all the URLs in your list. Hooray!

Here’s an example:

// Specify the maximum size of each array to pass in each call to fetchAll
const chunkSize = 100;

// Get the URLs from the spreadsheet and flatten the array
var urls = sheet.getRange(1, 1, sheet.getLastRow()).getValues().flat();

// Set the request parameters for each URL
var requests = urls.map[1]url) => ({'url': url, 'followRedirects': false, 'muteHttpExceptions': true};

// Iterate through each chunk of requests
for (let i = 0; i < requests.length; i += chunkSize) {
  
  // Get the next subset of requests
  var chunk = requests.slice(i, i + chunkSize);
  
  // Get the response data from the subset of requests
  var responses = UrlFetchApp.fetchAll(chunk);

  // For each response, log the response code
  responses.forEach[2]response) => Logger.log(response.getResponseCode();

  // Sleep for 1 second before making another call to fetchAll
  Utilities.sleep(1000);
} 

References

References
1 url) => ({'url': url, 'followRedirects': false, 'muteHttpExceptions': true}
2 response) => Logger.log(response.getResponseCode(

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.