OpenWeatherMap Node.js
You can use the OpenWeatherMap API in Node.js using HTTP, Request, Unirest, or Axios.
View the RapidAPI Node.js docs here.
How to use the OpenWeatherMap API in Node.js (Node.js Example)
1. Sign up for a Free RapidAPI User Account
From any page on the RapidAPI Marketplace, click “Sign Up” and register for a free account.
2. Navigate the OpenWeatherMap API page
Get to the OpenWeatherMap API Page by clicking here or searching for it in the RapidAPI marketplace search bar.
3. Subscribe to the API
Next, click on the API’s Pricing Tab and select a plan to subscribe to. The OpenWeatherMap API has 2 pricing plans on RapidAPI:
- Basic – OpenWeatherMap’s free plan. $0.00/month with a 100 request/day hard limit. Rate limited at 10 requests/minute.
- Pro – $10/month that offers unlimited API requests. Rate limited at 100 requests/minute.
4. Test an OpenWeatherMap API Endpoint
After subscribing to a pricing plan, head back to the endpoints page and choose an endpoint, fill out the required parameters, and click “Test Endpoint”.
If done correctly, on the right side of the API console, you should see a response like this:
5. Copy the Node.js (HTTP, Request, Unirest, Axios) Code Snippet and add it to your application!
Now that you have successfully tested that the API works, click on the Code Snippet dropdown and select one of the following:
- Node.js –> HTTP
- Node.js –> Request
- Node.js –> Unirest
- Node.js –> Axios
You’ll see something similar to this:
HTTP
var settings = { var http = require("https"); var options = { "method": "GET", "hostname": "community-open-weather-map.p.rapidapi.com", "port": null, "path": "/weather?q=London%252Cuk", "headers": { "x-rapidapi-host": "community-open-weather-map.p.rapidapi.com", "x-rapidapi-key": "[your rapidapi key]" } }; var req = http.request(options, function (res) { var chunks = []; res.on("data", function (chunk) { chunks.push(chunk); }); res.on("end", function () { var body = Buffer.concat(chunks); console.log(body.toString()); }); }); req.end();
Request
var request = require("request"); var options = { method: 'GET', url: 'https://community-open-weather-map.p.rapidapi.com/weather', qs: {q: 'London%2Cuk'}, headers: { 'x-rapidapi-host': 'community-open-weather-map.p.rapidapi.com', 'x-rapidapi-key': '[your rapidapi key]' } }; request(options, function (error, response, body) { if (error) throw new Error(error); console.log(body); });
Unirest
var request = require("request"); var options = { method: 'GET', url: 'https://community-open-weather-map.p.rapidapi.com/weather', qs: {q: 'London%2Cuk'}, headers: { 'x-rapidapi-host': 'community-open-weather-map.p.rapidapi.com', 'x-rapidapi-key': '[your rapidapi key]' } }; request(options, function (error, response, body) { if (error) throw new Error(error); console.log(body); });
Axios
const axios = require("axios"); axios({ "method":"GET", "url":"https://community-open-weather-map.p.rapidapi.com/weather", "headers":{ "content-type":"application/octet-stream", "x-rapidapi-host":"community-open-weather-map.p.rapidapi.com", "x-rapidapi-key":"[your rapidapi key]" },"params":{ "q":"London%2Cuk" } }) .then((response)=>{ console.log(response) }) .catch((error)=>{ console.log(error) })
Modify as needed and drop it into your application.
Voila, you’re done!
What data is available with the OpenWeatherMap API?
The OMW API has 4 endpoints available on RapidAPI:
- GET Current Weather Data
- GET Call 16 day / daily forecast data
- GET Search Weather Data
- GET 5 day / 3 hour forecast data
GET Current Weather Data
Description: Using this kind of requests you can get weather data in any location on the earth. The current weather data are updated online based on data from more than 40,000 weather stations.
Example request:
var http = require("https"); var options = { "method": "GET", "hostname": "community-open-weather-map.p.rapidapi.com", "port": null, "path": "/weather?q=London%252Cuk", "headers": { "x-rapidapi-host": "community-open-weather-map.p.rapidapi.com", "x-rapidapi-key": "[your rapidapi key]" } }; var req = http.request(options, function (res) { var chunks = []; res.on("data", function (chunk) { chunks.push(chunk); }); res.on("end", function () { var body = Buffer.concat(chunks); console.log(body.toString()); }); }); req.end();
GET Call 16 day / daily forecast data
Description: 16-day forecasts are available for any location or city. Forecasts include daily weather and available in JSON or XML format. It is only available for all paid accounts.
Example request:
var http = require("https"); var options = { "method": "GET", "hostname": "community-open-weather-map.p.rapidapi.com", "port": null, "path": "/forecast/daily?q=san%20francisco%252Cus", "headers": { "x-rapidapi-host": "community-open-weather-map.p.rapidapi.com", "x-rapidapi-key": "[your rapidapi key]" } }; var req = http.request(options, function (res) { var chunks = []; res.on("data", function (chunk) { chunks.push(chunk); }); res.on("end", function () { var body = Buffer.concat(chunks); console.log(body.toString()); }); }); req.end();
GET Search Weather Data
Description: By city name. Input the city name or its part and get the list of the most proper cities in the world. Example – Lon or Lond or London. The more precise city name you put the more precise list you will get. To make it more precise put the city’s name or its part, comma, the name of the county or 2-letter country code. You will get all the proper cities in the chosen county. The order is important – the first is city name then a comma then the county. Example – Lon, UK or Lon, GB or London, GB or Lon, England. By geographic coordinates.
Example request:
var http = require("https"); var options = { "method": "GET", "hostname": "community-open-weather-map.p.rapidapi.com", "port": null, "path": "/find?units=imperial%252C%20metric&q=london", "headers": { "x-rapidapi-host": "community-open-weather-map.p.rapidapi.com", "x-rapidapi-key": "[your rapidapi key]" } }; var req = http.request(options, function (res) { var chunks = []; res.on("data", function (chunk) { chunks.push(chunk); }); res.on("end", function () { var body = Buffer.concat(chunks); console.log(body.toString()); }); }); req.end();
GET 5 day / 3 hour forecast data
Description: A 5-day forecast is available for any location or city. It includes weather data every 3 hours. The forecast is available in JSON or XML format.
Example request:
const axios = require("axios"); axios({ "method":"GET", "url":"https://community-open-weather-map.p.rapidapi.com/find", "headers":{ "content-type":"application/octet-stream", "x-rapidapi-host":"community-open-weather-map.p.rapidapi.com", "x-rapidapi-key":"[your rapidapi key]" },"params":{ "units":"imperial%2C metric", "q":"london" } }) .then((response)=>{ console.log(response) }) .catch((error)=>{ console.log(error) })