Add Weather Data to Your App
Weather looks like one API call. In practice it is location, caching, units, time zones and a key that must stay off the page. Here is the setup that holds up.

Key takeaways
- Call the weather API from an edge function with the key in Secrets, never from the browser.
- Round coordinates to two decimals and cache each result for 15 to 30 minutes: fewer calls, better privacy, the same answer for users.
- Ask the provider for the location's time zone and your preferred units up front, or "today" and "tomorrow" will be wrong for half your users.
To add weather data to your app, pick a weather API (Open-Meteo, OpenWeatherMap, WeatherAPI.com or Tomorrow.io are common choices), store its key in your backend secrets, and fetch the forecast from a small server function that caches results by rounded location for 15 to 30 minutes. The browser asks your function, not the provider, so the key stays private and a busy day does not blow through your quota. Then map the provider's condition codes to your own icons and show the time in the location's time zone.
What you will build
A weather card and a forecast strip: current temperature, "feels like", a condition icon and short label, wind and rain chance, then the next 12 hours and the next 7 days. It works from the visitor's location when they allow it and from a city search when they do not. Typical homes for it: an outdoor event page ("Saturday looks dry"), a sports club app deciding whether practice is on, a travel planner, a farm or garden log, or a game that changes its sky to match the player's real weather.
What you need
- An account with a weather provider and an API key. Free tiers are usually enough to build and launch a small app. Read the terms: some free tiers are for non-commercial use only, some require visible attribution, and some cap calls per minute as well as per day.
- A backend. Startbase Cloud gives your project edge functions to make the call and Secrets to hold the key, saved under a name like WEATHER_API_KEY.
- A geocoding source if users type city names. Most weather providers offer one alongside the forecast; it turns "Porto" into a latitude, longitude and time zone.
The data flow
- 01Location. The page asks for the visitor's position with the browser's geolocation API (it only works over HTTPS, and only after the visitor agrees). If they decline, show a city search instead of an error.
- 02Round. Round latitude and longitude to two decimal places, roughly a kilometer. Weather does not change meaningfully within that distance, and you are no longer storing anyone's exact position.
- 03Ask your function. The page calls your weather function with the rounded coordinates and the user's unit preference.
- 04Check the cache. The function looks in a weather_cache table for a row with the same rounded coordinates fetched in the last 20 minutes. If it finds one, it returns it immediately.
- 05Fetch and store. Otherwise it calls the provider with the key from Secrets, normalizes the response into your own shape, saves it with a fetched_at timestamp, and returns it.
- 06Render. The page draws the card and forecast from your normalized shape, never from the raw provider response, so switching providers later touches one function.
A normalized shape keeps the rest of the app simple. Something like: location name, time zone, units, current (temperature, feels_like, condition, wind_speed, wind_direction, humidity, observed_at), hourly (an array of time, temperature, condition, precipitation_chance) and daily (date, high, low, condition, precipitation_chance, sunrise, sunset). Store condition as your own small vocabulary (clear, partly_cloudy, cloudy, fog, drizzle, rain, snow, thunder) and map each provider's codes to it in one place.
Many providers use the WMO weather code table (0 for clear sky, 61 to 65 for rain of increasing intensity, 95 for thunderstorm, and so on). Others use their own IDs. Either way, a single lookup table in your function is the only place those numbers should appear. If the caching idea is new to you, what is caching covers why a short time-to-live is the right tradeoff here.
Units, time zones and "today"
Two bugs show up in almost every first weather feature. The first is units: most APIs default to metric or to Kelvin, and Americans expect Fahrenheit and miles per hour. Ask for the units explicitly, store the user's preference, and label every number (18°C, 12 km/h). Do not convert in several places.
The second is time. A forecast for Tokyo viewed from New York should show Tokyo's afternoon as afternoon. Request times in the location's time zone (many APIs accept a timezone=auto style option and return the zone name), keep that zone with the data, and format with it. Otherwise "today" flips at the viewer's midnight instead of the location's, and your 7-day strip quietly shows the wrong days.
A brief you can paste
Add weather to the event page. Create an edge function get-weather that takes latitude, longitude and units (metric or imperial). It rounds coordinates to 2 decimals, returns a cached result from a weather_cache table if one is younger than 20 minutes, and otherwise calls the weather provider using the WEATHER_API_KEY secret, requesting times in the location's time zone. Normalize the response to: current (temp, feels_like, condition, wind, rain_chance), hourly for the next 12 hours, and daily for 7 days, with conditions mapped to clear, partly_cloudy, cloudy, fog, rain, snow or thunder. On the page, show a weather card for the event's venue coordinates and a 7-day strip that highlights the event date. Default to the browser's locale for units with a toggle. If the call fails, hide the card and show nothing broken.
Pitfalls
- Calling the provider from the browser. The key is visible to anyone who opens developer tools, and every page view is a billed call. Our guide to API keys and secrets explains why "hidden" frontend variables are still public.
- No cache. A post that goes viral turns into thousands of identical calls for the same city in the same minute. A 15 to 30 minute cache keyed on rounded coordinates is enough.
- Promising precision you do not have. Forecasts are model output. Say "70% chance of rain" rather than "it will rain at 3 pm", and show when the data was fetched.
- Asking for location on page load. Visitors deny unexplained prompts, and browsers remember the denial. Ask when they tap "Use my location", and offer city search alongside.
- Ignoring attribution rules. Some free tiers require a visible credit line or link. Put it under the card from day one.
- Severe weather alerts. Official warnings come from national weather services with their own feeds and wording. If your app shows them, show the official text and source rather than paraphrasing.
Weather is a good first integration because it exercises every part of the pattern: a secret, a server function, a cache and a clean data shape. Once it works, the same structure carries to almost any read-only API. For a fuller picture of where server code fits, see edge functions explained.
Frequently asked questions
Is there a free weather API?
Yes. Several providers have free tiers, and Open-Meteo offers keyless access for non-commercial use. Check each provider's terms for commercial use, attribution and rate limits before you launch.
Can I call a weather API directly from my website?
You can, but if the API needs a key, that key becomes public. Route calls through a server function that holds the key and caches results, which also protects your quota.
How often should I refresh weather data?
Every 15 to 30 minutes is a good starting point for current conditions. Most providers update their models less often than that, so refreshing every minute only costs calls.
How do I get weather for the user's location?
Use the browser's geolocation API after the user taps a button, round the coordinates, and send them to your weather function. Offer a city search for people who decline location access.

