r/GoogleAppsScript • • 2d ago

Question Problem accessing weather data from open-meteo.com using API call within Google Sheet with timed triggers

Hi,

I'm new to creating App scripts so please bear with me.

I've created a script to extract current weather details using a call to open-meteo.com and then write to a sheet.

When I test the script manually by pressing 'run' all works as expected, the script completes and the extracted data is written to the sheet.

So then I've set two timed triggers (6am and 6pm) to run the script.

However I get the following reports when these timers fire:

Exception: Request failed for https://api.open-meteo.com returned code 429. Truncated server response: {"error":true,"reason":"Daily API request limit exceeded. Please try again tomorrow."}

Reading the documentation there appears to be a 10,000 daily limit on the API calls.

So my assumption is that when the script is triggered there have already been too many calls from a particular Google address on that day?

I guess the manual run is treated differently for some reason?

Is this a correct assumption?

If that is the case, is there some way of making the call that will work without hitting the API call limit?

Any help welcome.

Thanks

0 Upvotes

4 comments sorted by

2

u/Unusual_Analyst3648 2d ago

Hello,

Two free alternatives:

WeatherAPI.com

OpenWeatherMap.org

Alternatively, you can modify your code to include a loop that retries up to 5 times in the event of a 429 error.

Google has thousands of IP addresses. If you make a request and it fails (with a 429 error), waiting a few seconds and trying again will often route your request through a different Google server, one that may not be blocked.

1

u/shadewood_mole 2d ago

Many thanks for the alternatives and workaround. I'll give them both a try.

2

u/No-Sheepherder7116 2d ago edited 2d ago

Yes, your assumption is basically right, with one important detail: that daily limit is per IP address, not per account.

Apps Script's UrlFetchApp does not go out from your own IP. It runs on shared Google infrastructure, so your two calls a day are sharing that day's quota with other people's scripts that happen to use the same outgoing IP. That is why it can already be used up by 6am, and why a manual run later can work — you are likely hitting a different outgoing IP, or a time when that IP's quota has not been used up yet.

A quick retry loop alone is not a reliable fix. If that IP's quota is used up for the day, retrying a few seconds later will just get the same 429, and it adds more calls. A retry with a delay can sometimes help if you land on a different outgoing IP, but do not depend on it.

What I would do:

  1. Make exactly one call per run, and only ask for the current fields you actually need. Do not call it in a loop, once per row, or once per cell.

  2. Move the triggers off the hour, for example 6:20am and 6:20pm instead of 6:00. A lot of time triggers fire on the hour, so that is the busiest time for the shared quota. Time triggers are approximate anyway, so this does not change when the sheet is updated in any meaningful way.

  3. Save the last good result, and fall back to it on a 429 instead of failing or writing a blank or error row. This is the part that makes the twice-daily sheet usable even when a run hits the limit.

Here is a drop-in pattern for the fetch part. Keep your existing request, and only write to the sheet after this returns data:

function getWeatherWithRetry(url, maxAttempts) {
  maxAttempts = maxAttempts || 3;
  var props = PropertiesService.getScriptProperties();

  for (var attempt = 1; attempt <= maxAttempts; attempt++) {
    try {
      var response = UrlFetchApp.fetch(url, { muteHttpExceptions: true });
      var code = response.getResponseCode();

      if (code === 200) {
        props.setProperty('lastWeather', response.getContentText());
        return JSON.parse(response.getContentText());
      }

      Logger.log('Weather request failed, HTTP ' + code + ', attempt ' + attempt);
      if (code !== 429 && code !== 500 && code !== 502 && code !== 503) {
        break;
      }
    } catch (e) {
      Logger.log('Weather request error on attempt ' + attempt + ': ' + e);
    }

    if (attempt < maxAttempts) {
      Utilities.sleep(attempt === 1 ? 10000 : 20000);
    }
  }

  var cached = props.getProperty('lastWeather');
  if (cached) {
    Logger.log('Using saved weather after failed attempts.');
    return JSON.parse(cached);
  }

  throw new Error('Weather request failed after ' + maxAttempts + ' attempts.');
}

Then in your main function, call that, and only write the values to the sheet if it returns something. If it falls back to the saved result, add a note in the sheet like "cached from earlier run" so you know that row is not fresh.

If you want a long-term fix rather than working around the shared quota, switch to a weather provider that gives you a personal API key. Then the quota is tied to your key, not to a shared outgoing IP, so other Apps Script users cannot use it up.

1

u/shadewood_mole 1d ago

Thank you for the detailed reply. I'd guessed as much about the shared calls from lots of Google scripts from one IP address.

The code is useful as I'm still learning but for now I think using a personal API key is probably the better way to go.

Unusual_Analyst3648 has suggested a couple for me to try.

Thanks again to both of you.