Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

OpenWeatherMap returns 401 Unauthorized in a Flutter app

ErrorInvalid API key

Short answer

A 401 from OpenWeatherMap means the server did not accept the key on that request. Test the same URL with curl first. The cause is one of three: the appid parameter is missing or misspelled, the key was created less than about two hours ago, or your plan does not include the endpoint you called.

  • Applies to Flutter 3.35, Dart 3.9, package http 1.x, OpenWeatherMap current weather API
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Send the exact URL your app builds, but from the terminal. If curl also gets a 401, the problem is the key or the plan, not your Dart code.

    curl -i "https://api.openweathermap.org/data/2.5/weather?id=524901&appid=YOUR_API_KEY"
  2. 2Match what you see, then jump to its fix

    1. 1401 on every call, and the URL printed from your app has no appid=... part or spells it differentlyThe key is not sent as the appid query parameterFix
    2. 2A brand new key gives 401 while an older key from the same account worksThe key was created a moment ago and is not active yetFix
    3. 3Current weather works, but another endpoint such as the 16-day forecast returns 401 with the same keyYour plan does not include the product you are callingFix

What does the error look like?

  • Invalid API key
  • 401 Unauthorized

Cause 1 of 3

The key is not sent as the appid query parameter

1Check

The FAQ says to add the key as an appid parameter to the GET request, and a request without it gets 401. Check the spelling of the parameter name. Print the final URL before the request goes out.

final uri = Uri.https('api.openweathermap.org', '/data/2.5/weather', {
  'id': '524901',
  'appid': apiKey,
});
print(uri);

2Fix

Build the URL with a query map that contains appid, and read the key from one place so a blank value is caught early.

assert(apiKey.isNotEmpty, 'API key is empty');
final response = await http.get(uri);

3Verify

The same URL now returns weather JSON instead of a 401 body.

curl -i "https://api.openweathermap.org/data/2.5/weather?id=524901&appid=YOUR_API_KEY"

Confirmed by openweathermap.org

Did this fix it?

Cause 2 of 3

The key was created a moment ago and is not active yet

1Check

The FAQ says a key is activated automatically up to 2 hours after registration. For a paid plan, activation follows the payment.

curl -s -o /dev/null -w "%{http_code}\n" "https://api.openweathermap.org/data/2.5/weather?id=524901&appid=YOUR_API_KEY"

2Fix

Wait and retry with the same key. Do not generate new keys in the meantime; the wait is on their side. Check the key in your account page against the one in your code, character by character.

# re-run every few minutes until the status code changes from 401 to 200
curl -s -o /dev/null -w "%{http_code}\n" "https://api.openweathermap.org/data/2.5/weather?id=524901&appid=YOUR_API_KEY"

3Verify

The status line shows 200.

curl -i "https://api.openweathermap.org/data/2.5/weather?id=524901&appid=YOUR_API_KEY" | head -n 1

Confirmed by openweathermap.org

Did this fix it?

Cause 3 of 3

Your plan does not include the product you are calling

1Check

The FAQ states that a Free plan does not cover some products, for example the 16-day daily forecast API, historical weather data and Weather Maps 2.0, and that requesting them returns 401. Compare the endpoint in your code with the subscription shown in your account.

grep -rn "api.openweathermap.org" lib/

2Fix

Call an endpoint your plan includes, or upgrade the subscription that covers the product.

// current weather, as in the FAQ example
Uri.https('api.openweathermap.org', '/data/2.5/weather', {'id': '524901', 'appid': apiKey});

3Verify

The endpoint answers 200 with the same key.

curl -i "https://api.openweathermap.org/data/2.5/weather?id=524901&appid=YOUR_API_KEY" | head -n 1

Confirmed by openweathermap.org

Did this fix it?

How do I stop it coming back?

  • Keep the key in one constant or build-time define, never typed twice, and log whether it is empty at start-up.
  • Test a new key with curl before pasting it into the app.
  • Check the plan page before you pick an endpoint from the docs.
  • Do not commit the key to a public repository; rotate it if you did.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.