Skip to content
Commitwise
Menu

How do you design a REST API that clients can rely on: errors, pagination and versioning?

BackendMid-level

In shortUse resource-oriented URLs and correct status codes, a consistent machine-readable error format (such as RFC 9457 problem details), cursor-based pagination for large or changing lists, idempotency for retries, and additive evolution with explicit versions only for breaking changes.

Model answer

Resources and status codes: nouns for resources (/orders/{id}/items), HTTP methods for actions, and status codes that mean what they say: 201 with a Location for creation, 204 for no content, 400 for invalid input, 401/403 for authentication/authorization, 404, 409 for conflicts, 422 for semantic validation if you use it, 429 for rate limits, 5xx only for server faults.

Errors: one format everywhere, for example problem details (application/problem+json, RFC 9457): type, title, status, detail, plus a stable machine-readable code and field-level validation errors. Clients branch on codes, not on English messages. Include a request id for support.

Pagination: cursor (keyset) pagination, ?after=<opaque cursor>&limit=50, is stable when rows are inserted and fast on large tables (it uses an index instead of OFFSET). Offset pagination is fine for small admin lists that need page numbers.

Retries: PUT and DELETE are idempotent by design; for POST that creates payments or orders, accept an Idempotency-Key header and return the original result for repeats.

Evolution: additive changes (new fields, new endpoints) don’t need a version, as long as clients ignore unknown fields. Breaking changes (removing or renaming fields, changing meaning) get a new version (/v2 or a media type), a deprecation header and a sunset date. Mobile apps make this critical: old versions stay installed for years. Document everything with OpenAPI and test compatibility with contract tests.

What they're looking for

Status code discipline, a single error schema with codes clients can act on, pagination trade-offs, backward-compatible evolution, idempotency, and documentation (OpenAPI).

Common mistakes

  • Returning 200 with an error message in the body.
  • Offset pagination on large, frequently changing tables.
  • Breaking changes without a new version or a deprecation period.

More backend questions

Esc

Type to search the whole site.