A simple and easy-to-use wrapper for the OpenWeather API to fetch weather data for a given city. It allows you to get current weather information such as temperature, humidity, weather conditions, and wind speed.
- Fetch weather data for any city.
- Retrieves information such as temperature, weather description, humidity, and wind speed.
- Uses
.envfile to securely store and load the OpenWeather API key.
Simply install the package with
npm i openweather-wrapper-api
To use the OpenWeather API, you need an API key. You can get it for free by signing up on OpenWeather.
Create a .env file in the root directory of the project and add your OpenWeather API key like so:
OPENWEATHER_API_KEY=your_api_key_hereReplace your_api_key_here with the API key you obtained from OpenWeather.
Important: Make sure your
.envfile is not committed to version control (e.g., Git). You can add.envto your.gitignorefile to prevent it from being tracked.
Once you have the .env file set up, you need to install the necessary dependencies. Run the following command in your terminal:
npm installThis will install the axios and dotenv dependencies required to interact with the OpenWeather API and load environment variables.
After setting up your environment and installing the necessary dependencies, you can start using the package to fetch weather data.
In your JavaScript code, import the getWeatherByCity function from the package:
const { getWeatherByCity } = require('openweather-wrapper-api');You can now call getWeatherByCity(city) to get the weather information for a specific city. Make sure to replace "city" with the name of the city you want to look up.
Here is an example:
const { getWeatherByCity } = require('openweather-wrapper-api');
// Fetch weather data for a city
async function getWeather() {
try {
const weather = await getWeatherByCity('Berlin');
console.log(`Weather in ${weather.city}:`);
console.log(`Temperature: ${weather.temperature}°C`);
console.log(`Weather: ${weather.weather}`);
console.log(`Humidity: ${weather.humidity}%`);
console.log(`Wind Speed: ${weather.windSpeed} m/s`);
} catch (error) {
console.error('Error fetching weather data:', error);
}
}
getWeather();Here is an example of the output you will receive:
Weather in Berlin:
Temperature: 15°C
Weather: Clear sky
Humidity: 50%
Wind Speed: 3.6 m/s
This project is licensed under the MIT License - see the LICENSE file for details.
If you'd like to contribute to this project, feel free to open an issue. Any contributions, whether small or large, are welcome!
Thank you for using the OpenWeather Wrapper API! If you have any questions, feel free to open an issue or reach out.
Enjoy coding! 😊