Skip to content

15 min read

What is a REST API: design and best practices

In one sentence: A REST API organizes everything into resources, things with their own URL like /orders/17, and uses HTTP's methods as verbs and its status codes as answers. That way, anyone who knows one part of your API can guess the rest without asking you.

What you will learn

  • Design an API's URLs as resources, with plural nouns, and know how far to nest them.
  • Choose the method and the status code for each action, including 201 with Location, 204 and 409.
  • Turn an action that has no method of its own, like cancelling an order, into a resource.
  • Explain what a stateless API is, and what REST meant in 2000 compared with today.

Before you start, read: phase-3/09-event-loop.md phase-2/02-methods.md

Ana and Luis’s shop is going to have a phone app, and someone else is building it. Their first question is: “How do I cancel an order?” Imagine the API had grown as needs came up, one route at a time:

GET /getProducts.json
POST /createOrder
GET /order?id=17
POST /orders/17/change
GET /cancelOrder?id=17

Each route follows its own rule, so none of them tells you what the next one will look like: you have to ask. And the cancel one is dangerous: it’s a GET, and a link preview in a chat would already cancel the order (Phase 2).

In this lesson you design the API so that someone who doesn’t know you understands it after seeing two routes. That’s what REST is for: the conventions almost every public API follows.

The analogy

Think of a warehouse. Each shelf has a sign, “Orders,” and each box, its number: “Orders / 17.” At the counter there are only four forms, the same ones for everything: “Show me,” “Store this new one,” “Change this” and “Remove this.” You pick one and write the label: “Show me: Orders / 17.”

With the labels and the four forms you know how to ask for anything, without a manual for each box. And since each form carries everything needed, any clerk can help you, even one who has never seen you before.

The labels are the resources’ URLs; the forms, HTTP’s methods; and being helped by anyone means the server doesn’t keep state.

Where the analogy breaks down

  • An API, besides the box, answers with a code that says what happened: created, doesn’t exist, can’t be done.
  • Some actions aren’t boxes, like “cancel order 17.” REST turns them into a new box: “the cancellation of order 17.”
  • The clerk doesn’t remember you, but the warehouse does keep the boxes: “stateless” is about the conversation, not about the data.

You’re still in your project, ~/backend-course/phase-3/shop-api. Since other people will now use it, the API loses what was only there for learning: the /error route from lesson 8, the slow routes from lesson 9 (with their node:crypto and node:util imports) and the examples folder. These files change:

  • Directorysrc/
    • app.ts changes
    • data.ts changes
    • schemas.ts changes
  • Directorytest/
    • errors.test.ts changes
    • rest.test.ts new
    • event-loop.test.ts deleted

Download this lesson’s folder to copy its tests and, if you get stuck, to compare:

Ventana de terminal
cd ~/backend-course/phase-3
curl -O https://backenddesdecero.com/practicas/shop-api/10-rest.tar.gz
tar -xzf 10-rest.tar.gz
rm -r shop-api/examples shop-api/test/event-loop.test.ts
cp -r 10-rest/test shop-api/

rm -r deletes the examples folder and the test of the slow routes, which no longer exist (Phase 1).

These are the shop’s URLs at the end of the lesson:

URL What it is
/products The collection: all the products
/products/2 One item in the collection: the mug
/orders All the orders
/orders/17 Order 17
/orders/17/cancellation The cancellation of order 17

They all name things, never actions. Each one is a Resource (in an API)API stands for Application Programming Interface.Each thing an API talks about that has its own URL, such as a product (/products/2) or the list of orders (/orders). It’s named with a noun, and the HTTP method says what you do with it.Go to definition: something the API talks about that has its own URL. What you get when you ask for it, {"id":2,"name":"Mug","price":8.5}, isn’t the mug, but a representation of it in 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.

Collections go in the plural, and an item is the collection plus its id: /orders/17, “number 17 of the orders.” Each combination of method and URL, like GET /orders or POST /orders, is an EndpointEach address of an API you can send requests to, together with its method. GET /orders and POST /orders are two endpoints on the same URL. It’s a server route seen from the outside, by whoever uses the API.Go to definition: a Route (on a server)Each combination of HTTP method and URL path that a server knows how to handle, such as GET /products, together with the function that responds to those requests. A request that matches no route gets a 404.Go to definition in your code, seen from the outside.

