Topic 031 / Core
REST API design
Chapter 3.4 · API Styles and Serialization
What and why
Once a server is reachable (→ 30), its API needs a consistent shape so clients can predict how to call any endpoint without reading bespoke documentation for each one. REST (representational state transfer) is an architectural style for HTTP (→ 28) APIs: resources identified by URIs, manipulated through a small set of standard HTTP methods, each request self-contained, with outcomes carried in standard status codes. Builds on: → 28.
Diagram
| Resource | URI | Method | Meaning |
|---|---|---|---|
| Collection | /orders |
GET | list orders |
| Collection | /orders |
POST | create an order |
| Item | /orders/{id} |
GET | read one order |
| Item | /orders/{id} |
PATCH | partially update it |
| Item | /orders/{id} |
DELETE | remove it |
How it works
- Model the domain as nouns — resources — not verbs; actions become HTTP methods on a
resource, not endpoint names like
/createOrder. - Give every resource a stable URI; collections and individual items each get their own.
- Use HTTP methods for the verb: GET to read, POST to create, PUT to replace, PATCH to partially update, DELETE to remove (→ 28's method semantics apply directly).
- Carry outcome in the status code, not only in the response body — a client should be able to branch on status alone for the common cases.
- Keep each request self-contained: no server-side session state the client silently depends on between requests (statelessness, generalized later at → 91).
- Use one consistent error-body shape across every endpoint, so client error handling isn't rewritten per endpoint.
Practical example
(illustrative). POST /orders with a valid body returns 201 with
the new order's location; GET /orders/{id} returns 200 with the order or 404 if it
doesn't exist; DELETE /orders/{id} returns 204 with an empty body on success — every
endpoint answers through the same small vocabulary of statuses instead of inventing its
own.
When to use it
- Use when: designing a default public or external API — wide client support, cacheable reads, a simple shared mental model.
- Avoid when: many different client shapes need the same underlying data (→ 32 fits better), or the calls are purely internal service-to-service traffic needing strict typed contracts and low latency (→ 33 fits better).
- Failure scenarios: inconsistent verb or status usage across endpoints forces every client to special-case each one; leaking internal implementation details (database IDs, internal state names) into the public contract couples clients to internals that should be free to change.
- Simpler alternative: one well-designed REST API is simpler than defaulting to GraphQL or putting every action behind POST regardless of what it does.
Practice
(pattern kind — implement minimal; 20–30 min, no lab services). Design the resource model for Project 1 (task management) or Project 2 (URL shortener): list every resource, its URI, its methods, and the status code each method returns on success and on the most likely failure — before writing any handler code. Done when every resource has a URI, methods, and success/failure status codes, with no verbs in the URIs.
Key takeaway
REST gives an API a predictable shape by treating the domain as resources and letting HTTP's existing verbs and status codes carry meaning. Done when you can: design a resource table for a small domain with no endpoint needing its own special-case client logic. Used next in → 35, 36, 37; Project 1 (checklist: resource-level authorization builds on this API, topic 39).
References
None — this topic makes no external claims.
Source directory