API Design: REST Done Right

REST conventions, versioning, pagination, errors, idempotency keys. Your API is your contract.

Beginner7 min read
Language
Theme
ClientGET /users/42/ordersAPInouns + methods200 / 201 / 4xx / 429Cursor pagination?after=order_8123Idempotency keyretries never double-chargeVersioning: only add, never remove (or /v1, /v2)
Key idea at a glance

An API is a contract you cannot renegotiate cheaply

Once clients integrate, every breaking change is someone else's outage. Good API design is therefore mostly about predictability: consistent naming, consistent errors, consistent pagination. Boring is a compliment here.

REST ground rules that actually matter

  • Resources are nouns: GET /users/42/orders, not /getUserOrders. Collections plural, hierarchy reflects ownership.
  • Methods carry semantics: GET (safe, cacheable, no side effects), POST (create/process), PUT (full replace, idempotent), PATCH (partial update), DELETE. Misusing GET for state changes breaks caches, prefetchers and retry logic.
  • Status codes tell the truth: 200 OK, 201 created, 204 no content, 400 bad input, 401 unauthenticated, 403 unauthorized, 404 missing, 409 conflict, 429 rate limited, 500 your fault. Returning 200 with an error body defeats every retry and monitoring tool in the chain.
  • Errors are structured: a stable shape like {error: {code: "card_declined", message: "...", request_id: "..."}} so clients can handle programmatically and support can trace.

Pagination: the detail everyone underestimates

Offset (?page=5&limit=50): simple, but OFFSET 100000 makes the DB scan and discard 100k rows: deep pages get slower linearly, and inserts during pagination cause duplicates/skips. Cursor (?after=order_8123&limit=50): the client passes the last seen ID; the query is WHERE id > X ORDER BY id LIMIT 50: O(1) at any depth, stable under inserts. Use offset for small admin tables, cursors for feeds and anything big.

Idempotency: the payments-grade requirement

Client taps Pay, request times out, client retries. Without protection: double charge. The pattern: the client generates a unique idempotency key per logical operation; the server stores it with the result; a retry with the same key returns the stored result instead of re-executing. Stripe popularized this; every mutating endpoint that can be retried needs it (payments, order creation, booking). GETs are naturally idempotent; POSTs are not.

Versioning and evolution

The cheapest versioning is never breaking: only add fields and endpoints, never remove or rename; clients ignore what they do not know. When a true break is unavoidable: /v1/... in the URL (simple, cache-friendly) or a version header, with the old version maintained for a deprecation window and sunset headers announcing the end date.

Worked example: an orders API

POST /orders (idempotency key required, returns 201 + order), GET /orders?after=...&limit=50 (cursor), GET /orders/123, POST /orders/123/cancel (action as sub-resource since it does not fit CRUD), errors in the stable shape above, 429s with Retry-After, /v1 prefix from day one.

Common interview traps

  • 200-with-error-body responses.
  • No pagination, or offset pagination on a firehose.
  • Retryable mutating endpoints without idempotency keys.
  • Verbs in URLs and one-off naming conventions per endpoint.

Interview Q&A

Q: PUT vs PATCH?
A: PUT replaces the whole resource and is idempotent (send it twice, same result). PATCH applies a partial change and may not be idempotent. For APIs clients retry, prefer idempotent operations or idempotency keys.

Q: How do you keep webhooks reliable as an API surface?
A: Sign payloads (HMAC), deliver with retries + exponential backoff, make events idempotent (event IDs), provide a replay endpoint, and document ordering (usually not guaranteed, events carry timestamps and full state).

Key takeaways

Interview pointer

In any design where services talk, the interviewer may probe the API shape. Have resource naming, pagination strategy, and idempotency ready.