OpenWeatherMap returns 401 Unauthorized in a Flutter app
Invalid API keyShort 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.
Fix it fast
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"2Match what you see, then jump to its fix
- 1
401 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
A 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
Current 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
- 1
What does the error look like?
Invalid API key401 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
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 1Confirmed by openweathermap.org
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 1Confirmed by openweathermap.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fixMobile apps
Dart cannot turn a compact timestamp like 20201101153000 into a DateTime
FormatException: Invalid date format
DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space.
2 causes, each with a fix