Nest one level at most. /orders/17/cancellation hangs from the order because it doesn’t exist without it. But order 17 lives at /orders/17, not at /customers/5/orders/17: each thing has a single address. Nest when the child’s id only makes sense inside the parent, as on GitHub, where each repository has its own issue number 1: /repos/nodejs/node/issues/1.

The URL says what you act on, and 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, what you do, as you saw in lesson 2 of Phase 2. You already had reading (GET) and creating (POST). The shop gets three new actions:

Endpoint What it does
DELETE /products/2 Delete a product (only Ana, with her token)
PATCH /orders/17 Change an order’s quantity
POST /orders/17/cancellation Cancel an order

Deleting goes in src/app.ts, after app.post('/products', …):

src/app.ts
// Delete: DELETE to the product. If it works, there's nothing to tell: 204, without a body.
app.delete('/products/:id', adminOnly, validate('param', idSchema), (c) => {
if (!deleteProduct(c.req.valid('param').id)) {
return c.json({ error: 'No such product' }, 404);
}
return c.body(null, 204);
});
  • app.delete(…) creates a route for DELETE. It first goes through adminOnly, the guard from lesson 6 of Phase 3, and through the id’s validation.
  • deleteProduct(…) is in data.ts and returns false if the product didn’t exist: then, 404.
  • c.body(null, 204) answers 204 No Content: done, and nothing to tell. null means “no body.”

Changing the quantity uses PATCH, which changes only part of the resource (PUT replaces, PATCH changes a part). It goes after app.post('/orders', …):

src/app.ts
// Change one part: PATCH to the order, with only what changes. If the order is already paid, the
// request is well written, but it clashes with the resource's state: 409 Conflict.
app.patch('/orders/:id', validate('param', idSchema), validate('json', orderChangeSchema), (c) => {
const order = findOrder(c.req.valid('param').id);
if (!order) return c.json({ error: 'No such order' }, 404);
if (order.status !== 'pending') {
return c.json({ error: `The order is ${order.status}: it can no longer be changed` }, 409);
}
order.quantity = c.req.valid('json').quantity;
return c.json(order);
});
  • Two validate in a row: first the id in the URL, then the body (lesson 7 of Phase 3). If either fails, 422, and the route doesn’t even run.
  • order.status !== 'pending': an order that is paid, shipped or cancelled can no longer be changed: 409 Conflict, with the status in the message.
  • order.quantity = … changes the stored order, and the response is the whole order, with 200: the client sees how it ended up without asking for it again.

The body is checked with a new schema, in src/schemas.ts, below the one for orders:

src/schemas.ts
// The only thing the client can change in an order is the quantity. Not the status: it changes with
// what happens (a payment, a shipment, a cancellation), not because someone writes it.
export const orderChangeSchema = z.object({
quantity: orderSchema.shape.quantity,
});

orderSchema.shape.quantity reuses the quantity rule (a whole number from 1 to 10). And the schema only has quantity: the client doesn’t write the status. It changes because of what happens (a payment, a shipment, a cancellation), and Zod drops any other field.

Add deleteProduct and orderChangeSchema to the imports in app.ts, next to the ones you already had.

A client that doesn’t know you decides what to do based on 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, so each action always answers the same way (lesson 3 of Phase 2):

Action If it works If not
Read (GET) 200 and the resource 404 if it doesn’t exist
Create (POST /orders) 201 and the new resource, with its URL in Location 422 if the data isn’t valid
Change (PATCH) 200 and the resource as it ended up 404, 422 or 409
Cancel (POST …/cancellation) 200 and the order 404 or 409
Delete (DELETE) 204, without a body 401 without a token, 404

Notice two pairs:

  • 422 versus 409. With {"quantity":0}, the data isn’t valid: 422. With {"quantity":3} on a cancelled order, the data is fine, but it clashes with the resource’s state: 409. After a 409, the same request may work if the state changes; after a 422, never.
  • 404 versus 405. A route that doesn’t exist is a 404; a method the route doesn’t have, a 405 with Allow (Phase 2). Their lists change:
