How long do Google Apps Script triggers wait before timing out?

I’m working on a Google Apps Script that fetches data from an external API and puts it into a Google Sheet. The script runs automatically every minute using a time-based trigger.

The problem is that my script keeps failing with timeout errors several times each day. I also get ‘Address Unavailable’ errors sometimes. The API provider told me their service can take up to 30 seconds to respond when busy, but all requests should finish within 60 seconds.

I need to know what the default timeout limit is for Google Apps Script triggers. Does the timeout change if nobody has the spreadsheet open in their browser?

Is there any way to increase the timeout duration for triggers? I want to make sure my script waits long enough for the API to respond.

You’re hitting the urlfetch timeout, not the trigger timeout. That API taking 30-60 seconds is exactly why you’re getting failures. Even with emmad’s 90 second suggestion, that’s still risky. Try caching responses or splitting requests into smaller batches. Also check if the API has webhooks instead of polling every minute.

Google Apps Script triggers timeout after 6 minutes max, regardless of whether the spreadsheet is open. This limit applies to all trigger types, including time-based triggers. Unfortunately, you cannot extend this timeout duration as it is hardcoded by Google.

Given that your API can take up to 60 seconds to respond, the timeout shouldn’t be the main concern here. Six minutes should generally be sufficient. The ‘Address Unavailable’ errors you are encountering indicate potential network issues between Google’s servers and your API. Such connection failures may cause your script to hang while waiting for responses that fail to arrive.

To address this, I recommend implementing error handling using try-catch blocks. Consider utilizing UrlFetchApp.fetch() with the ‘muteHttpExceptions’ parameter for more controlled handling of requests. Additionally, incorporating retry logic with exponential backoff can help when your API experiences temporary downtimes.

Samuel87’s right about the 6-minute limit, but there’s another piece to this puzzle. UrlFetchApp operations timeout after 60 seconds by default. If your API provider says responses can take up to 60 seconds, you’re cutting it really close. I’ve hit this same wall with slow APIs before. What saved me was setting a custom timeout using the timeout parameter in UrlFetchApp.fetch(). Bump it up to 90 seconds to give yourself some breathing room: UrlFetchApp.fetch(url, {timeout: 90000}) - that’s in milliseconds. Also, running triggers every minute might be too aggressive for a slow API. Try spacing them out to every 2-3 minutes so you don’t get overlapping executions when the API’s getting hammered.