Node.js | Dark Sky API for Weather Forecasts

The Dark Sky API is very popular as it allows you to request weather forecasts and historical weather data programmatically.

Feature of Dark Sky API:

  1. It is easy to get started and easy to use.
  2. It is widely used and popular API for Weather Forecasts.

Installation of request module:

  1. You can visit the link to Install Request module. You can install this package by using this command.
    npm install request
  2. After installing request module you can check your request version in command prompt using the command.
    npm version request
  3. Now go to Dark Sky Offical website and create an account and get your API KEY.
  4. 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.
    node index.js

Filename: index.js

filter_none

edit
close

play_arrow

link
brightness_4
code

const request = require('request');
var API_KEY = 'Your_API_KEY';
  
const forecast = function (latitude, longitude) {
  
        + API_KEY + '/' + latitude + ', ' + longitude
        + '?units=si&exclude=minutely, hourly, flags, alerts';
  
    request({ url: url, json: true }, function (error, response) {
        if (error) {
            console.log('Unable to connect to Forecast API');
        } else if (response.body.error) {
            console.log('Unable to find location. Try to '
                    + 'search another location.');
        } else {
            console.log(response.body.daily.data[0].summary);
            console.log('It is currently ' 
                + response.body.currently.temperature
                + ' degrees out.'
            );
  
            console.log('The high today is ' 
                + response.body.daily.data[0].temperatureHigh
                + ' with a low of ' 
                + response.body.daily.data[0].temperatureLow
            );
  
            console.log('There is a '
                + response.body.currently.precipProbability
                + '% chance of rain.'
            );
        }
    })
}
  
var latitude = 22.7196; // Indore latitude
var longitude = 75.8577; // Indore longitude
  
// Function call
forecast(latitude, longitude);

chevron_right


Steps to run the program:

  1. The project structure will look like this:
    project structure
  2. Make sure you have installed request module using following command:
    npm install request
  3. Run index.js file using below command:
    node index.js

    Output of above command

So this is how you can use the Dark Sky API which allows you to request weather forecasts and historical weather data programmatically.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.