Write an Endpoint Reference

Your tests now check every answer Niamh’s schedule API gives, including the ones that say no. They tell Niamh when the server changes. They don’t tell anyone else how to call it. Joon-ho Park, a volunteer who’s building the station’s new website, wants the site’s schedule page to add and edit shows through the API, and they’ve never seen app.ts.

In this lesson, you’ll write the reference Joon-ho needs: every endpoint’s method, path, parameters, and body, and every response it can give, each with an example you took from a real curl -i run. Start where Joon-ho starts, with the only docs the project has.

Read the Old Docs as a Client

A client developer reads the docs to write two things: the request and the code that handles each answer. The project has a docs/ folder with Niamh’s old README section, docs/old-api.md. Open it:

Language: Markdown Text Filename: docs/old-api.md
# Schedule API

Start it with `npm start`. It runs on port 3000.

## GET /shows

All the shows.

```console

$ curl http://localhost:3000/shows
[{"id":"1","title":"Morning Commute","host":"Kwame Asante",...},...]

```

## GET /shows/:id

One show.
...

## POST /shows

Adds a show. You get the new show back with its id:

```json

{
  "id": "e4968910-af5a-4bcb-bd96-d1d03c1417b8",
  "title": "Saturday Folk",
  "host": "Freya Lindqvist",
  "day": "Saturday",
  "start": "14:00",
  "minutes": 90
}

```

## PATCH /shows/:id

Changes a show. You get the changed show back.

## DELETE /shows/:id

Deletes a show.

Every path is there, and two have an example of things going well. Now read the POST /shows entry as Joon-ho, who wants to add a show from a form.

Which fields must the body have? Is "2pm" a valid start time? Does the request need a Content-Type header? What comes back when a volunteer leaves the length empty, and what should the page show then? The docs don’t answer any of it.

Docs that show only the successful request leave the client to guess the errors. The statuses, the error format, and the limits that trigger them turn up in production, one confused volunteer at a time. Joon-ho’s only other option is reading app.ts and shows.ts, which docs should spare them.

The fix is an endpoint reference: a document that tells a client everything they need to call each endpoint without reading the code. For every endpoint, it gives the method and path, what the endpoint does, its path parameters, the body’s fields and their rules, and every response it can send, errors included, each with its status and an example. You’ll write one in docs/api.md, next to the old file.

Describe What Every Endpoint Shares

Some facts hold for all five endpoints: where the API lives, that bodies are JSON, and what an error looks like. Writing them under every endpoint makes five copies that can drift apart, so the reference says them once, at the top. Create docs/api.md and start it with the basics:

Language: Markdown Text Filename: docs/api.md
# Riverside FM Schedule API

The schedule API lists the station's shows and lets volunteers add, change and
remove them.

## Basics

- **Base URL:** `http://localhost:3000` after `npm start`. Every path below
  starts there.
- **JSON:** responses are JSON. `POST` and `PATCH` take a JSON body and must
  send the header `Content-Type: application/json`, or the server never reads
  the body and answers `422`. `GET` and `DELETE` take no body.
- **Ids** are strings in the path and in the body: `/shows/6`, `"id": "6"`.

The line about Content-Type comes from sending Joon-ho’s likeliest first request, a curl without the -H option. Before you run it, back up the six shows from your second terminal, because some of this lesson’s requests change the data. Start your dev server with npm start in the first terminal if it isn’t running. Then try a good show with no header. In this lesson’s terminal output, ... marks headers left out and long JSON bodies shortened:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.