{}DSA Atlas

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.

Beginner → Intermediate6 interview questions

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

ConstraintWhy it matters in practice
Client–serverUI and backend evolve independently
Statelessany server can handle any request → horizontal scaling
CacheableCDNs and browsers can reuse responses
Uniform interfacegeneric tooling, predictable APIs
Layered systemproxies, 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."

ResourceURINotes
Customer/customers/{id}
Product/products/{id}public, cacheable
Order/orders/{id}owned by a customer
Order items/orders/{id}/itemspart 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_case or camelCase, pick one).

Step 3: map operations to methods and status codes

OperationRequestSuccessCommon errors
ListGET /orders?…200 + page400 (bad filter)
CreatePOST /orders201 + Location400/422 validation, 409 duplicate
ReadGET /orders/{id}200 (+ ETag)404
ReplacePUT /orders/{id}200 / 204404, 409, 412
Partial updatePATCH /orders/{id}200404, 409, 412, 422
DeleteDELETE /orders/{id}204404 (or 204 again — idempotent)
Async actionPOST /exports202 + status URL429

Step 4: actions that aren't CRUD

Text
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

JSON
{
  "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

Level 0 — one endpoint, RPC over HTTP (POST /api with an "action" field) Level 1 — resources: /orders/42, /customers/7 Level 2 — HTTP methods + status codes (most real APIs) Level 3 — hypermedia controls (HATEOAS)
Aim for solid Level 2; add links where they help clients discover allowed actions.

Worked example: a ride-hailing API (sketch)

Text
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

  1. Resources, relationships and identifiers.
  2. Endpoints with methods and status codes.
  3. Request/response shapes (show one JSON example).
  4. Pagination, filtering, sorting for every list.
  5. AuthN/AuthZ (who can call what — object-level checks).
  6. Idempotency for creates/payments; concurrency control for updates (ETags).
  7. Errors (consistent format), rate limits, versioning.
  8. Async operations (202 + status) and events/webhooks if needed.

Interview questions

0 / 6 marked confident

Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.