Add a 15-day weather forecast to your app in 5 minutes

2026-11-25来源 dev.to JavaScriptEnglish

Need a weather forecast in your app – a travel planner, an event page, a dashboard for a farm or a
construction site? This walkthrough uses the WeatherOnline Weather Forecast API on RapidAPI:
forecasts for locations worldwide, up to 15 days daily plus hourly, 3-hourly and 6-hourly data.
The free BASIC plan (500 requests/month) is enough to follow along.

1. Get a key

Open the API page,
subscribe to the BASIC plan (free) and copy your X-RapidAPI-Key from the playground.

Every request sends two headers:

X-RapidAPI-Key: <your key> X-RapidAPI-Host: weatheronline-weather-forecast.p.rapidapi.com 

2. Find a location

Forecasts are requested by a WeatherOnline location key. Search by name:

const HOST = 'weatheronline-weather-forecast.p.rapidapi.com'; const headers = { 'X-RapidAPI-Key': process.env.RAPIDAPI_KEY, 'X-RapidAPI-Host': HOST }; const search = await fetch(`https://${HOST}/v2/search?q=London`, { headers }).then(r => r.json()); console.log(search.results[0]); // { wmo: '03770', name: 'London', country: 'UK', lat: 51.5085, lon: -0.12574, timezone: 'Europe/London' } 

Take the wmo value. If you already have coordinates (e.g. from the browser's geolocation),
skip this step and pass lat and lon instead – the nearest forecast location is used.

3. Get the forecast

const fc = await fetch(`https://${HOST}/v2/forecast/daily?key=03770`, { headers }).then(r => r.json()); console.log(fc.location.city); // "City of Westminster" for (const day of fc.daily) { const { weekday, mday, mon } = day.fcttime; console.log(`${weekday} ${mday}.${mon}.: ${day.airtemp_daytime_max}° / ${day.airtemp_nighttime_min}°, ` + `rain ${day.precip_period_acc} mm, UV ${day.uvindex_daytime}`); } 

Each day also has humidity, pressure, wind per 6-hour segment, sunshine, frost probability,
sunrise/sunset and moon phase. All values come as strings – convert with Number() where needed.

Hourly data works the same way:

const hourly = await fetch(`https://${HOST}/v2/forecast/hourly?lat=40.71&lon=-74.01&temp_unit=F&wind_unit=mph`, { headers }).then(r => r.json()); console.log(hourly['1hourly'][0].fcttime_date, hourly['1hourly'][0].airtemp_date); 

Useful parameters: lang (13 languages), temp_unit (C/F), wind_unit (ms, kph, mph, kn, bft),
rain_unit (mm/inch).

4. Keep the key on the server

Don't ship your RapidAPI key in browser code. Put a tiny proxy in front – the complete example
(a Node server without dependencies plus a small HTML page) is here:
weather-widget example.

Errors

Status error Meaning
400 invalid_parameter / missing_location Bad value, or neither key nor lat+lon
429 rate_limited Plan limit reached
502 upstream_error Temporary – retry

Attribution

When you display the data, credit "Weather data: WeatherOnline" with a link to
https://www.weatheronline.co.uk.

Happy building – questions go to the API's Discussions tab on RapidAPI.

原文:https://www.a8q9.cn/en/article/add-a-15-day-weather-forecast-to-your-app-in-5-minutes-fc992a.html

相关文章