Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

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

What you will learn

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

On your resume

Learn on Codify