Projects › Beginner
Weather App
Search any city or use your location to see current weather and a 5-day forecast from a real API.
Level: Beginner · Time: 2–4 days · Tech stack: JavaScript, Fetch API, OpenWeather API
Features to build
- Search by city with recent searches
- "Use my location" with the Geolocation API
- Current weather, feels-like, humidity, wind
- 5-day forecast cards
- Loading and error states, °C / °F toggle
What you will learn
- Calling REST APIs with fetch
- async/await and error handling
- Working with JSON
- API keys
Step-by-step guide
1. Get a free API key
Sign up at OpenWeatherMap and copy your key. (For a public repo, put the key in a tiny serverless function so it isn't exposed.)
2. Fetch current weather
async function getWeather(city) {
const url = `https://api.openweathermap.org/data/2.5/weather?q=${encodeURIComponent(city)}&units=metric&appid=${KEY}`;
const res = await fetch(url);
if (!res.ok) throw new Error(res.status === 404 ? 'City not found' : 'Weather service error');
return res.json();
}
3. Show loading and errors
form.addEventListener('submit', async (e) => {
e.preventDefault();
setState('loading');
try { render(await getWeather(input.value.trim())); }
catch (err) { setState('error', err.message); }
});
Never leave the user staring at a blank screen.
4. 5-day forecast
The /forecast endpoint returns 3-hourly data. Group it by date and pick the reading closest to 12:00 for each day.
5. Use the device location
navigator.geolocation.getCurrentPosition(({ coords }) => getByCoords(coords.latitude, coords.longitude)) — handle the "permission denied" case.
6. Remember recent searches
Keep the last 5 cities in localStorage and show them as chips.
Level it up
- Change the background with the weather
- Debounced city autocomplete
- Install it as a PWA that works offline
On your resume
- Built a weather app consuming the OpenWeather REST API with geolocation, a 5-day forecast and robust loading/error handling.