Ask a real weather service for tomorrow’s forecast. Learn what a web API, a URL, a query and a status code are, send a request with fetch and read its JSON body with a second await, give that body a type, and check response.ok so a bad answer is caught by your own code, while no answer at all ends in fetch failed.
TypeScript
Fundamentals
`fetch`: getting data from a web API. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
Tonight you plan tomorrow. One task on your list is to walk to work
instead of taking the bus, and that depends on the weather. Your
phone's weather app knows it: a moment ago it asked a weather service
over the internet, and the service sent the forecast back. So far
TaskKit's data came from a file and from your keyboard. A website is
a third place, and lesson 11.1 named it as one of the waits a program
has: tomorrow's weather from a website, which takes a moment to
answer.
Think of ordering a book at a library's desk. You write on a slip
exactly which book you want and hand it over. The librarian first
tells you whether they have it, and only then goes to fetch the book
itself. Your program can ask a weather service the same way. By the
end of this lesson, a program of yours asks a real one for tomorrow's
highest temperature, prints it, and says what went wrong when the
answer is a bad one or never comes.
A web API: a website that answers programs
A website you open in a browser sends back pages for people to read.
Some websites answer programs instead: they send back only data,
usually as JSON (JavaScript Object Notation, values written as text,
lesson 12.1). Such a website is a web API. API
is short for application programming interface: the agreed way one
program asks another for something, which questions it may ask and
what the answers look like. This lesson asks Open-Meteo, a free
weather API that needs no account.
Everything you ask a web API is written as an address, a URL (Uniform
Resource Locator, the full address of something on the internet), the same
kind of address you type into a browser. The address this lesson uses is
written in one piece, with no spaces; here it is part by part:
https://api.open-meteo.com: which computer to ask;
/v1/forecast: which part of Open-Meteo to ask;
?latitude=50.45&longitude=30.52: the place, Kyiv, given as how far
north and east it is;
&daily=temperature_2m_max: the value wanted, each day's highest
temperature;
&forecast_days=2: how many days, today and tomorrow;
&timezone=auto: whose clock counts the days, Kyiv's, not a world
one.
With timezone=auto, the days are counted by the clock of the place you
gave. Without it, Open-Meteo counts them by one clock for the whole
world, and a day may start or end at the wrong hour for you.
Everything after the ? is the query: the question itself, as
pairs of a name and a value, name=value, joined by &. The names
are Open-Meteo's own, listed in its documentation;
temperature_2m_max is the highest temperature of the day, measured
two metres above the ground.
Sending the address is a request, and what comes back, the API's answer,
is a response. A response begins with a status code, three digits that
say how the request went, and then brings its body: the data itself, here
JSON text. These words and codes are part of HTTP, short for Hypertext
Transfer Protocol: the rules browsers and programs follow to talk to
websites, which is why the address starts with https: HTTP with an S for
secure, so nobody between you and the website can read the conversation. The
three codes you meet in this lesson:
Status code
What it means
200 OK
Here is what you asked for
400 Bad Request
The query is wrong, such as a latitude of 500
404 Not Found
This computer has no such address
fetch: ask, then read
fetch is a function built into Node. It sends a request to a
URL and gives back a promise of the response, a Promise<Response>.
A Response is an object holding what has arrived so far:
status, the code, and ok. ok is true for every code from 200
to 299, because those all mean the request worked. The body is still on its
way, like the book while the librarian goes to get it.
To read the body, call response.json(). It waits for the rest of
the text and builds a value from it, as JSON.parse does. That is a
second wait, so it gives back a promise too, and needs its own
await. Press Next step to walk through both waits, or let it
play:
Yourcode
Node
Open-Meteo
1
Your code to Node: fetch(url)
2
Node to Open-Meteo: GET/v1/forecast?…
3
Open-Meteo to Node: 200OK
4
Node to Your code: response
5
Your code to Node: response.json()
6
Open-Meteo to Node: {"daily":{…}}
7
Node to Your code: forecast
The status comes back first; the JSON text is read with a
second await
All 7 steps shown.Step 1 of 7: Your code → Node — fetch(url)Step 2 of 7: Node → Open-Meteo — GET /v1/forecast?…Step 3 of 7: Open-Meteo → Node — 200 OKStep 4 of 7: Node → Your code — responseStep 5 of 7: Your code → Node — response.json()Step 6 of 7: Open-Meteo → Node — {"daily":{…}}Step 7 of 7: Node → Your code — forecast
All 7 steps shown.
GET in the second arrow is how a request says "send me this". The
page cannot reach a web API, so this program runs in your terminal.
In ~/projects/taskkit, make weather.ts:
Your dates are today and tomorrow, and the temperatures are the
forecast for them, so your numbers will differ.
Open-Meteo's answer has an object inside an object, {"daily":{…}},
so its type, on lines 1 to 8, has two parts: Daily describes the
inner object, and Forecast says that its daily property holds
one. The names are written exactly as Open-Meteo writes them. Each
array holds one item per day, in order, so [0] is today and [1]
is tomorrow. The address is too long for one line of code, so lines
10 to 13 glue three strings into one with +. Line 15 waits for the
response, and line 16 prints its status. Line 17 waits for the body
and keeps the value in forecast.
response.json() gives back Promise<any>: like JSON.parse in
lesson 12.1, it builds the value from text TypeScript never sees. So
you write the type on the box, forecast: Forecast, and the check
keeps working. Only the properties you read need to be in the type;
the answer holds more, such as latitude, and TypeScript just does
not know about them.
A bad answer is still an answer
Change one number in the address: in weather.ts, write
latitude=500 instead of latitude=50.45. Open-Meteo still answers.
The status is 400, ok is false, and the body is JSON of another
shape:
Open-Meteo's answer, laid outjson
{ "error": true, "reason": "Latitude must be in range of -90 to 90°. Given: 500.0."}
Open-Meteo may send the two keys in either order. fetch does not
mind: it asked, an answer came, and its promise is
fulfilled. Only your program knows that a 400 is not the answer it
wanted, so it has to look at ok itself. Both versions below sit in
a try / catch that prints the error's message, as in lesson 11.5.
The interfaces and url, now with latitude=500, stay as they are;
the code below replaces lines 15 to 19 of weather.ts.
Bad — no look at ok, so the error's
body is read as a forecast.
No forecast: Cannot read properties of undefined (reading'temperature_2m_max')
Good — if (!response.ok) throws
before the body is read, and the message names the status. These
lines replace the Bad ones.
weather.tsts
try { const response = await fetch(url); if (!response.ok) { throw new Error(`HTTP ${response.status}`); } const forecast: Forecast = await response.json(); const high = forecast.daily.temperature_2m_max[1]; console.log(`Tomorrow: ${high} °C`);} catch (error) { if (error instanceof Error) { console.log(`No forecast: ${error.message}`); }}
What it printstext
No forecast: HTTP 400
The 400 answer's body has no daily, so in the first version
forecast.daily is undefined and reading a property of it throws.
You learn that something is undefined, not that the address was
wrong; the status tells you where to look.
There is a third case: no answer at all. Mistype the computer's name,
api.open-meteoo.com, or turn off your Wi-Fi, and nobody answers.
Only now is the promise of fetch rejected. await throws its
reason, a TypeError whose message is fetch failed, and the
catch prints No forecast: fetch failed. Here are the three runs
side by side. Switch between them, then press Next step to see
where each one stops, or let it play:
fetch(url): passes✓Did any answer come back?
response.ok: passes✓Is the status from 200 to 299?
.json(): reachedRead the JSON text into forecast
console.log: reachedPrint: Tomorrow: 14.9 °C
fetch stops only when no answer comes; a bad answer is
stopped by your own if
All 6 steps shown.Step 1 of 6: fetch(url)Step 2 of 6: fetch(url): passesStep 3 of 6: response.okStep 4 of 6: response.ok: passesStep 5 of 6: .json()Step 6 of 6: console.log
All 6 steps shown.
fetch(url): passes✓Did any answer come back?
response.ok: stops here✕Is the status from 200 to 299?
.json(): never reachedRead the JSON text into forecast
console.log: never reachedPrint: Tomorrow: 14.9 °C
response.ok stops hereNo forecast: HTTP 400
fetch stops only when no answer comes; a bad answer is
stopped by your own if
All 4 steps shown.Step 1 of 4: fetch(url)Step 2 of 4: fetch(url): passesStep 3 of 4: response.okStep 4 of 4: response.ok: stops here. No forecast: HTTP 400
All 4 steps shown.
fetch(url): stops here✕Did any answer come back?
response.ok: never reachedIs the status from 200 to 299?
.json(): never reachedRead the JSON text into forecast
console.log: never reachedPrint: Tomorrow: 14.9 °C
fetch(url) stops hereNo forecast: fetch failed
fetch stops only when no answer comes; a bad answer is
stopped by your own if
All 2 steps shown.Step 1 of 2: fetch(url)Step 2 of 2: fetch(url): stops here. No forecast: fetch failed
All 2 steps shown.
Put the address back as it was, with latitude=50.45 and the
computer's name spelled right. Your weather.ts is now the whole
program: the two interfaces, url, and the Good version under them.
Run it again:
bash
node weather.ts
Outputtext
Tomorrow: 14.9 °C
Your number is tomorrow's forecast, so it will differ.
Predict before you run
This time the typo is in the second part of the address: forcast,
with one letter missing. Decide what the program prints before you run it.
This is lesson 11.3's mistake, a promise used as its value, at the
place where it is easiest to make. Once await fetch(url) gives you
the response, it feels as if the data is there. It is not yet: the
body needs its own await, and that second wait is easy to forget.
Common mistake
Reading the body without await
You write the program from memory, and line 20 loses its await:
npm run check (npm, the Node package manager, runs TaskKit's
check script, which is tsc) prints its two > lines, and then
these; TS2741 is TypeScript's (TS) number for the error. The first
message is wrapped here to fit:
What the check printstext
weather.ts:20:9 - error TS2741: Property 'daily' is missing in type'Promise<any>' but required in type 'Forecast'.20 const forecast: Forecast = response.json(); ~~~~~~~~ weather.ts:7:3 - 'daily' is declared here. 7 daily: Daily; ~~~~~Found 1 error in weather.ts:20
Without await, response.json() is the promise, not what it will
hold. A promise has no daily, and the check says so before Node
runs anything.
Property 'daily' is missing in type 'Promise<any>' but required in type 'Forecast'.
The fix
Wait for the body as you wait for the response:
weather.tsts
const forecast: Forecast = await response.json();
When the check says a Promise<…> is missing a property, look for
the await that is not there.
Pass all four tasks to complete the lesson with practice. Reach the end without them and it counts as read only.
Exercises
Each one is checked in your browser. The page cannot reach a web API,
so one asks about the terminal, one builds an address, and two work
with an answer that has already arrived.
Exercise · Read the code
No network
You turn off your Wi-Fi and run the Good weather.ts. Which line
throws?
Exercise · Fix the error
The ok test the wrong way round
The page cannot call fetch, so here response is a plain object
with the two properties your if reads, as a 404 would bring them.
The program should print No forecast: HTTP 404, but it prints
Reading the forecast. Fix it.
Loading the editor…
Exercise · Write the code
Ask about Lviv
Build the address that asks for the same forecast in Lviv, from
latitude and longitude. Keep every other part of the address as
weather.ts has it.
Loading the editor…
Exercise · Write the code
Describe the answer
text is the part of the body that Open-Meteo sent on 2026-10-23 and the
program reads, and the lines under it read tomorrow from it, as they would
from await response.json(). They cannot run yet: Forecast is not written.
Write the two interfaces this answer needs, with Open-Meteo's names, so the
program prints 2026-10-24: 14.9 °C.
Loading the editor…
Recap
Recap · 3 questions
Check yourself
What does await fetch(url) give you?
Check yourself
In a URL, what is the part after the ? called?
Check yourself
When is response.ok true?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
fetch: getting data from a web API
What things are
Word
What it means
Web API
A website that answers programs with data, often JSON
URL
The address a request goes to
Query
The part after ?: name=value pairs joined by &
Request
What your program sends: here, an address
Response
What comes back: a status code, then a body
Status code
Three digits: 200 OK, 400 Bad Request, 404 Not Found
Body
The data a response brings, here JSON text
HTTP
The rules for asking websites; https addresses use them
Code
Write
To
await fetch(url)
Send a request; get a Response
response.status
Read the status code, such as 200
response.ok
Ask whether the status is from 200 to 299
await response.json()
Read the body into a value of type any
const forecast: Forecast
Keep checking what the body gives
if (!response.ok)
Then throw: stop on a bad answer, name its status
Rules
Rule
What it means
Two waits
One for the response, one for its body
A bad answer is an answer
fetch gives a Response for 400 and 404 too
No answer rejects
await fetch throws TypeError: fetch failed
No await on .json()
You hold a promise: TS2741, a property missing
Where to go next
You can now ask a web API for data with await fetch(url), check the
answer with response.ok before you read it, read its JSON with
await response.json(), and give that value a type. You know which
mistakes still bring an answer, and which one brings none.
Next is Why an API response type doesn't guarantee what the data
really looks like, where the answer and your Forecast disagree,
and the check, which trusted your type, cannot tell you.
Before you move on, in weather.ts change [1] to [2] and leave
forecast_days=2 as it is. Predict what it prints, then run it to
see. Then put [1] back, and keep weather.ts: the next lesson
starts from it, and deletes it once Module 12 is done.