Choosing HTTP status codes without arguing
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.
| Code | Means | Client action | Retry? |
|---|---|---|---|
| 400 | Malformed request | Fix the request | No |
| 401 | No valid credentials | Authenticate, then retry | After auth |
| 403 | Authenticated, not allowed | Stop, show a permission error | No |
| 404 | No resource here | Check the URL | No |
| 409 | Conflicting state | Resolve, then retry | Once |
| 422 | Valid syntax, invalid content | Fix the fields | No |
| 429 | Rate limited | Wait for Retry-After | Yes |
| 503 | Temporarily unavailable | Wait, then retry | Yes |
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.
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 BenchFrequently 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
- Capacity planning with Little's LawTurn a target request rate and three latency percentiles into the concurrency, threads and instances an API actually needs.
- Tail latency: why P95 and P99 decide your timeoutAverages hide the requests that hurt. How to read percentiles, calculate what they cost, and set timeouts that do not cut off healthy traffic.
- Retry storms: how a 1% error becomes 300% loadRetries are new traffic. Calculate the amplification, recognise the cliff, and design retry budgets that fail politely.
- REST vs GraphQL: measuring the payload differenceOver-fetching, under-fetching and query text all show up in the bytes. A practical way to compare the transfer volume of both designs.