Skip to main content

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.
export const getCurrentWeather = async (lat: number, lon: number)

Parameters

lat
number
required
Latitude coordinate (-90 to 90)
lon
number
required
Longitude coordinate (-180 to 180)

Returns

Returns a promise that resolves to a weather object or null if the request fails.
coord
object
Geographic coordinates
weather
array
Weather condition information
main
object
Main weather metrics
wind
object
Wind information
name
string
City name

Example

import { getCurrentWeather } from '@/lib/api/weather';

const weatherData = await getCurrentWeather(40.7128, -74.0060);

if (weatherData) {
  console.log(`Temperature in ${weatherData.name}: ${weatherData.main.temp}°C`);
  console.log(`Conditions: ${weatherData.weather[0].description}`);
}

Error Handling

The function catches all errors and returns null 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.
export const getWeatherByCity = async (city: string)

Parameters

city
string
required
City name (can include country code, e.g., “London,UK”)

Returns

Returns the same weather object structure as getCurrentWeather, or null if the city is not found.

Example

import { getWeatherByCity } from '@/lib/api/weather';

const weatherData = await getWeatherByCity('Buenos Aires');

if (weatherData) {
  console.log(`Current weather in ${weatherData.name}:`);
  console.log(`${weatherData.main.temp}°C - ${weatherData.weather[0].description}`);
} else {
  console.log('City not found');
}

Error Handling

Returns null if:
  • The city name is invalid or not found
  • The API request fails
  • Network connectivity issues occur
Errors are logged with “Error en getWeatherByCity:” prefix.

getWeatherForecast

Retrieves extended weather forecast (5 days, 3-hour intervals). Returns up to 40 forecast data points.
export const getWeatherForecast = async (lat: number, lon: number)

Parameters

lat
number
required
Latitude coordinate (-90 to 90)
lon
number
required
Longitude coordinate (-180 to 180)

Returns

Returns an array of forecast objects, or null if the request fails. Each forecast object contains weather data for a 3-hour interval.
dt
number
Unix timestamp of the forecast time
main
object
Temperature and atmospheric data (same structure as current weather)
weather
array
Weather conditions (same structure as current weather)
wind
object
Wind information
pop
number
Probability of precipitation (0 to 1)
dt_txt
string
Forecast time in text format (YYYY-MM-DD HH:MM:SS)

Example

import { getWeatherForecast } from '@/lib/api/weather';

const forecast = await getWeatherForecast(40.7128, -74.0060);

if (forecast) {
  // Get first 8 entries for 24-hour forecast
  const next24Hours = forecast.slice(0, 8);
  
  next24Hours.forEach(entry => {
    console.log(`${entry.dt_txt}: ${entry.main.temp}°C`);
    console.log(`Rain probability: ${(entry.pop * 100).toFixed(0)}%`);
  });
}

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 the NEXT_PUBLIC_OPENWEATHER_API_KEY environment variable:
NEXT_PUBLIC_OPENWEATHER_API_KEY=your_api_key_here

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)

Build docs developers (and LLMs) love