Ankit KanojiyaBeginner

Evergreen guide by Ankit Kanojiya · Beginner · Backend

REST API Design: Resources, Status Codes, and Versioning

A practical guide to designing predictable REST APIs around resources, correct HTTP semantics, and a versioning strategy that survives change.

Well-designed REST APIs are predictable. Clients should be able to guess how an endpoint behaves from its shape, and servers should communicate outcomes through the HTTP contract rather than custom conventions. This guide covers the decisions that matter most in day-to-day API work.

Model Resources, Not Actions

REST centers on resources identified by URLs. Prefer plural nouns and let the HTTP method carry the verb.

  • GET /orders lists orders
  • POST /orders creates one
  • GET /orders/42 fetches a single order
  • PATCH /orders/42 partially updates it
  • DELETE /orders/42 removes it

Avoid encoding verbs in paths such as /getOrders or /createOrder. When an operation genuinely is not CRUD, model it as a sub-resource or a resource that represents the process, for example POST /orders/42/refunds.

Nest only when a child cannot exist without its parent. Deeply nested paths like /users/3/orders/42/items/9 become painful; after one level, prefer top-level access with a filter: GET /items?order_id=42.

Use HTTP Methods Honestly

Method semantics are a contract, not decoration.

  • GET and HEAD are safe and must not change state.
  • PUT and DELETE are idempotent: repeating them yields the same end state.
  • POST is neither safe nor idempotent and is used for creation or non-idempotent actions.
  • PATCH applies a partial update.

Honoring these lets caches, proxies, and client retry logic work correctly. A GET with side effects breaks assumptions throughout the stack.

Return the Right Status Codes

Status codes let clients branch without parsing prose.

  • 200 OK for a successful read or update
  • 201 Created with a Location header for a new resource
  • 204 No Content when there is nothing to return
  • 400 Bad Request for malformed input
  • 401 Unauthorized when authentication is missing or invalid
  • 403 Forbidden when the caller is authenticated but not allowed
  • 404 Not Found for a missing resource
  • 409 Conflict for version or uniqueness clashes
  • 422 Unprocessable Entity for semantically invalid data
  • 429 Too Many Requests when rate limited
  • 500 and 503 for server faults

Reserve 4xx for client errors and 5xx for server errors. Mislabeling a validation failure as 500 sends clients into retry loops that make incidents worse.

Design Consistent Payloads

Pick one error shape and use it everywhere. A machine-readable code plus a human message works well.

{
  "error": {
    "code": "order_not_found",
    "message": "No order exists with id 42",
    "details": []
  }
}

For collections, return pagination metadata so clients never have to guess. Cursor pagination scales better than offset pagination on large, frequently changing tables because it avoids rows shifting between pages.

GET /orders?limit=20&cursor=eyJpZCI6NDJ9

Filtering, Sorting, and Partial Responses

Keep query parameters for behavior that modifies a read rather than identifies a resource.

  • Filtering: GET /orders?status=paid
  • Sorting: GET /orders?sort=-created_at
  • Field selection: GET /orders?fields=id,total

Document defaults explicitly, including the default page size and maximum allowed limit, so clients cannot accidentally request unbounded result sets.

Versioning That Survives Change

Breaking changes are inevitable; the goal is to introduce them without breaking existing clients.

  • URL versioning (/v1/orders) is the most visible and the easiest to route and cache.
  • Header versioning (Accept: application/vnd.api.v2+json) keeps URLs stable but is harder to test by hand.

URL versioning is the pragmatic default for most teams. Whatever the mechanism, treat additive changes (new optional fields, new endpoints) as non-breaking and avoid them triggering a version bump. Reserve new versions for removals, renames, and changes in meaning. Publish a deprecation timeline and signal it with a Deprecation header so clients have a migration window.

Idempotency for Writes

Networks drop responses, so clients retry. For non-idempotent creation, accept an idempotency key.

POST /payments
Idempotency-Key: 9f1c2b7a-5e2a-4a1e-9f3d-7c0b2a1d4e5f

The server stores the result for that key and returns the same response on a replay, preventing duplicate charges.

Practical Checklist

  • Use nouns for paths and methods for verbs.
  • Map outcomes to accurate status codes.
  • Standardize error and pagination shapes.
  • Validate input and reject early with 400 or 422.
  • Version deliberately and deprecate on a schedule.
  • Support idempotency keys for critical writes.

Consistency compounds. An API where every endpoint follows the same rules is far cheaper to consume, document, and maintain than one that is individually clever in a dozen different ways.