Get your API key
Every request to the OpenWeatherMap REST API authenticates with an API key passed in the appid query parameter. Follow these steps to create a free account, retrieve your key, store it in an environment variable, and verify that it's active.
Step 1: Create an OpenWeatherMap account
- Open the OpenWeatherMap sign-up page in your browser.
- Enter a username, your email address, and a password.
- Confirm that you meet the age requirement, accept the privacy policy and terms of service, and select Create Account.
- Open the verification email from OpenWeatherMap and select the confirmation link to activate your account.
Step 2: Copy or generate an API key
- Sign in to your OpenWeatherMap account.
- Select your username in the top navigation bar and select My API keys, or open
https://home.openweathermap.org/api_keysdirectly on the My API keys page. - Locate the Default key created automatically for your account, or enter a label such as
local-devunder Create key and select Generate to create a dedicated key. - Copy the 32-character hexadecimal key string to your clipboard.
Step 3: Store your key in an environment variable
Code samples and tutorials throughout this documentation read your key from the OPENWEATHER_API_KEY environment variable so credentials stay out of your source code.
- Set
OPENWEATHER_API_KEYin your terminal session, replacingyour_api_keywith the 32-character key you copied.
- macOS / Linux
- Windows (PowerShell)
export OPENWEATHER_API_KEY="your_api_key"
To keep the variable available in future terminal windows, append the export line to ~/.zshrc (macOS and Zsh) or ~/.bashrc (Linux and Bash) and reload your shell configuration.
$env:OPENWEATHER_API_KEY = "your_api_key"
To persist the variable across future PowerShell sessions for your user account, run [Environment]::SetEnvironmentVariable("OPENWEATHER_API_KEY", "your_api_key", "User") and open a new terminal window.
- Print the variable to confirm that your current terminal session recognizes it.
- macOS / Linux
- Windows (PowerShell)
echo "$OPENWEATHER_API_KEY"
$env:OPENWEATHER_API_KEY
Treat your API key like a password. Never hardcode it in application files or commit it to Git repositories. Load credentials from environment variables, and add .env files to .gitignore before making your first commit.
Step 4: Wait for activation and verify your key
New API keys typically activate within 10–15 minutes, though activation can take up to 2 hours.
- Send a test request for London using your
OPENWEATHER_API_KEYenvironment variable.
- macOS / Linux
- Windows (PowerShell)
curl "https://api.openweathermap.org/data/2.5/weather?q=London&appid=$OPENWEATHER_API_KEY"
curl.exe "https://api.openweathermap.org/data/2.5/weather?q=London&appid=$env:OPENWEATHER_API_KEY"
- Compare the JSON response against the two possible states below.
Pending activation (401 Unauthorized)
If your key is still activating—or if OPENWEATHER_API_KEY contains a typo—the API returns HTTP 401 with this JSON error payload:
{
"cod": 401,
"message": "Invalid API key. See https://openweathermap.org/faq#error401 for more info."
}
Confirm that OPENWEATHER_API_KEY matches the 32-character string on your My API keys page with no extra spaces, wait 10–15 minutes, and run the command again.
Active key (200 OK)
Once activation finishes, the endpoint returns a 200 OK weather payload (abbreviated below):
{
"coord": { "lon": -0.1257, "lat": 51.5085 },
"weather": [
{
"id": 804,
"main": "Clouds",
"description": "overcast clouds",
"icon": "04d"
}
],
"main": {
"temp": 285.15,
"feels_like": 284.32,
"humidity": 72
},
"name": "London",
"cod": 200
}
When you see "cod": 200, your API key is active and ready to use. Notice that "temp": 285.15 uses Kelvin by default when units is omitted, which you'll switch to Celsius on the next page.
Next step
Continue to Make your first request to fetch metric weather data with curl, Node.js, or Python.