In this lesson you will
- Explain what an API, a request, a response and a status code are
- Load JSON with fetch, async and await, and check response.ok
- Handle network errors, bad status codes and slow servers with clear messages
- Build safe URLs, send JSON with POST, and keep secret keys off the browser
Requests, responses and your first fetch
Every time a browser loads anything, it sends an HTTP request to a URL and gets a response back. The response has a status code, such as 200 for success, 404 for "not found" and 500 for a server error, a few headers, and a body. An API is simply a URL whose body is data, usually JSON, made for programs to read. The Frankfurter API, for example, publishes the euro reference rates of the European Central Bank: open api.frankfurter.dev/v1/latest?base=USD in a new tab and you get today’s rates as JSON, with no account and no key.
fetch(url) sends a GET request and returns a promise for a Response object. The headers arrive first, so the body has to be read separately, with another promise: await response.json() parses JSON into a normal object, and await response.text() gives plain text. That is why fetch code always has two awaits. The Response also tells you status, ok (true for any status from 200 to 299) and the final url after redirects.
Console
- Console messages, errors, form data and clicked links appear here.
These are real rates, so your numbers will differ from anyone else’s on another day. The console shows the status and the whole JSON object; the loop turns data.rates into list items with Object.entries.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
What is the difference between fetch and XMLHttpRequest?
XMLHttpRequest is the older way to make requests, built on events and callbacks. fetch does the same job with promises, so it works naturally with async and await. Use fetch in new code; you will meet the older one in legacy pages and some libraries.
Are there free public APIs I can practise with?
Yes. Frankfurter for exchange rates and the Norwegian Meteorological Institute’s weather API need no key and allow browser requests. Always read an API’s terms: some ask for a contact detail in requests, limit how often you may call them, or forbid commercial use.
Why are the UAE dirham and Saudi riyal missing from ECB exchange rates?
The ECB publishes reference rates for a fixed list of about 30 currencies, and Gulf currencies are not on it. Both are pegged to the US dollar, at 3.6725 dirhams and 3.75 riyals, so a converter can go through the dollar and multiply by the peg.
How do I send data to a server with fetch?
Pass an options object as the second argument with method: "POST", a headers object with "Content-Type": "application/json", and body: JSON.stringify(data). Then check response.ok and read the reply with await response.json(), just like a GET request.
Can fetch be used outside the browser?
Yes. Node.js has had a built-in fetch since version 18, stable since version 21, and Deno and Cloudflare Workers support it too, so the same code style works on servers. There it is not limited by CORS, which is one reason API calls with secret keys belong on the server.
Sources
Facts in this lesson were checked against these official pages on 11 November 2026.
- MDN: Making network requests with JavaScript
- MDN: Using the Fetch API
- MDN: Response.ok
- MDN: HTTP response status codes
- MDN: AbortSignal.timeout()
- MDN: URLSearchParams
- MDN: Cross-Origin Resource Sharing (CORS)
- Frankfurter: free exchange rates API (ECB data)
- ECB: Euro foreign exchange reference rates
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.