This Nodejs Application help to understand weatherStack API Integration.We will create sample nodejs application that will access free weather information using Rest API.
The Weatherstack API is providing a free API(Allowed 1000 calls/month) to access weather information.
There are following features provided by WeatherStack –
- Providing Free Whether API
- Global weather information with one easy to use API
- You can get realtime, historical, and forecast weather data
- Weather information in your local language
- You can get multiple locations weather information with on single request.
- You can integrate API with many programming languages Like Node.js, Ruby, Go, Python, PHP, and jQuery
WeatherStack API Example Using Node ExpressJS
I am using express framework to create this API.
WeatherStack API Token
We will get API token by signing up at WeatherStack.
Basic Usage Of WeatherStack API
The sample syntax of API access with token as follows, You need to pass token and query with API URI.
We will use axios to make http request into nodejs application. The Axios is a Promise based HTTP client for the react/nodejs application.The AXIOS can be used for both the browser and Node.js. You can install axios using npm
, bower or CDN.
Get Weather API Data Using Axios
We will create axios instance and use HTTP Get method fetch data from server.
const axios = require('axios'); const params = { access_key: 'YOUR_ACCESS_KEY', query: 'New York' } axios.get('https://api.weatherstack.com/current', {params}) .then(response => { const apiResponse = response.data; console.log(`Current temparture in ${apiResponse.location.name} is ${apiResponse.current.temperature}℃`); }).catch(error => { console.log(error); });
They are providing many options to pass location by city name, latitude/longitude, postal code, IP address, or even Geolocation via the fetch:ip
option.
Forecast
The post WeatherStack – A Free Weather API Example Using Nodejs appeared first on Rest Api Example.