Toolpuffin

    Loading...

    Up and down to choose, Enter to open, Esc to close.

    How to Learn to Use an API with fetch: A Simple Plan

    Updated December 2026

    Written by the Toolpuffin team. Tools in this guide are tested in the browser.

    A web API is an address that sends back data instead of a page. In JavaScript you call it with fetch(). This guide gives a simple order to learn that, shows the one mistake that surprises almost every beginner (fetch does not fail on a 404), and explains status codes and POST requests. The facts come from MDN and web.dev. The free course practises writing the code, but it does not run it.

    Try the tool: Learn to Use APIs

    What an API call is

    MDN describes the Fetch API as a JavaScript interface for making HTTP requests and processing the responses. You give fetch() an address, the server sends a response, and you read the data from that response. Most APIs send their data as JSON, so it helps to know that format first.

    A simple order to learn it

    1. Put the address in a variable, so there is one place to change it.
    2. Call fetch(url). It returns a promise, a value that will arrive later.
    3. Wait for the response with .then() or with async and await.
    4. Read the body with response.json(), which also returns a promise.
    5. Check that the request worked before you use the data (see the next section).
    6. Use try and catch to handle a failed request without breaking the page.

    Status codes and the fetch surprise

    Every HTTP response has a three-digit status code. MDN groups them in five classes: 100 to 199 are informational, 200 to 299 are successful, 300 to 399 are redirects, 400 to 499 are client errors (such as 404, not found) and 500 to 599 are server errors.

    Here is the surprise: MDN explains that the promise from fetch() rejects on some errors, such as a network failure, but not when the server answers with an error status like 404. The promise then resolves normally. web.dev says the same. So you check the response yourself: the property response.ok is true when the status is in the range 200 to 299.

    A common pattern is to throw an error when response.ok is false, so that your catch block handles it in the same way as a network failure.

    Sending data with POST

    To send data, MDN shows how to pass a second argument to fetch() with the method (for example POST), the headers (for example a content-type of application/json) and the body. When you send JSON, the body is text, so you turn your value into text with JSON.stringify().

    Practise, and test with a real API

    The free course has eight short lessons: an address in a constant, fetch, then and json, async and await, query parameters, response.ok, a POST request and try and catch. The course checks that your code contains what the task asks for, with simple rules. It does not run your code and makes no real request, so also try your code in your own browser or editor to see real responses and real error messages.

    Sources

    More guides