Understanding history is key to predicting and understanding the future, especially when it comes to weather.
With historical weather data, you get access to data regarding the weather conditions in a particular location over a long period of time. These records include hourly, weekly, yearly, and even decades-old data. A well-maintained database of historical weather data includes a wide range of weather variables such as rainfall, temperature, wind speed, wind direction, humidity, barometric pressure, and much more.
This type of in-depth historical weather data helps us understand the past and current weather conditions in a particular region. This data is also valuable when it comes to predicting future weather conditions. Therefore, adding this data to your application can improve the reliability of your data for user activities and processes that rely on a specific weather environment.
Whatever you’re building, here is how you can integrate historical weather data using a weather API.
How to integrate weather data to your app
In this section, I have put together a series of steps that you should follow to successfully add historical weather data to your website or application.
Choose a reliable weather API
Unless you have access to a continuously updated database of weather data, your first step should be to pick a reliable weather API. Weather APIs provide access to a wide range of weather information and data collated by weather stations around the world. Through a weather API, developers can easily integrate live, historical, and forecast weather information to websites, applications, and custom widgets.
To demonstrate how a Weather API works, we will use the . With Tomorrow.io, you can get every piece of the real-time, forecast, and historical data to power your decisions. The API also comes with additional weather insights such as fire index, road risk, and map layers that you can embed on your application. We’re going to be using the free version which supports up to 1000 API calls per day.
Obtain your API key
Before pulling weather information from an API to your application, you must first obtain an API key. The API key is either included in the request header or passed as a query parameter to securely authenticate all requests made to the API. For Tomorrow.io, you can find your API key in the dashboard, as shown below.
Note: For demonstrative purposes, we have not redacted the API key in the snapshot above. However, your and never exposed to the public. API key disclosure can lead to unwanted API calls, app blockage, data leaks, or even worse.
Add code to call the Tomorrow.io API
Like any other weather provider, Tomorrow.io provides multiple endpoints for pulling weather data. In this case, we are interested in the historical weather API endpoint.
As you can see, the historical weather endpoint on the Tomorrow.io API provides three options for embedding historical weather data. The option you choose depends on how far back in history you’d like your data to go. You can use choose to fetch data for the past six hours, past four weeks, or even access weather records dating 20 years old.
Once you have the coordinates ready, you can start building the API call by passing the location coordinates and query parameters to the API call function below. When using Node.JS, the basic call function looks like this:
In the above call function, pass the latitude and longitude for the target location as query parameters. You are also required to provide a timestep in minutes, which represents the time interval of fetching data between the start time and end time. Next, enter the data fields for weather elements you’d like to fetch. Let’s use precipitation, temperature, humidity, wind speed, and baro_pressure in our call.
Here is the complete API request after supplying all the above query parameters.
Analyze JSON response
Once the above API call is sent successfully, www.tomorrow.io responds with a JSON file containing the requested weather data. Below is the JSON response containing historical weather data for Liverpool as requested in the API call.
In the request, we specified a 30-min time step. This means the API returns multiple data-sets for the target duration. In our case, we have five data-sets containing historical weather data for the specified period.
Customize the weather display component
You can customize how this historical weather data is displayed on your website or application. This involves creating a display component such that every time the server returns a JSON file, it is parsed, and the data fed into an output template rendered on the web page. Additionally, you can add some styling elements to make it more readable and appealing to end-users.
Add map tiles to enhance the user experience
At this point, pretty much of the work on integrating weather data into your application is done. However, you can still improve the visualization of weather data that users retrieve by adding map layers. For this, we use a third-party mapping tool like , , or . The Tomorrow.io API integrates seamlessly with any of these mapping service providers.
Here’s a sample weather map for Bloomington, Nebraska, generated using Mapbox:
Other effective ways of enhancing your application include adding a search function that allows users to query the application by location ID. Instead of providing the city coordinates, the user only needs to key in a unique location ID or name, then proceed to retrieve the data. Additionally, you can integrate notifications and create better response messages to make your application more interactive.
There you go! You have all the steps it takes to integrate historical weather information into your website or application. As you can see, the integration process is simple and straightforward. Additionally, you are not limited to the capabilities covered in this article. Weather APIs provide tons of data for individual and business consumption. So, follow the above steps and get all the historical weather data you need for your users.