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.
- REST API
- HTTP
- API Design
- Status Codes
- Versioning
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 /orderslists ordersPOST /orderscreates oneGET /orders/42fetches a single orderPATCH /orders/42partially updates itDELETE /orders/42removes 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.
GETandHEADare safe and must not change state.PUTandDELETEare idempotent: repeating them yields the same end state.POSTis neither safe nor idempotent and is used for creation or non-idempotent actions.PATCHapplies 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 OKfor a successful read or update201 Createdwith aLocationheader for a new resource204 No Contentwhen there is nothing to return400 Bad Requestfor malformed input401 Unauthorizedwhen authentication is missing or invalid403 Forbiddenwhen the caller is authenticated but not allowed404 Not Foundfor a missing resource409 Conflictfor version or uniqueness clashes422 Unprocessable Entityfor semantically invalid data429 Too Many Requestswhen rate limited500and503for 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
400or422. - 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.