Python Weather API Example

This beginner-friendly example shows how to request weather data from an API, read the JSON response, and print a few useful values.

The goal here is to build a simple real-world Python example. You do not need to fully understand HTTP yet. You only need to see the basic pattern:

  1. Send a request
  2. Get data back
  3. Read the JSON
  4. Print the values you want

Quick example

import requests

url = "https://api.open-meteo.com/v1/forecast"
params = {
    "latitude": 40.7128,
    "longitude": -74.0060,
    "current_weather": True
}

response = requests.get(url, params=params)
data = response.json()

print(data["current_weather"]["temperature"])
print(data["current_weather"]["windspeed"])

This uses the free Open-Meteo API and the requests package.

It keeps the example small:

  • Make a GET request
  • Parse JSON
  • Print values

What this example does

This example:

  • Makes a GET request to a weather API
  • Sends location data as query parameters
  • Gets JSON data back from the API
  • Reads a few fields from the response
  • Prints the current weather values

What you need before running it

Before you run the code, make sure you have:

  • Python installed
  • The requests package installed with pip
  • An internet connection
  • Basic understanding of dictionaries and function calls

If you are new to API requests, see how to make an API request in Python.

Install requests

The requests package is not built into Python, so you need to install it first.

Run:

pip install requests

If that does not work, try:

python -m pip install requests

You can also check that it is installed with:

python -c "import requests; print(requests.__version__)"

How the code works step by step

Here is the same example again:

import requests

url = "https://api.open-meteo.com/v1/forecast"
params = {
    "latitude": 40.7128,
    "longitude": -74.0060,
    "current_weather": True
}

response = requests.get(url, params=params)
data = response.json()

print(data["current_weather"]["temperature"])
print(data["current_weather"]["windspeed"])

1. Import requests

import requests

This gives you access to the requests package so you can send HTTP requests.

2. Set the API URL

url = "https://api.open-meteo.com/v1/forecast"

This is the API endpoint you are calling.

3. Create a params dictionary

params = {
    "latitude": 40.7128,
    "longitude": -74.0060,
    "current_weather": True
}

This dictionary holds the query parameters sent with the request.

In this example:

  • latitude and longitude choose the location
  • current_weather asks for current weather data

4. Call requests.get() with the URL and params

response = requests.get(url, params=params)

This sends a GET request to the API.

If you print response.url, you can see the full URL with the parameters added to it.

5. Convert the response to Python data

data = response.json()

The API sends JSON text back.
response.json() converts that JSON into normal Python data, usually dictionaries and lists.

If you want a deeper explanation, read how to parse JSON in Python or Python JSON module overview.

6. Access nested dictionary values

print(data["current_weather"]["temperature"])
print(data["current_weather"]["windspeed"])

The response contains a top-level dictionary.

Inside it, current_weather is another dictionary.
Inside that dictionary, temperature and windspeed are keys.

7. Print the result

The program prints the values returned by the API.

Your output will vary depending on the real weather, but it may look something like this:

22.4
13.1

Understanding the JSON response

The API returns JSON text. After you call response.json(), you get Python data you can work with.

A simplified response might look like this:

{
    "latitude": 40.71,
    "longitude": -74.01,
    "current_weather": {
        "temperature": 22.4,
        "windspeed": 13.1,
        "winddirection": 240,
        "weathercode": 3
    }
}

Important parts:

  • The whole response is a dictionary
  • current_weather is a dictionary inside the response
  • temperature and windspeed are keys inside current_weather

So this line:

data["current_weather"]["temperature"]

means:

  • get the value for "current_weather"
  • then get the value for "temperature" inside it

If you want more practice reading API data, see how to handle API responses in Python.

Simple improvements to the example

Here is a slightly better version that prints labels, rounds values, and checks the status code first:

import requests

city_name = "New York"
url = "https://api.open-meteo.com/v1/forecast"
params = {
    "latitude": 40.7128,
    "longitude": -74.0060,
    "current_weather": True
}

response = requests.get(url, params=params)

if response.status_code == 200:
    data = response.json()
    current_weather = data.get("current_weather", {})

    temperature = current_weather.get("temperature")
    windspeed = current_weather.get("windspeed")

    if temperature is not None and windspeed is not None:
        print(f"Weather for {city_name}")
        print(f"Temperature: {round(temperature, 1)}°C")
        print(f"Wind Speed: {round(windspeed, 1)} km/h")
    else:
        print("Weather data is missing expected values.")
else:
    print("Request failed.")
    print("Status code:", response.status_code)

This version improves the original example by:

  • Storing the city name separately for display
  • Printing clear labels
  • Rounding values before printing
  • Checking the status code before using the response
  • Handling missing keys more safely with .get()

Common problems when this example fails

Some common causes are:

  • requests is not installed
  • There is no internet connection
  • You used the wrong key name when reading the JSON
  • You switched to an API that requires an API key
  • You assumed every weather API uses the same JSON structure

You may also run into these specific errors:

  • ModuleNotFoundError because requests is not installed
  • KeyError because the expected JSON key is missing
  • TypeError from treating a list like a dictionary
  • Connection-related request failure due to network issues
  • An authentication error when using an API that needs a key

If you see a missing-key problem, read KeyError in Python: causes and fixes.
If Python cannot import requests, see ModuleNotFoundError: no module named X fix.

Beginner debugging steps

If the example does not work, try these steps.

print(response.status_code)

This tells you whether the request succeeded.

print(response.text)

This helps you inspect the exact data returned by the API.

print(data.keys())

This shows which keys exist at the top level of the response.

print(data["current_weather"])

Only do this if that key exists.

Compare your key names with the real response

A very common beginner mistake is using a key name that does not exactly match the API response.

For example, this will fail if the key is not present:

print(data["weather"]["temp"])

Always compare your code with the actual JSON returned by the API.

Useful install and check commands:

pip install requests
python -m pip install requests
python -c "import requests; print(requests.__version__)"

FAQ

Do I need an API key for this example?

No. The Open-Meteo version shown here does not require an API key.

Some other weather APIs do require one.

Why use requests.get() instead of opening a URL manually?

requests.get() is easier for beginners.

It makes it simple to:

  • send query parameters
  • read the response
  • work with JSON data

What type of data does response.json() return?

Usually Python dictionaries and lists, depending on the JSON structure.

Why does my code raise KeyError?

The key name in your code may not match the real API response.

Print the response and inspect the keys before trying to read nested values.

See also