src/app.ts
// A method the route doesn't have: 405, and in Allow, the ones it does have.
const notAllowed = { error: 'Method not allowed' };
app.all('/products', (c) => c.json(notAllowed, 405, { Allow: 'GET, POST' }));
app.all('/products/:id', (c) => c.json(notAllowed, 405, { Allow: 'GET, DELETE' }));
app.all('/orders', (c) => c.json(notAllowed, 405, { Allow: 'GET, POST' }));
app.all('/orders/:id', (c) => c.json(notAllowed, 405, { Allow: 'GET, PATCH' }));
app.all('/orders/:id/cancellation', (c) => c.json(notAllowed, 405, { Allow: 'POST' }));

notAllowed keeps the body in a VariableA name that holds a value to use later, such as price = 8.5. In JavaScript it’s created with const, if it won’t be swapped for another value, or with let, if it will.Go to definition so it isn’t repeated, and the lines still go after all the routes (lesson 5 of Phase 3).

An action that doesn’t fit: turn it into a resource

Section titled “An action that doesn’t fit: turn it into a resource”

Cancelling isn’t creating, reading, changing or deleting. The temptation is a verb in the URL, /cancelOrder. REST’s way out is to name the action as a thing, the cancellation of the order, and create it with a POST:

src/app.ts
// An action that isn't create, read, change or delete: it becomes a resource, "the order's
// cancellation", and it's created with POST. Cancelling twice leaves the order the same: cancelled.
app.post('/orders/:id/cancellation', validate('param', idSchema), (c) => {
const order = findOrder(c.req.valid('param').id);
if (!order) return c.json({ error: 'No such order' }, 404);
if (order.status === 'shipped') {
return c.json({ error: 'The order is already shipped: it cannot be cancelled' }, 409);
}
order.status = 'cancelled';
return c.json(order);
});
  • A shipped order can’t be cancelled: 409. A pending or paid one becomes 'cancelled'.
  • Cancelling twice leaves the order the same, cancelled, so the second time isn’t an error.
  • It answers 200 with the order, not 201: the cancellation doesn’t have its own URL to read later. What changed is the order, and that’s what it returns.

GitHub does something similar to lock an issue: PUT /repos/{owner}/{repo}/issues/{issue_number}/lock locks it, and a DELETE to the same URL unlocks it.

The new status goes in src/data.ts:

src/data.ts
// A cancelled order isn't deleted: it's still there, with its status, so it can be looked up later.
export type OrderStatus = 'pending' | 'paid' | 'shipped' | 'cancelled';
export interface Order {
id: number;
productId: number;
quantity: number;
status: OrderStatus;
}

A cancelled order isn’t deleted: it’s still at /orders/17, and whoever looks it up knows what happened to it. A 404 wouldn’t tell them anything.

Deleting a product has a catch. Until now, a new product got “how many there are + 1” as its id. If you delete 3 from [1, 2, 3] and create a cap, two products are left, and the cap gets… 3. The app that saved a link to /products/3, the hoodie, would now show a cap. That’s why createProduct changes, in src/data.ts:

src/data.ts
// The id can no longer be "how many there are + 1": if 2 is deleted from [1, 2, 3], two are left,
// and the next one would be 3 again. A counter that only goes up never repeats an id.
let lastProductId = products.length;
export function createProduct(name: string, price: number): Product {
lastProductId = lastProductId + 1;
const product: Product = { id: lastProductId, name, price };
products.push(product);
return product;
}
// Returns false if it didn't exist.
export function deleteProduct(id: number): boolean {
const position = products.findIndex((product) => product.id === id);
if (position === -1) return false;
products.splice(position, 1);
return true;
}
  • lastProductId starts at 3, the last id that exists, and createProduct adds one to it each time. It’s a let because it changes, and it only goes up: even if you delete, it doesn’t repeat.
  • deleteProduct looks for the product’s position with findIndex, which gives -1 if it isn’t there, and splice(position, 1) removes the element at that position.

A URL always points to the same thing. If the thing disappears, the URL gives 404; it never becomes something else.

Stateless: each request makes sense on its own

Section titled “Stateless: each request makes sense on its own”

Imagine an API that remembers which page of orders it showed you, and gives you the next one with GET /orders/next. It works until the shop runs two copies of the server that share the requests between them (Phase 9):

  1. The app → Server AGET /orders/next
  2. Server A → The app200: page 1
  3. The app → Server BGET /orders/next
  4. Server Bhas never seen this app
  5. Server B → The app200: page 1, again

