opyebAPI Bench

Choosing HTTP status codes without arguing

HTTP · 7 min read

Status codes are the smallest part of an API contract and the part clients depend on most. A client that cannot tell a retryable failure from a permanent one will retry everything, and a client that cannot tell authentication from authorisation will loop through a refresh flow that cannot succeed.

Decide by what the client should do next

The useful question is not what went wrong on the server but what the caller should do about it. Retry, refresh credentials, change the request, or stop. Every code below maps to one of those answers, and that mapping is what makes an error response useful.

CodeMeansClient actionRetry?
400Malformed requestFix the requestNo
401No valid credentialsAuthenticate, then retryAfter auth
403Authenticated, not allowedStop, show a permission errorNo
404No resource hereCheck the URLNo
409Conflicting stateResolve, then retryOnce
422Valid syntax, invalid contentFix the fieldsNo
429Rate limitedWait for Retry-AfterYes
503Temporarily unavailableWait, then retryYes

The pairs that cause most of the confusion

401 versus 403. A 401 says the request lacks valid credentials; a 403 says the credentials are fine but the operation is not permitted. Returning 401 for an authenticated user without permission sends clients into a login loop, because logging in again cannot help.

400 versus 422. If the JSON does not parse or a required field is missing, that is 400. If the shape is correct but a business rule rejects it — an end date before a start date, a coupon that has expired — 422 communicates more precisely. Many teams simply use 400 everywhere, which is acceptable if the body carries a machine-readable error code.

409 versus 412. A conflict with current state, such as a duplicate key, is 409. A failed conditional header such as If-Match is 412, because the client explicitly asked for conditional behaviour and the precondition did not hold.

Rule: never return 200 with an error object. A client that checks only the status code will treat a failed operation as success, and the mistake surfaces much later as corrupted data.

Server errors and retryability

A 500 means the server failed without a more specific answer; a 502 means an upstream returned something unusable; a 503 means the service is refusing traffic on purpose; a 504 means an upstream did not answer in time. From the client's side 502 and 503 are usually retryable, 500 is retryable only for idempotent operations, and 504 depends on whether the work might have completed.

Codes worth adopting

451 is a clear signal for legally blocked content. 428 tells clients that a conditional request is required, which is useful for optimistic concurrency. 425 supports replay protection for early data. 103 lets a browser preload assets before the final response. Adding them costs nothing and removes ambiguity from the contract.

Whatever you choose, document it once and keep it stable. A code that changes meaning between endpoints is worse than a slightly wrong code applied consistently.

Making error bodies as useful as the code

A status code tells the client which family of problem it faces; a structured body tells it what to do next. Include a stable machine-readable error code, a human-readable message, and, where relevant, the field or parameter at fault. Clients should never have to parse prose to decide whether to retry, and support engineers should not have to guess which validation failed.

Keep the error vocabulary small and documented. Twenty error codes that map cleanly to client behaviour are more useful than two hundred that each describe a unique server condition. When a new condition appears, decide first which existing behaviour it belongs to and only add a code when the client genuinely must act differently.

  • Return one error object shape for every failure, including 401 and 429.
  • Never leak stack traces or internal hostnames in production bodies.
  • Document which codes are retryable so client libraries can encode the rule.

Open the calculators and put these numbers to work on your own API.

Open API Bench

Frequently asked questions

Should validation errors be 400 or 422?

Use 400 when the request cannot be parsed, and 422 when it parses but violates a business rule. If you pick only one, use 400 with a structured error code in the body.

Is 403 ever retryable?

Only after the caller changes something — a permission grant, a different token scope, a different resource. Retrying the identical request with the same credentials cannot succeed.

What should a deleted resource return?

404 if it never existed or you do not want to reveal history; 410 if it existed and is permanently gone. 410 is a useful signal for retired endpoints that clients should stop calling.

Sources

Related guides