Skip to content

REST API design: an API others can use

In Phase 3 you wrote an APIAPI stands for Application Programming Interface.The way a program offers its services to other programs, that is, what you can ask it for and how it answers. The frontend talks to the backend through an API.Go to definition that works. It had a single ClientThe program that starts the conversation by asking another program, the server, for something. Your browser is a client.Go to definition, the shop’s website, and if something wasn’t clear, the person using it could ask you. In this phase, other people use it: people you don’t know, who can’t ask you and who can’t break every time you change something.

You don’t switch projects. You keep working on the same shop API, and each lesson adds something it needs once it leaves home.

What changes when other people use your API

Section titled “What changes when other people use your API”

Ana and Luis’s shop is doing well, and two new clients come along. Imagine:

  • A phone app, made by an outside studio. It shows the products, creates orders and lets you know when one is on its way.
  • A delivery company, which every morning asks for the orders ready to be picked up and wants to hear about new ones without asking all the time.

Neither of them has seen your code. They only see your responses. That changes many things that didn’t matter until now:

When only your website used it When other people use it
If you rename a field, you change the website that same day The app people have installed doesn’t update itself: a renamed field breaks it for months
If something fails, you look at your LogA text file where a program writes down what happens, one line for each thing. A web server’s has a line for each request it receives, and it’s the first thing you look at when something fails.Go to definition The delivery company only sees your response, and has to understand the error from it
You know by heart what each address returns Nobody can ask you: what isn’t written down doesn’t exist for them
Three products and a few orders fit in one response Thousands of orders don’t fit: they have to come in parts
If you want to know whether there’s a new order, you look The delivery company wants you to tell it, not to ask you every minute

Each row is a lesson in this phase. Your API will learn a few new things, but what matters is something else: that other people can use it without you.

A good API can be used without asking the people who made it. For that it needs three things:

  • Predictable. If GET /orders/7 returns an order, someone who doesn’t know you guesses that GET /products/2 returns a product, and that an error will have the same shape in both. The addresses, the HTTP methodHTTP stands for HyperText Transfer Protocol.The word every HTTP request starts with, which says what the client wants to do with what it asks for. GET is reading, POST sending data, PUT replacing, PATCH changing a part and DELETE deleting.Go to definition and the Status codeA three-digit number at the start of every HTTP response that sums up what happened. 200 means “all good,” 404 “that doesn’t exist” and 400 “your request is malformed.”Go to definition follow rules that many people already know. That’s what the REST, errors and pagination lessons are about.
  • Documented. What each address accepts and returns is written down, and not in a separate document nobody updates, but in a contract that comes from the same code that checks the data. That’s the OpenAPI lesson.
  • Stable. It changes without breaking anyone: new things are added, and what can’t be added goes in a new version, with a farewell date for the old one. That’s the versioning lesson.

All three will sound familiar from any app: a good interface is understood without a manual and doesn’t move the buttons around every week. Here, the interface is your URLs, your codes and your JSONJSON stands for JavaScript Object Notation.A format for writing data as text, with braces, quotes and colons: {"product":"mug","quantity":2}. People can read it, and so can almost any programming language, and it’s the most common format for the data that travels between a frontend and a backend.Go to definition, and whoever uses it is another program.

An example of what changes. If you ask the Phase 3 API for an order of zero mugs, it answers in a format you made up:

HTTP/1.1 422 Unprocessable Entity
content-type: application/json
…
{"error":"The data is not valid","details":[{"field":"quantity","problem":"At least 1"}]}

By the end of this phase, it answers like this (we captured it with the final version of the project and Node 26, and trimmed some headers with …):

HTTP/1.1 422 Unprocessable Entity
content-type: application/problem+json
…
{"type":"https://shop.example/problems/invalid-data","title":"The data is not valid","status":422,"detail":"See \"errors\" for what is wrong with each field.","errors":[{"field":"quantity","problem":"At least 1"}]}