The solution is for the request to carry what’s needed: either of the two understands GET /orders?page=2. That’s a StatelessSaid of a server that remembers nothing from one request to the next. Each request carries everything needed to understand it, such as who you are or which page you want, and that’s why any copy of the server can handle it. What it does keep is the data, such as the orders.Go to definition API: the server remembers nothing from one request to the next.

Yours already is. Ana sends her token in Authorization with every request, and adminOnly checks it every time: the server doesn’t remember that she “already identified herself.” It does keep the orders, the state of the resources; not the state of the conversation.

In exchange for sending a little more with each request, you get:

  • Any copy of the server can answer, so you can add more when needed (Phase 9).
  • An idempotent request can be repeated without fear: it doesn’t depend on what happened before.
  • Caches work (Phase 2): the same URL means the same thing for everyone.

There’s no naming standard, but there are habits almost every public API follows:

Better Worse Why
POST /orders POST /createOrder The method already says the verb
/orders/17 /order?id=17 The id identifies the resource: it goes in the path. The 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 filters a list, like ?max=10
/payment-methods /paymentMethods Always lowercase: in the path, /Orders and /orders can be different. To separate words, the hyphen is the most common (kebab-case)
/products/2 /products/2.json The format is requested with the Accept header, not in the URL

And no accented letters: a route like /café would travel encoded as /caf%C3%A9.

RESTREST stands for Representational State Transfer.A way of designing APIs on top of HTTP: each thing is a resource with its own URL, and you work with it using the usual methods (GET, POST, PATCH, DELETE) and status codes. Roy Fielding described it in 2000; today the name is used for almost any JSON API organized this way.Go to definition was born in the doctoral thesis of Roy Fielding, one of the authors of HTTP, in 2000. It isn’t a standard or a tool, but a list of constraints that, according to him, explained the web’s success. You’ve already seen three: statelessness, responses that can be cached and a uniform interface, the same way of asking for everything.

The part of that interface almost nobody follows is hypermedia: each response carrying links to what you can do next, like a web page. In 2008, Fielding complained about APIs that called themselves REST without it.

What Fielding described What’s called REST today
Resources, methods and codes Yes Yes
Stateless Yes Yes, almost always
Links in every response Mandatory Few APIs include them

Whoever asks you for “a REST API” almost always wants the second column, the one the shop follows.

The goal: take an order through its whole life, check that the API says no when it should, and compare your design with GitHub’s.

Write the changes above and start the server. The outputs are from Node 26.11.1 and curl 8.14.1, on Linux; your dates and your x-request-id values will be different.

1. Start the API

Ventana de terminal
npm start

What you will see:

> shop-api@1.0.0 start
> node --env-file-if-exists=.env src/server.ts
{"level":30,"time":1791905956996,"pid":53,"hostname":"laptop","msg":"The shop is listening on http://localhost:3000"}

Your computer’s name will show up in hostname. If you get does not provide an export named, check the imports in app.ts. Open another terminal for the rest.

2. Create an order

Ventana de terminal
curl -i --json '{"productId":2,"quantity":1}' localhost:3000/orders

What you will see:

HTTP/1.1 201 Created
content-type: application/json
location: /orders/1
vary: Origin
x-request-id: 0bf60221-f3ef-46e9-ba8d-bcc68fd7d949
content-length: 54
Date: Tue, 13 Oct 2026 15:39:18 GMT
Connection: keep-alive
Keep-Alive: timeout=5
{"id":1,"productId":2,"quantity":1,"status":"pending"}

201, and in location, the new resource’s URL. If your number is different, use it in the next steps.

3. Change the quantity

Ventana de terminal
curl -i -X PATCH --json '{"quantity":3}' localhost:3000/orders/1

What you will see:

HTTP/1.1 200 OK
content-type: application/json
vary: Origin
x-request-id: d0a81c0c-73b0-40ad-8620-5713cb0a8def
content-length: 54
Date: Tue, 13 Oct 2026 15:39:18 GMT
Connection: keep-alive
Keep-Alive: timeout=5
{"id":1,"productId":2,"quantity":3,"status":"pending"}

-X PATCH changes the method --json would use (POST) and keeps the body. The whole order comes back. If you get a 405, your app.all('/orders/:id', …) is before the app.patch: move it to the end.

4. Cancel it, twice

Ventana de terminal
curl -i -w '\n' -X POST localhost:3000/orders/1/cancellation
curl -w '\n' -X POST localhost:3000/orders/1/cancellation

