Overview
The Weather API provides functions to retrieve current weather conditions and forecasts using the OpenWeatherMap API. All functions return data in metric units with Spanish language support.getCurrentWeather
Retrieves current weather data by geographic coordinates (latitude/longitude). Ideal for automatic GPS-based location detection.Parameters
Latitude coordinate (-90 to 90)
Longitude coordinate (-180 to 180)
Returns
Returns a promise that resolves to a weather object ornull if the request fails.
Geographic coordinates
Weather condition information
Main weather metrics
Wind information
City name
Example
Error Handling
The function catches all errors and returnsnull instead of throwing. Errors are logged to the console with the prefix “Error en getCurrentWeather:“.
getWeatherByCity
Searches for current weather data by city name. Perfect for manual city search functionality.Parameters
City name (can include country code, e.g., “London,UK”)
Returns
Returns the same weather object structure asgetCurrentWeather, or null if the city is not found.
Example
Error Handling
Returnsnull if:
- The city name is invalid or not found
- The API request fails
- Network connectivity issues occur
getWeatherForecast
Retrieves extended weather forecast (5 days, 3-hour intervals). Returns up to 40 forecast data points.Parameters
Latitude coordinate (-90 to 90)
Longitude coordinate (-180 to 180)
Returns
Returns an array of forecast objects, ornull if the request fails. Each forecast object contains weather data for a 3-hour interval.
Unix timestamp of the forecast time
Temperature and atmospheric data (same structure as current weather)
Weather conditions (same structure as current weather)
Wind information
Probability of precipitation (0 to 1)
Forecast time in text format (YYYY-MM-DD HH:MM:SS)
Example
Usage Notes
- The full response contains up to 40 forecast entries (5 days × 8 per day)
- Components typically use
.slice(0, 8)to display the next 24 hours - All temperatures are in Celsius (metric units)
- Language is set to Spanish (“lang=es”)
Configuration
All weather functions require theNEXT_PUBLIC_OPENWEATHER_API_KEY environment variable:
API Endpoint
Base URL:https://api.openweathermap.org/data/2.5
Default Settings
- Units: Metric (Celsius, km/h)
- Language: Spanish (“es”)
- Timeout: No explicit timeout set (browser default)
