Skip to content

JavaScript fetch API: Load JSON from Public APIs, Check Errors and Show Data

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

Most useful pages show data they do not hold themselves: today’s exchange rates, the weather in Dubai, the status of a delivery. They get it from an API, a web address that answers with data instead of a page. In the browser you ask for that data with fetch, which returns a promise, so everything from the last lesson about async, await, try and catch applies directly. This lesson loads real exchange rates from a free public API, checks every way the request can go wrong, and shows the result safely on the page. It is exactly how the currency converter on this site works.

Quick answer

Call const response = await fetch(url) inside an async function with try and catch. fetch only rejects when the network fails, so check response.ok yourself and throw when it is false, using response.status (such as 404 or 500) in the message. Read the body with const data = await response.json(). Build query strings with new URLSearchParams() or the URL object, add a time limit with AbortSignal.timeout(8000), and write values to the page with textContent. Never put a secret API key in browser code; call those APIs from your own server.

35 min read and practice · Content checked on 11 November 2026

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.

Try it: Live US dollar rates from a public API
Code
ResultUpdates as you type
Console
  1. 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
Sign in or create a free account

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.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.