What you will see:

HTTP/1.1 200 OK
content-type: application/json
vary: Origin
x-request-id: a312ef72-57e6-4ba1-a27e-62abd624d0b7
content-length: 56
Date: Tue, 13 Oct 2026 15:39:18 GMT
Connection: keep-alive
Keep-Alive: timeout=5
{"id":1,"productId":2,"quantity":3,"status":"cancelled"}
{"id":1,"productId":2,"quantity":3,"status":"cancelled"}

A POST without a body: the URL says it all. The second time (without -i, only the body) it answers the same: repeating it changes nothing.

5. Try to change it again

Ventana de terminal
curl -i -X PATCH --json '{"quantity":2}' localhost:3000/orders/1

What you will see:

HTTP/1.1 409 Conflict
content-type: application/json
vary: Origin
x-request-id: 94d6f5d4-80ef-4435-af98-a15ed2c48c88
content-length: 63
Date: Tue, 13 Oct 2026 15:39:18 GMT
Connection: keep-alive
Keep-Alive: timeout=5
{"error":"The order is cancelled: it can no longer be changed"}

The request from step 3, with valid data, and now a 409: it clashes with the order’s state.

6. Read the cancellation

Ventana de terminal
curl -i localhost:3000/orders/1/cancellation

What you will see:

HTTP/1.1 405 Method Not Allowed
allow: POST
content-type: application/json
vary: Origin
x-request-id: 5a0822c1-cf80-4fb7-b058-1a8957a99bc3
content-length: 30
Date: Tue, 13 Oct 2026 15:39:18 GMT
Connection: keep-alive
Keep-Alive: timeout=5
{"error":"Method not allowed"}

The route exists, but only for POST: 405, and allow says so. If you get a 404, the /orders/:id/cancellation line is missing from the app.all lines.

Deleting asks for Ana’s token, the one in your .env (lesson 8 of Phase 3). Save it in a shell variable (Phase 1), with yours instead of this one:

7. Delete the hoodie

Ventana de terminal
TOKEN=674679605d58a679d6cd63f7d69113497cfba8acd7383e43
curl -i -X DELETE -H "Authorization: Bearer $TOKEN" localhost:3000/products/3

What you will see:

HTTP/1.1 204 No Content
vary: Origin
x-request-id: 4d7915c5-c968-424e-b641-2258514f66a9
Date: Tue, 13 Oct 2026 15:39:18 GMT
Connection: keep-alive
Keep-Alive: timeout=5

204 and nothing else: no content-type and no body. Repeat the curl and you’ll see a 404. If you get a 401, the token isn’t the one in your .env, or TOKEN is empty: a shell variable only exists in the terminal where you created it.

8. Create a product and look at its id

Ventana de terminal
curl -i -w '\n' -H "Authorization: Bearer $TOKEN" --json '{"name":"Cap","price":15}' localhost:3000/products
curl -w '\n' localhost:3000/products

What you will see:

HTTP/1.1 201 Created
content-type: application/json
location: /products/4
vary: Origin
x-request-id: e720ef30-4b5e-4c09-8032-c2b2b1d58926
content-length: 32
Date: Tue, 13 Oct 2026 15:39:18 GMT
Connection: keep-alive
Keep-Alive: timeout=5
{"id":4,"name":"Cap","price":15}
[{"id":1,"name":"T-shirt","price":20},{"id":2,"name":"Mug","price":8.5},{"id":4,"name":"Cap","price":15}]

The cap is 4, not 3, even though there are only three products: /products/3 is still the deleted hoodie, and it gives 404.

9. Run the tests

Ventana de terminal
npm test

What you will see:

> shop-api@1.0.0 test
> node --env-file=test/.env --test
✔ an unexpected error: 500 without details, with the request id (30.377253ms)
✔ a route that does not exist: 404 in JSON (0.928832ms)
✔ a body that is not JSON: 400, in JSON too (3.170913ms)
✔ the token comes from the environment variable (4.938743ms)
✔ without ADMIN_TOKEN, the server does not start and says why (329.694531ms)
✔ each request writes a JSON line with its id (196.502412ms)
✔ creating an order: 201, with its URL in Location, and it shows up in the list (104.409156ms)
✔ DELETE /products/:id without a token: 401 (1.904497ms)
✔ DELETE /products/:id: 204 without a body; afterwards, 404 (4.482078ms)
✔ after a delete, a new product does not repeat the id of another one (8.282614ms)
✔ PATCH /orders/:id changes the quantity and returns the whole order (4.040328ms)
✔ PATCH with an invalid quantity: 422; to an order that does not exist: 404 (2.765663ms)
✔ PATCH to an order already paid: 409, and the order does not change (2.587997ms)
✔ POST /orders/:id/cancellation: the order is cancelled, even if repeated (2.594747ms)
✔ cancelling a shipped order: 409; one that does not exist: 404 (3.702162ms)
✔ a method the route does not have: 405, with the ones it has in Allow (5.710285ms)
ℹ tests 16
ℹ suites 0
ℹ pass 16
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 934.41754

Sixteen tests and fail 0; your timings will be different. If you get a failure on /slow, you haven’t deleted test/event-loop.test.ts.

Ask GitHub’s API for the Node.js repository and keep a few fields with jq (lesson 8 of Phase 3). This output is from a Mac, with curl 8.7.1:

10. A GitHub resource

Ventana de terminal
curl -s https://api.github.com/repos/nodejs/node | jq '{full_name, description, url, issues_url, pulls_url}'

What you will see:

{
"full_name": "nodejs/node",
"description": "Node.js JavaScript runtime ✨🐢🚀✨",
"url": "https://api.github.com/repos/nodejs/node",
"issues_url": "https://api.github.com/repos/nodejs/node/issues{/number}",
"pulls_url": "https://api.github.com/repos/nodejs/node/pulls{/number}"
}

Look at the URLs: plural nouns, in lowercase, and the issues nested under their repository. The response also carries links to related things, a bit of what Fielding asked for; {/number} is the slot for the issue’s number. Captured on October 13, 2026: the description may change.

Challenge: redesign the five routes from “The problem” with what you’ve seen. Which one becomes a new resource?Show answer
Before After
GET /getProducts.json GET /products
POST /createOrder POST /orders
GET /order?id=17 GET /orders/17
POST /orders/17/change PATCH /orders/17
GET /cancelOrder?id=17 POST /orders/17/cancellation

The last one becomes a resource, “the cancellation,” and stops being a GET: a link no longer triggers it.

  • In the browser’s address bar. github.com/nodejs/node/issues/1 is a resource URL for people: the repository, its collection of issues and one of them.
  • In DevTools (how to open them). In the Network tab of any website, filter by Fetch/XHR: you’ll see the requests to its API, many of them in this style.
  • Verbs in the URL (/createOrder, /deleteProduct). The method is already the verb. If an action doesn’t fit, turn it into a resource.
  • A GET that changes something, like GET /cancelOrder?id=17. Any bot or preview that follows the link runs it.
  • A 200 with the error inside. If the order can’t be changed, it’s a 409, not a 200 with {"ok":false} (Phase 2).
  • Letting the client write the status. With PATCH {"status":"shipped"} anyone could ship an order without paying for it.
  • A REST API names resources with plural nouns: the collection (/orders) and each item with its id (/orders/17), a single address per thing.
  • The method is the verb, and each action always answers with its code: 201 with Location when creating, 200 when reading or changing, 204 when deleting; 409 if it clashes with the state, 422 if the data isn’t valid.
  • An action that doesn’t fit a method becomes a resource: POST /orders/17/cancellation.
  • Stateless: each request carries what’s needed, and that’s why any copy of the server can answer it.
  • Fielding described REST in 2000, with links in every response; today, REST means resources, methods, codes and JSON.
The delivery company has to let the shop know it has delivered order 17. What endpoint do you give it?

The app sends PATCH /orders/5 with {"quantity":2}, but order 5 is already paid. What does the API answer?

Ana deletes a product from the app and gets a 204 without a body. The app shows “Error: empty response”. Where’s the bug?

An API notes in memory who has done POST /login, so it doesn’t ask them for the token every time. Since there are two copies of the server, it sometimes gives a 401 to someone who just logged in. Why?

In an API that gives each new product the id “how many there are + 1,” you delete 1 from [1, 2, 3] and create another one. What id does it get?

A colleague says the shop’s API isn’t “really” REST, because its responses don’t carry links. Is he right?

phase-4 · lesson 1/9