Programming foundations

Stage 6 · Lesson 4 of 8

Calling a public API with fetch

Build and ship your first projects

Current versions of Node.js have built in, the same API as in the browser. const res = await fetch(url) sends a . Like other , it resolves (finishes) as soon as a response arrives, even for a 404 or 500 error. fetch only rejects on network failures, so check res.ok or res.status before calling await res.json().

401 means a bad key and 429 means too many requests. Build with URLSearchParams rather than gluing strings together. Pass signal: AbortSignal.timeout(10000) so a slow server cannot hang your program.

Read the documentation for , and cache responses while developing. Wrap the call in one function that returns plain data or throws a clear error.

A question to check yourself, an AI practice session and 3 cheat sheet entries come when you add the roadmap to your account. Add it

Resources for this stage

Start with· follow one of these

  • GitHub repoFree
    Project Based Learning

    Community maintained · Beginner

    Follow one guided build in your language, then build your own without a guide

  • BookFree
    Eloquent JavaScript

    Marijn Haverbeke · Intermediate

    Free: the project chapters (a robot, a programming language, a pixel editor) show real structure

Keep handy

  • Free APIs that give a small project real data to work with

Also worth your time

Mark this stage as not done?