It says the same thing, but in a format that isn’t yours: a standard’s, Problem Details, written in an RFCRFC stands for Request for Comments.The public documents where the exact rules of internet protocols are written. HTTP/1.1, for example, is defined in RFCs 9110 and 9112.Go to definition. The delivery company already knows it from other APIs, and its program can read it without you explaining anything. Look at the content-type HeaderInformation a protocol puts in front of its data to do its job. In HTTP they are text lines of the form “Name: value”, such as Host or Content-Type; in TCP or IP they are numbers in fixed positions, such as ports or addresses.Go to definition too: it announces that format before anyone reads the body.

The list of orders, on the other hand, no longer comes whole. It comes in pages, and each one says where the next one is:

HTTP/1.1 200 OK
content-type: application/json
link: <http://localhost:3000/v2/orders?limit=2&cursor=MjQ>; rel="next"
…
[{"id":25,"productId":2,"quantity":2,"status":"pending","createdAt":"2026-10-08T09:00:00.000Z"},{"id":24,"productId":1,"quantity":1,"status":"pending","createdAt":"2026-10-08T02:00:00.000Z"}]

Lesson by lesson, the API gains this:

Lesson What the shop’s API gains
1. REST Changing an order (PATCH), cancelling it and removing a product (DELETE), each with its own method and its own code
2. Errors Every error with the same shape, the one from the Problem Details standard
3. Pagination Orders in pages, filtered by status, and products sorted by price
4. Versioning /v1/ and /v2/ at the same time: the price moves to cents without breaking whoever read it in euros
5. OpenAPI A contract, openapi.json, and a documentation page at /docs
6. Serialization Each order’s date, written properly, and the same order in JSON and in a binary format, Protobuf
7. Webhooks An address where the payment gateway reports each payment, with a signature, and orders that aren’t duplicated if someone repeats the request
8. Real time An order’s status, live, without asking every minute
9. Alternatives Nothing new in the project: you compare REST with GraphQL and gRPC

The data is still kept in the program’s memory. Storing it in a database is Phase 5.

By the end of this phase you’ll know how to design an API that other people use without asking you. You’ll know:

  • How to name an API’s addresses around its resources, so they can be guessed, and what REST really means.
  • How to answer errors always the same way, with a standard format.
  • How to hand out a long list in pages without skipping or repeating anything, even if new data arrives in the meantime.
  • How to change an API without breaking its clients, and what counts as breaking.
  • How to document it with OpenAPI, as a contract that both people and programs read.
  • The traps of JSON: dates, money, very large numbers and null.
  • How to let others know when something happens, with webhooks, and how to send data live with Server-Sent Events and WebSockets.
  • When GraphQL or gRPC is worth it, and why REST is still the norm for a public API.
  1. REST
  2. Errors
  3. Pagination
  4. Versioning
  5. OpenAPI
  6. Serialization
  7. Webhooks
  8. Real time
  9. Alternatives

Everything happens on your computer, as in Phase 3. There are three places:

Where What for From
Your project, shop-api Each lesson adds something to it, and you try it with curl on http://localhost:3000 Lesson 1
GitHub’s API, with curl A well-designed public API you can read without an account: how it names its addresses, how it hands out lists in pages and what limits it sets This introduction
The browser console Listening to your server live, without reloading the page Lesson 8

The console is the DevToolsThe developer tools that come with browsers such as Chrome or Firefox, opened with F12. Their Network tab shows every request the page makes, with its response and headers; the Console, the page’s errors; and Application (Storage, in Firefox), what the site stores in your browser, such as cookies.Go to definition tab where you type code for the browser to run; you met it in the Phase 2 introduction. Lesson 9 also uses another public API, a GraphQL one, also without an account.

  • On Windows, everything happens in WSL, as it has since Phase 0.
  • The practice server keeps resting: in Phase 8 you’ll put this very API on it.

