Importing from APIs: Ordering Weather Data off the Menu

Every machine learning model in this archive is hungry for the same thing: data. Before an algorithm can find patterns in the weather, someone has to get the weather into a table — and the fastest way to do that is an API, a web service that hands you forecasts and observations on request. This entry covers what a weather API is, how to pull a real forecast with a few lines of Python, and the ground rules (keys, rate limits, terms) that keep you a welcome guest.

The Restaurant Model: Why You Order Instead of Cook

Producing weather data from scratch means running the kitchen yourself: satellite feeds, station networks, and numerical models that demand supercomputer time. An API lets you skip all of that and order off a menu instead.

The restaurant does the expensive work once and serves thousands of customers. That economy of scale is why even large forecasting operations source much of their raw data through APIs rather than running their own instruments.

Placing a First Order: Open-Meteo

Open-Meteo is an ideal first kitchen: its forecast API is free for non-commercial use with no API key required. A request is one URL — https://api.open-meteo.com/v1/forecast — plus parameters. The required ones are latitude and longitude (WGS84 coordinates); hourly takes a comma-separated list of variables such as temperature_2m,precipitation; forecast_days accepts 0–16 (default 7).

Here is a complete, runnable example using Python’s Requests library:

import requests

params = {
    "latitude": 52.52,      # Berlin — swap in your own coordinates
    "longitude": 13.41,
    "hourly": "temperature_2m,precipitation",
    "forecast_days": 2,
}
resp = requests.get("https://api.open-meteo.com/v1/forecast",
                    params=params, timeout=30)
resp.raise_for_status()     # stop early if the kitchen sends back an error
data = resp.json()

unit = data["hourly_units"]["temperature_2m"]
for time, temp in zip(data["hourly"]["time"][:6],
                      data["hourly"]["temperature_2m"][:6]):
    print(f"{time}  {temp} {unit}")

The response is JSON with an hourly object holding parallel arrays — one of ISO 8601 timestamps, one per requested variable — plus an hourly_units object naming the units (°C, mm). The loop above prints the next six hours of temperatures.

Three menu details worth knowing from the start: timestamps arrive in GMT unless you send a timezone parameter; daily aggregations (a daily parameter instead of hourly) require that timezone parameter; and units default to metric, with options like temperature_unit=fahrenheit and precipitation_unit=inch if you need them. A past_days parameter (up to 92) even lets you reach back for recent history in the same call.

The Government Kitchen: NOAA’s National Weather Service API

For United States coverage, the official alternative is the National Weather Service API at api.weather.gov — the forecasts, observations, and alerts produced by the US government’s own forecasters. It is also free and keyless, but with a different house rule: every request must carry a User-Agent header identifying your application (ideally with contact information), so NWS can reach you if something goes wrong. Its rate limits are not published, but the documentation describes them as generous for typical use, and a blocked request can usually be retried within about five seconds.

House Rules: Keys, Rate Limits, and Terms

Wherever you order from, three rules keep you seated:

In Action at Dendrology

Beyond the Basics

Ready to go past the first order?

An API turns the hardest part of weather data science — getting the data — into a single well-formed request. Master the menu, and Dendrology’s entire forecasting toolkit is one order away.