Step 1 of 6

Meet the weather API

Welcome! In this course you’ll build a small Python program that fetches a real weather forecast, saves it, and draws a chart. You don’t need any experience. We’ll go one small step at a time, and every lesson ends with something that runs.

Goal for this lesson: see real weather data arrive on your screen.

What’s an API?

An API is a way for one program to ask another program for information. Think of ordering at a counter: you ask for something in a set way, and you get a set kind of answer back. We’ll use a free weather service called Open-Meteo. It needs no sign-up and no key.

Step 1: Look at the answer in your browser

Paste this address into your browser:

https://api.open-meteo.com/v1/forecast?latitude=29.42&longitude=-98.49&hourly=temperature_2m&temperature_unit=fahrenheit&forecast_days=2&timezone=auto

You’ll see a wall of text in curly braces. That’s the forecast for San Antonio, Texas, written in a format called JSON. Look for "temperature_2m": that’s a long list of temperatures, one for each hour.

Step 2: Set up Python

Open a terminal and check that Python is installed:

python3 --version

Then install the one library we need for talking to the web:

python3 -m pip install requests matplotlib

(We’ll use matplotlib later for the chart. Installing it now saves a stop.)

Step 3: Ask for it from Python

Make a new folder called weather-app, and inside it a file called weather.py:

import requests

URL = (
    "https://api.open-meteo.com/v1/forecast"
    "?latitude=29.42&longitude=-98.49"
    "&hourly=temperature_2m&temperature_unit=fahrenheit&forecast_days=2&timezone=auto"
)

response = requests.get(URL, timeout=10)
print("Status:", response.status_code)
print(response.text[:200])

Run it:

python3 weather.py

You should see Status: 200 (that means “all good”) and the first bit of the same text you saw in the browser.

Check yourself

Did you see Status: 200? If you got an error about requests, run the install command from Step 2 again. If you got a connection error, check your internet and try once more.

Next up: turn that wall of text into something Python can use.