Your project’s outputs were captured with Node 26, and GitHub’s on October 13, 2026. Your times, your dates and some numbers will be different.

You stay in the same folder as in Phase 3, ~/backend-course/phase-3/shop-api. The folder’s name says where the project was born, not which phase you’re in: moving it gains you nothing, and this way your paths stay the same as always.

Before lesson 1, check that your API starts: npm start inside the folder, as in lesson 8 of Phase 3. If it starts and curl localhost:3000/products gives you the list, you’re ready.

If you didn’t do Phase 3, or your project was left half-finished, start with the code from the end of that phase: the folder of its last lesson, 09-event-loop. First, set yours aside by renaming it (if you don’t have one, skip that line; mv would say No such file or directory):

Download the end of Phase 3

Ventana de terminal
cd ~/backend-course/phase-3
curl -O https://backenddesdecero.com/practicas/shop-api/09-event-loop.tar.gz
tar -xzf 09-event-loop.tar.gz
mv shop-api shop-api-before
mv 09-event-loop shop-api
ls

What you will see:

% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 11061 100 11061 0 0 68766 0 --:--:-- --:--:-- --:--:-- 69131
09-event-loop.tar.gz
shop-api
shop-api-before

They’re the commands from the Phase 3 introduction: curl -O downloads the packed folder and tar -xzf unpacks it, without printing anything. What’s new is mv, which here doesn’t move anything but renames it (lesson 2 of Phase 1): your project is now called shop-api-before, and the downloaded folder takes its place.

ls confirms it: three names. The size of the download may change when we update the code, and your speeds will be different.

Set it up

Ventana de terminal
cd shop-api
npm install

What you will see:

added 21 packages, and audited 22 packages in 2s
2 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities

npm install downloads the packages the project uses: Hono, Zod, pino and their dependencies. Your time will be different.

The .env is missing, because it holds secrets and never travels with the code. If you had one, copy it from your old folder with cp ../shop-api-before/.env .; if not, create it as in lesson 8 of Phase 3: cp .env.example .env, and a real ADMIN_TOKEN with openssl rand -hex 24.

Each lesson in this phase also has its folder, with the project as it is at the end of the lesson: from 10-rest to 17-realtime. You download them the same way, from ~/backend-course/phase-3, and they’re for comparing when you get stuck, not for replacing your project. Lesson 9 doesn’t have one: it doesn’t change the code.

The goal: without opening GitHub’s documentation, find the open issues of the Node.js repository in its API. An issue is a report someone opens on a repository: a bug, a proposal or a question. If the API is well designed, you should be able to guess the addresses.

GitHub’s API lives at https://api.github.com, and without an account it allows 60 requests per hour from each IP address. This exercise uses five.

Start with octocat, the account of GitHub’s mascot, which exists for examples:

The octocat user

Ventana de terminal
curl -s https://api.github.com/users/octocat | grep -E '"(login|url|repos_url|name|public_repos)"'

What you will see:

"login": "octocat",
"url": "https://api.github.com/users/octocat",
"repos_url": "https://api.github.com/users/octocat/repos",
"name": "The Octocat",
"public_repos": 8,

The response has more than thirty lines of JSON. grep -E keeps the five we care about: the ones with one of those names in quotes (lesson 2 of Phase 1). If nothing comes out, remove the | grep … and look at the whole response.

Look at the address: /users is all the accounts, and /users/octocat, one of them. First the collection, then which one. And the response gives you the next address without you looking for it: repos_url, that account’s repositories.

On the website, Node.js is at github.com/nodejs/node: the account nodejs and the repository node.

Challenge: from what you’ve seen, which API address do you think gives you the Node.js repository? Try it with curl before you look.Show answer

Accounts are under /users/…; repositories, under /repos/…, with the account and the name, as on the website:

Ventana de terminal
curl -s https://api.github.com/repos/nodejs/node | grep -E '^ "(full_name|url|issues_url|open_issues_count)"'
"full_name": "nodejs/node",
"url": "https://api.github.com/repos/nodejs/node",
"issues_url": "https://api.github.com/repos/nodejs/node/issues{/number}",
"open_issues_count": 1197,

The ^ in the grep asks for the name to start the line with two spaces, to keep the repository’s data and not its owner’s, which is nested further in. issues_url gives you the clue: {/number} is an optional slot. Without it, the list of issues; with a number, just one. Your open_issues_count will be different: it changes every hour.

With the clue from issues_url, the list is at /repos/nodejs/node/issues. There are more than a thousand, so ask for just two:

The two latest issues

Ventana de terminal
curl -s "https://api.github.com/repos/nodejs/node/issues?per_page=2" | grep -E '^ "(number|title|state)"'

What you will see:

"number": 66705,
"title": "test: deflake test-net-autoselectfamily-local-port",
"state": "open",
"number": 66704,
"title": "tools: update OpenSSL on the selected branch",
"state": "open",

?per_page=2 is a Query stringThe part of a URL that comes after “?”: data in name=value pairs separated by “&”, such as q=mug&sort=price. It travels to the server attached to the path, and it’s used to search, filter or choose a page of results.Go to definition (lesson 1 of Phase 2): “two per page.” The quotes around the address are needed because of the ?, which the shell could take for something else. Your numbers and titles will be different: you get the most recent ones.

Goal achieved, without documentation: the addresses can be guessed because they follow a rule.

The two issues are the first page. Where’s the second one? Not in the body, but in the headers:

The list's headers

Ventana de terminal
curl -sI "https://api.github.com/repos/nodejs/node/issues?per_page=2" | grep -iE '^(HTTP|link|etag|x-ratelimit-(limit|remaining))'

What you will see:

HTTP/2 200
etag: W/"497c2c4cfa9a2028b33819cd12088d3c4d9b7be36b32686dc1ee8eed07535c10"
link: <https://api.github.com/repositories/27193779/issues?per_page=2&after=Y3Vyc29yOnYyOpLPAAABoTU4rEjPAAAAAVuCfJw%3D&page=2>; rel="next"
x-ratelimit-limit: 60
x-ratelimit-remaining: 50

curl -sI sends a HEAD, which asks for the headers only (lesson 2 of Phase 2), and grep -iE keeps four of them, ignoring upper and lower case. Line by line:

  • link is the address of the next page, with rel="next". Look at after=…: it’s a bookmark that says “carry on after this one,” and you’ll see it in the pagination lesson. The 27193779 is the Node repository’s number: another way of naming it.
  • etag is this response’s fingerprint, the one from the Phase 2 caching lesson: with it, the client asks whether something has changed without downloading it again.
  • x-ratelimit-limit and x-ratelimit-remaining are your limit per hour and what you have left. Each request in this exercise has taken one away; your number will be different. If you reach zero, GitHub answers with an error until the hour is up. Setting limits like this is Phase 6.

A browser didn’t need any of these headers. They’re there for the program that uses the API: how to carry on, when to ask again and how much it has left. An API designed for others also answers what its client is going to ask next.

What if you guess wrong? Try asking for the repositories in Spanish:

A made-up address

Ventana de terminal
curl -s -w '\n' https://api.github.com/users/octocat/repositorios

What you will see:

{
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest",
"status": "404"
}

A 404, with the same shape as the 401 you saw in the Phase 2 headers lesson: message, documentation_url and status. GitHub answers all its errors the same way, and the program that uses it reads them all with the same code. That’s what your API will do from lesson 2 on. -w '\n' adds a line break at the end, so the prompt doesn’t come out stuck to the response.

In this phase, your API will learn to do all of this: addresses that can be guessed, lists with their next page and errors that look alike. Start with lesson 1, “REST”: you’ll see where the rules you just used without reading them come from, and you’ll redesign the shop’s routes with them.

phase-4 · introduction