Building a personal data project starts with gathering data. What better way to start than by exploring a free and well-documented API? In this article, we’ll dive into the National Weather Service API (api.weather.gov) to understand how it works, and we’ll write Python code to retrieve and explore weather data.
So… what are you going to be doing?
- Let’s read through the docs and try to understand how to navigate the API.📜
- I’m gonna write some Python code to fetch weather data.
- I’m gonna analyze the data structure of the
forecastHourly
endpoint.
📍 Understanding the API
The National Weather Service API organizes data using a grid-point system. Each grid point covers an area of 2.5 km x 2.5 km. The /points
endpoint is where you get information about retrieving data for a specific location.
Key Features of the /points
Endpoint
When you pass a latitude and longitude to /points
, you receive metadata about that location, including:
- URLs for forecast data:
forecast
: 12-hour forecast over seven days.forecastHourly
: Hourly forecast over seven days.forecastGridData
: Raw grid forecast data.
- Observation stations near the point.
- Associated zones (e.g., forecast zone, fire weather zone, county zone).
Here’s an example API call for Springfield, MO (latitude: 37.209
, longitude: -93.2923
):
|
|
🛠️ Constructing an API Request
Before making requests, it’s important to review the authentication requirements. The Weather API doesn’t require an API key (yet) but does require a User-Agent header for identification. This can be a string unique to your application, and including contact information is encouraged.
Here’s how to construct a Python request to the /points
endpoint:
Python Code to Query the /points
Endpoint
|
|
Exploring the /points
Response
Here’s a snippet of what you’ll see in the response:
|
|
The most relevant field for our purposes is the forecastHourly
URL. Let’s use it to fetch detailed hourly weather data.
Fetching Hourly Forecast Data
The forecastHourly
endpoint provides an array of weather data for each hour over the next seven days. Each entry includes:
startTime
andendTime
: The hour’s time range.temperature
andtemperatureUnit
: The forecasted temperature.relativeHumidity
: The percentage of humidity. 💧probabilityOfPrecipitation
: Chance of rain. ☔windSpeed
andwindDirection
: Wind conditions. 🌬️shortForecast
anddetailedForecast
: Summary descriptions.icon
: A URL to an icon representing the forecast. 🖼️
Python Code to Query the forecastHourly
Endpoint
|
|
Inspecting the forecastHourly
Data Structure
Here’s an example of a single period from the forecastHourly
response:
|
|
Key fields:
startTime
andendTime
: Define the hourly time period.temperature
: The forecasted temperature, with the unit (F
for Fahrenheit).relativeHumidity
: The percentage of humidity in the air.probabilityOfPrecipitation
: Chance of rain (in percentages).windSpeed
andwindDirection
: Provide details about wind conditions.shortForecast
: A concise summary of expected weather.icon
: A URL to an image representing the forecast.
The forecastHourly
endpoint returns an array of up to 155 periods, representing a week of hourly forecasts.
Exploring Further
By running this code, you’ll be able to:
- Extract and examine metadata about a location.
- Fetch detailed hourly weather data.
- Understand the structure of the API responses for further use.
This sets the stage for the next article, where we’ll build a FastAPI application to store this data in a PostgreSQL database and create an API to retrieve it. Stay tuned!
In the next article, we’ll transform this weather data into a full-fledged database-driven API!