Show me: fetch, ordering with code
In the last lesson, you opened an API in your browser by hand. Now your JavaScript will do the same thing, with the fetch command:
const url = 'https://api.open-meteo.com/v1/forecast?latitude=49.19&longitude=16.61¤t_weather=true';
const response = await fetch(url);
const data = await response.json();
console.log(data.current_weather.temperature);
What’s happening here, line by line:
fetch(url): send the request (the waiter heads to the kitchen).await: wait for it to come back. The internet takes a moment!response.json(): convert the response into a JavaScript object.- Then you work with the data like any regular object: dot, dot, done.
await only works inside a function marked async, or in a <script type="module">. If your browser shows the error “await is only valid in async functions”, wrap your code like this:
async function load() {
// fetch and await go here
}
load();
Print the data on the page
The console is for programmers: a visitor wants to see the data on the page:
<p id="temperature">Loading…</p>
<script type="module">
const response = await fetch('https://api.open-meteo.com/v1/forecast?latitude=49.19&longitude=16.61¤t_weather=true');
const data = await response.json();
document.querySelector('#temperature').textContent =
`Right now it's ${data.current_weather.temperature} °C`;
</script>
Familiar stuff from Magician (querySelector, textContent) plus the new fetch. Nothing more.
Your turn 💪
- Add a paragraph with
id="temperature"and the script above to your website. - Change the coordinates to your own city.
- Add the wind speed to the display too (
data.current_weather.windspeed). - Bonus: when the temperature is below 0, add ”🥶 Bundle up!” to the text.
Check: The real current temperature appears on your page after it loads. If you turn off Wi-Fi and refresh the page, it stays on “Loading…”: proof that the data really comes from the internet.
What to take away from this lesson
fetch(url)sends the request,awaitwaits for the response..json()converts the response into an object you already know how to work with.- Data from the whole internet is now just one function away.
© 2026 Ing. Martin Polak / AlgoRhino · Content usage terms