Five Day Weather Forecast
Explore how to retrieve a five day weather forecast with data points every three hours using the OpenWeatherMap API. Learn to make API requests in JavaScript, handle responses, and parse forecast data for specific geographical locations.
We'll cover the following...
Overview
This API allows us to retrieve the weather forecast for a period of 5-days for any specific location. This forecast provides weather data collected after every 3-hour interval. We can retrieve the forecast data in the JSON and XML formats.
5-day/3-hour forecast
We’ll make a GET request to the following URI to get the 5-day/3-hour forecast data for a location of our choice:
http://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&appid={APP_ID}
Request parameters
Parameter | Type | Category | Description |
| string | required | This is our unique API key. |
| decimal | required | This is the location’s latitude. |
| decimal | required | This is the location’s longitude. |
| string | optional | This refers to the format of the API response. Its default value is |
| integer | optional | This is the number of timestamps to retrieve. |
| string | optional | This refers to the units of measurement. Its possible values are |
| string | optional | This allows us to retrieve the output in our desired language. |
Lines 16–17: We enter 47.6038321 and -122.3300624, the geographical coordinates for Seattle, United States, for the required parameters lat and lon, respectively.
Lines 19–23: We may enter other optional parameter(s) listed in the table above to the queryParameters object to fine-tune our results.
Click the “Run” button in the widget below to see the output.
Let’s take a quick look at the code given in the widget above:
- Line 8: We define the URL for the 5-day/3-hour forecast endpoint.
- Lines 32–43: We define a custom function
getForecasts()to make an API call using fetch and handle any exception if it occurs. TheprintResponse()andprintError()are custom functions to print the API response and errors, respectively. - Line 46: We invoke the
getForecasts()function.
The output of the code above displays the 5-day weather forecast data for the specified location. It includes data collected after every 3-hour interval.
Response fields
Let’s look into the response fields generated by this endpoint.
Parameter | Type | Description |
| decimal | This is the location’s latitude. |
| decimal | This is the location’s longitude. |
| integer | This is the city's ID. |
| string | This is the city's name. |
| integer | This is the time of sunrise. Its units are Unix and UTC. |
| integer | This is the time of sunset. Its units are Unix and UTC. |
| integer | This is the number of retrieved timestamps. |
| integer | This is the percentage of cloudiness. Its units are in percentage (%). |
| integer | This is the time at which the data was forecasted. Its units are Unix and UTC. |
| decimal | This specifies the temperature as per human perception. For |
| integer | This is the percentage of humidity. Its unit is percentage (%). |
| integer | This specifies the default atmospheric pressure at sea level. Its unit is hPa. |
| decimal | This specifies the actual temperature. For |
| integer | This is the precipitation probability. Its unit is percentage (%). |
| integer | This specifies the average visibility. Its unit is meters. |
| string | This is the description of weather conditions. |
| string | This specifies the weather parameters' group. Its sample values are |
| decimal | This is the wind speed. For |
| integer | This is the volume of rain over the last 3 hours. Its unit is millimeters. Its value may be |
| integer | This is the volume of snow over the last 3 hours. Its unit is millimeters. Its value may be |
Note: The above list of response fields is not exhaustive. Head over to the Additional Response Fields lesson in the Appendix for the remaining response fields for this endpoint.