Welcome to Testing, Documenting and Building a Node API! You can already build an API that answers requests. In this course, you’ll prove an API works with tests, describe it for the people who call it, and then build one from a brief, on your own.
Here’s a first taste, from Riverside FM 104.6, a volunteer-run community radio station. Gloria Mendes hosts its Sunday request show. By the end of the course, her request line answers a song request from Luis, a listener, with 201 Created. The request line is a separate API with its own list of her shows.
Luis sent his request through the station’s web form, so his form id identifies him. The request’s body is wrapped for display, and the rest of the response is cut short:
$ curl -i -X POST http://localhost:3000/shows/1/requests \
-H "Content-Type: application/json" \
-d '{"name": "Luis Ortega", "formId": "form-2041",
"song": "Three Little Birds", "artist": "Bob Marley & The Wailers"}'
HTTP/1.1 201 Created
...
"played": false
...
And 25 tests check it, each one through real HTTP requests:
$ npm test
...
ℹ tests 25
ℹ pass 25
ℹ fail 0
...
Where You Start
Right now, the station has one API, written by Niamh Gallagher, its volunteer web person. It’s an Express 5 app in TypeScript that serves the schedule of six shows, from Morning Commute to Gloria’s The Sunday Request Show, stored in shows.json.
It works, but nothing checks that it keeps working, so nothing tells Niamh when a change breaks it. Run its tests, and Node’s test runner finds none:
$ npm test
> [email protected] test
> node --test
ℹ tests 0
ℹ suites 0
ℹ pass 0
ℹ fail 0
...
Its only docs are a short old page, docs/old-api.md, which lists each path and gives an example for some. It says nothing about error responses or request bodies. Anyone calling the API has to guess what a failure looks like. And Gloria’s request line doesn’t exist yet. Listeners text her their requests or send them through the station’s web form, and she copies them onto paper.
What You’ll Learn
By the end of this course, you’ll be able to:
- Test an Express API through real HTTP requests
- Document every endpoint
- Build a complete REST API from a brief, without a walkthrough
The course has three sections, one for each:
- Testing an API Through HTTP: In Test the Schedule API Through HTTP, you give Niamh’s API a test that starts its own copy of the API, sends it a real request with
fetch(), and checks the answer. Watch Two Tests Share One File shows why each test needs its own data, and Test the Error Paths Too checks the failures. Exercise: Give Every Test Its Own Server closes the section. - Documenting Every Endpoint: In Write an Endpoint Reference, you replace Niamh’s old page with a reference that gives each endpoint’s method, path, body and every response, errors included. When Niamh changes her server, Check the Docs Against the Server and Run the Docs’ Examples as Tests keep the two in step. Exercise: Make the Docs Match the Server closes the section.
-
Building an API From a Brief: Build One Slice End to End starts Gloria’s request line, and Follow One Request Through the API traces a call through its layers.
Then Project: Riverside FM’s Request Line gives you Gloria’s brief. It names the resources, which are shows, requests and a do-not-play list of songs the station won’t air. It sets the rules, such as one request per listener per show, and lists the checks as HTTP calls with the answers they must get, but it doesn’t give you the steps.
Two graded exercises, Exercise: Take Song Requests and Exercise: Enforce the Request Rules, check your build. In Explain Your Request Line, you explain your code to the RealJS mentor, an AI tutor that checks your understanding. You also make a change you haven’t seen. Finally, in Build an API of Your Own, you pitch an API from your own life and build it with the same method.
A quiz and a short summary video close the course.
Before You Start
This is the last course on the Node Servers and REST APIs path, and starting here is fine too. You don’t need to have taken the path’s other courses, as long as you already know how to:
- Build an Express 5 API in TypeScript, with routes, routers, route parameters and query strings, storing its data in a JSON file
- Validate requests with Zod and send error responses in one consistent shape from an error handler
- Read settings from environment variables, allow a browser front end on another origin with CORS, and log each request
- Call an API with
curl -iandfetch(), and write a test with Node’s test runner, which the first lesson recaps in a line
You’ll need a current LTS release of Node.js, a terminal and any editor. The projects bring their own packages, which npm install sets up.
How the Course Works
The course mixes text lessons, short videos and four coding exercises, then a quiz. The coding exercises run in your browser and check your code with automated tests. Type along with the text lessons on your own machine, with two terminals open: one for the running API, and one for curl and the tests.
Below this video are the slides and a code download. The code download has three folders:
- Start here,
riverside-schedule: Niamh’s schedule API with its old docs, which you work on in the first two sections - Head start, the request line (optional): the request line’s first slice with Gloria’s starting data, for the capstone if you’d rather not start from your own slice
- Finished code: the reference solution of the whole request line, with its tests and its reference
Next up is Test the Schedule API Through HTTP, where Niamh’s API gets its first test.