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);
}
Thank you, this worked for me! For anyone coming across this in the future – at least of time of writing (January 2025) UrlFetchApp.fetchAll maxes out at a chunkSize of 200, any more and you get the “invoked too many times” error even before sending the requests. Also, Utilities.Sleep(1000) is not necessary if you are fetching from a server where the response time is more than a second, since the rate limiting includes the time fetchAll takes to receive a response. I timed my fetchAll(200) requests and they took about 10 seconds, so I took the Utilities.Sleep(1000) out.