Node.js Open Weather Map API for Weather Forecasts
The Open Weather Map API is very popular as it allows you to request weather forecasts and historical weather data programmatically.
Feature of Open Weather Map API:
- It is easy to get started and easy to use.
- It is widely used and popular API for Weather Forecasts.
Installation of request module:
- You can visit the link to Install Request module. You can install this package by using this command.
- After installing request module you can check your request version in command prompt using the command.
- Now go to Open Weather Map website and create an account and get your API KEY.
- After that, you can create a folder and add a file, for example index.js. To run this file you need to run the following command.
npm install request
npm version request
node index.js
Filename: index.js
index.js
const request = require( 'request' ); var API_KEY = 'your_api_key' ; const forecast = function (latitude, longitude) { var url = `http: //api.openweathermap.org/data/2.5/weather?` +`lat=${latitude}&lon=${longitude}&appid=${API_KEY}` request({ url: url, json: true }, function (error, response) { if (error) { console.log( 'Unable to connect to Forecast API' ); } else { console.log( 'It is currently ' + response.body.main.temp + ' degrees out.' ); console.log( 'The high today is ' + response.body.main.temp_max + ' with a low of ' + response.body.main.temp_min ); console.log( 'Humidity today is ' + response.body.main.humidity ); } }) } var latitude = 22.7196; // Indore latitude var longitude = 75.8577; // Indore longitude // Function call forecast(latitude, longitude); |
Steps to run the program:
- The project structure will look like this:
- Make sure you have installed request module using following command:
- Run index.js file using below command:
npm install request
node index.js
So this is how you can use the Open Weather Map API which allows you to request weather forecasts and historical weather data programmatically.
Please Login to comment...