OpenWeatherMap

OpenWeatherMap

Science

Access current weather data for any location including over 200,000 cities. You can get Current Weather Data of your city, or sort it a little to hourly forecast for next 4 days. One call api, that lets you shift, sort and filter days. Also allows you to perform bulk downloading.

Visit API

📚 Documentation & Examples

Everything you need to integrate with OpenWeatherMap

🚀 Quick Start Examples

OpenWeatherMap Javascript Examplejavascript
// OpenWeatherMap API Example
const response = await fetch('http://openweathermap.org/api', {
    method: 'GET',
    headers: {
        'Content-Type': 'application/json'
    }
});

const data = await response.json();
console.log(data);

Getting Started with OpenWeatherMap API

OpenWeatherMap API provides current weather data, forecasts and historical data for any geographic location. In this blog post, we will explore how to use the OpenWeatherMap API with JavaScript, by providing you with step-by-step instructions and code samples.

Step 1: Register for an API Key

To use the OpenWeatherMap API, you need to register for an API key. Head over to the website http://openweathermap.org/api and create an account by following the instructions. Once you have created your account, you will receive an API key that you can use to make API requests.

Step 2: Install the Required Packages

To retrieve openWeather data through API, we need node-fetch package which can be installed via NPM.

npm install node-fetch

Step 3: Fetching Current Weather Data

To fetch the current weather data for a specific location, you can use the following code:

const fetch = require('node-fetch');

const apiKey = '<YOUR_API_KEY>';
const city = '<CITY_NAME>';
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}`;
     
fetch(url)
    .then(response => response.json())
    .then(data => console.log(data))
    .catch(error => console.error(error));

You will need to replace <YOUR_API_KEY> with your actual API key, and <CITY_NAME> with the name of the city for which you want to retrieve the current weather data.

Step 4: Fetching Forecast Data

To fetch forecast data for a specific location, you can use the following code:

const fetch = require('node-fetch');

const apiKey = '<YOUR_API_KEY>';
const city = '<CITY_NAME>';
const url = `https://api.openweathermap.org/data/2.5/forecast?q=${city}&appid=${apiKey}`;
     
fetch(url)
    .then(response => response.json())
    .then(data => console.log(data))
    .catch(error => console.error(error));

You will need to replace <YOUR_API_KEY> with your actual API key, and <CITY_NAME> with the name of the city for which you want to retrieve the forecast data.

Step 5: Fetching Historical Data

To fetch historical weather data for a specific location, you can use the following code:

const fetch = require('node-fetch');

const apiKey = '<YOUR_API_KEY>';
const city = '<CITY_NAME>';
const start = '<START_DATE>'; // format YYYY-MM-DD
const end = '<END_DATE>'; // format YYYY-MM-DD
const url = `https://api.openweathermap.org/data/2.5/onecall/timemachine?q=${city}&dt=${start}&end=${end}&appid=${apiKey}`;
     
fetch(url)
    .then(response => response.json())
    .then(data => console.log(data))
    .catch(error => console.error(error));

You will need to replace <YOUR_API_KEY> with your actual API key, <CITY_NAME> with the name of the city for which you want to retrieve the historical data, <START_DATE> with the start date in the format of YYYY-MM-DD, and <END_DATE> with the end date in the format of YYYY-MM-DD.

Conclusion

In this blog post, we have explored how to use the OpenWeatherMap API with JavaScript. With these simple code examples, you can easily retrieve current weather, forecast and historical weather data for any location. The OpenWeatherMap API has many other features and endpoints, so be sure to check out the official documentation http://openweathermap.org/api for more information.

📊 30-Day Uptime History

Daily uptime tracking showing online vs offline minutes

Jun 12Jun 14Jun 16Jun 18Jun 20Jun 22Jun 24Jun 26Jun 28Jun 30Jul 2Jul 4Jul 6Jul 8Jul 1104008001440Minutes
Online
Offline

Related APIs in Science