REST & Resource Modelling
Fielding's REST constraints, modelling a domain as resources and sub-resources, URI design rules, mapping operations to methods and status codes, actions that don't fit CRUD, HATEOAS and the Richardson maturity model, plus a complete worked API design.
REST in one sentence
Expose your domain as resources identified by URLs, manipulate them through a small uniform set of methods, and let HTTP's semantics (status codes, caching, content negotiation) do the heavy lifting.
Fielding's constraints
| Constraint | Why it matters in practice |
|---|---|
| Client–server | UI and backend evolve independently |
| Stateless | any server can handle any request → horizontal scaling |
| Cacheable | CDNs and browsers can reuse responses |
| Uniform interface | generic tooling, predictable APIs |
| Layered system | proxies, gateways, CDNs can be inserted transparently |
| Code on demand (optional) | e.g. JavaScript delivered to browsers |
Step 1: find the resources
Start from the domain, not the database or the screens. Nouns become resources; verbs become methods or state transitions.
Domain: "Customers place orders containing products. Orders are paid, then shipped in one or more shipments. Customers can review products they bought."
| Resource | URI | Notes |
|---|---|---|
| Customer | /customers/{id} | |
| Product | /products/{id} | public, cacheable |
| Order | /orders/{id} | owned by a customer |
| Order items | /orders/{id}/items | part of the order aggregate |
| Payment | /orders/{id}/payments or /payments/{id} | its own lifecycle |
| Shipment | /shipments/{id} | an order can have several |
| Review | /products/{id}/reviews, /reviews/{id} |
Step 2: URI design rules
- Plural nouns for collections:
/orders,/orders/{id}. - No verbs in paths for CRUD: not
/getOrders, not/createOrder. - Lowercase, hyphenated:
/shipping-addresses. - Opaque, stable IDs (
ord_9Kx2), never sequential DB ids if enumeration is a risk. - Shallow nesting: at most
/parents/{id}/children. - Query strings for filtering, sorting, paging, field selection:
/orders?status=paid&sort=-created_at&limit=20. - No file extensions; use
Accept/Content-Type. - Use consistent casing in JSON (
snake_caseorcamelCase, pick one).
Step 3: map operations to methods and status codes
| Operation | Request | Success | Common errors |
|---|---|---|---|
| List | GET /orders?… | 200 + page | 400 (bad filter) |
| Create | POST /orders | 201 + Location | 400/422 validation, 409 duplicate |
| Read | GET /orders/{id} | 200 (+ ETag) | 404 |
| Replace | PUT /orders/{id} | 200 / 204 | 404, 409, 412 |
| Partial update | PATCH /orders/{id} | 200 | 404, 409, 412, 422 |
| Delete | DELETE /orders/{id} | 204 | 404 (or 204 again — idempotent) |
| Async action | POST /exports | 202 + status URL | 429 |
Step 4: actions that aren't CRUD
POST /orders/{id}/cancellation # model the action as a resource (has reason, time, refund)
POST /orders/{id}:cancel # Google AIP "custom method" style
PATCH /orders/{id} {"status":"cancelled"} # plain state transition
POST /payments/{id}/refunds # refunds are resources with their own lifecycle
POST /emails # "send email" = create an email resource
Pick one style and use it everywhere. Make actions idempotent and validate allowed state transitions (an order can't be cancelled after it has shipped → 409 Conflict with a clear error).
Step 5: representations
{
"id": "ord_9Kx2",
"object": "order",
"status": "paid",
"customer_id": "cus_42",
"items": [
{ "product_id": "prd_7", "quantity": 2, "unit_price": { "amount": 49900, "currency": "INR" } }
],
"total": { "amount": 99800, "currency": "INR" },
"created_at": "2026-10-07T10:21:00Z",
"links": { "self": "/v1/orders/ord_9Kx2", "payments": "/v1/orders/ord_9Kx2/payments" }
}
Guidelines: ISO-8601 UTC timestamps; money as integer minor units + currency; enums as lowercase strings; no internal fields (DB ids, internal flags); include id and maybe a type/object discriminator; envelope list responses ({"data": [...], "next_cursor": "..."}) so you can add metadata later.
The Richardson maturity model
Worked example: a ride-hailing API (sketch)
POST /v1/ride-requests rider requests a ride (Idempotency-Key header)
GET /v1/ride-requests/{id} poll status (or subscribe via SSE/WebSocket)
POST /v1/ride-requests/{id}:cancel rider cancels (allowed until pickup)
GET /v1/drivers/me/offers driver sees offers (long poll / push)
POST /v1/offers/{id}:accept driver accepts (first accept wins → 409 for others)
PATCH /v1/trips/{id} driver updates trip state: arrived → started → completed
PUT /v1/drivers/me/location high-frequency location updates (or UDP/WebSocket)
GET /v1/trips/{id}/receipt fare breakdown
POST /v1/trips/{id}/ratings rate the trip
Note how real-time parts (driver location, offers) step outside pure REST into push channels — see Real-time communication.
Checklist for any REST design question
- Resources, relationships and identifiers.
- Endpoints with methods and status codes.
- Request/response shapes (show one JSON example).
- Pagination, filtering, sorting for every list.
- AuthN/AuthZ (who can call what — object-level checks).
- Idempotency for creates/payments; concurrency control for updates (ETags).
- Errors (consistent format), rate limits, versioning.
- Async operations (202 + status) and events/webhooks if needed.
Interview questions
Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.