← Back

JavaScript Weather API Practice Exercise

Use this guided practice page to rebuild a weather forecast request from memory. The browser calls this website's backend, while the private WeatherAPI key remains on the server.

Review the complete Weather API lesson when you want to compare your solution with the full explanation.

Learning objectives

Request flow

Form submission
-> validate city and days
-> fetch("/api/weather?...")
-> backend validates the request
-> backend asks WeatherAPI.com with its private key
-> browser renders selected forecast data

Working practice form

Enter a city and request one to three forecast days. Watch the status message while the request is running, then inspect the result and the Network panel in browser DevTools.

Enter a city to begin.

Weather data by WeatherAPI.com.

Practice steps

  1. Select the form, status paragraph, result list and submit button.
  2. Listen for submit and call event.preventDefault().
  3. Read and trim elements.city.value, then read elements.days.value.
  4. Build /api/weather parameters without adding any API key.
  5. Disable the button and show a loading message during the request.
  6. Check the HTTP response before reading its JSON body.
  7. Render the forecast or show the backend's safe error message.
  8. Restore the button in finally().

Further challenges

← Back