Tell the caller clearly what went wrong, and whether to try again.
Error handling in an API means telling the caller clearly what went wrong, whose fault it is, and whether to try again. HTTP status codes are the short, shared language for this.
When a request fails, two readers need to understand why: the program that made the call (should it retry? log in again? fix its input?) and the person who debugs it later. The status code is for the program. The body is for both.
| Class | Meaning | Whose problem | Retry? |
|---|---|---|---|
| 2xx | It worked | Nobody | Usually no |
| 3xx | Look somewhere else, or use your cached copy | Nobody | Follow the redirect |
| 4xx | The request was wrong | The client | Not until the request is fixed (429 and 409 are special) |
| 5xx | The server failed | The server | Often yes, later, with backoff |
| Code | Name | Use it when | Client should |
|---|---|---|---|
| 200 | OK | A request succeeded and has a body | Use the body |
| 201 | Created | A new resource was created (send its location) | Use the new resource |
| 204 | No Content | Success, nothing to return (for example a delete) | Carry on |
| 301 / 308 | Moved | A resource has a new permanent address | Follow and update links |
| 304 | Not Modified | A conditional GET matched, use your cached copy | Use the cache |
| 400 | Bad Request | The request is malformed or cannot be parsed | Fix the request |
| 401 | Unauthorized | No valid credentials | Log in or refresh |
| 403 | Forbidden | Known user, not allowed | Ask for access |
| 404 | Not Found | No such resource | Do not retry |
| 409 | Conflict | The request clashes with the current state | Reload, merge, retry |
| 422 | Unprocessable Content | Well-formed, but the values break the rules | Fix the values |
| 429 | Too Many Requests | A rate limit was hit (send Retry-After) | Wait, then retry |
| 500 | Internal Server Error | Something unexpected broke | Retry later with backoff |
| 502 / 504 | Bad Gateway / Gateway Timeout | A server behind a proxy failed or was too slow | Retry later with backoff |
| 503 | Service Unavailable | Down or overloaded for now (send Retry-After) | Retry later |
HTTP/1.1 422 Unprocessable Content
Content-Type: application/problem+json
{
"type": "https://example.com/errors/invalid-quantity",
"title": "Quantity is invalid",
"status": 422,
"detail": "quantity must be 1 or more, but was -5",
"instance": "/orders",
"request_id": "req-4f9a21",
"errors": [{ "field": "quantity", "message": "must be at least 1" }]
}The shape above follows the widely used problem details idea: a stable machine-readable type, a human title and detail, the status and a pointer to what failed. Add a request_id so support can find the log line. Any single consistent shape across your API beats a perfect shape used in only half of it.
HTTP/1.1 201
Content-Type: application/problem+json
{
"type": "https://example.com/errors/created",
"title": "Created",
"status": 201,
"detail": "POST /orders β the order was created with id 77.",
"request_id": "req-4f9a21"
}A structured body has a stable type, a human title and detail, the status, and a request id that support can look up in the logs. Never put a stack trace in it.
| Term | Meaning in easy words |
|---|---|
| Status code | A three-digit number at the start of an HTTP response that says how the request went |
| Status class | The first digit of the code: 2xx, 3xx, 4xx or 5xx |
| Client error (4xx) | The request itself was wrong |
| Server error (5xx) | The server failed to handle a good request |
| Retry-After | A header that tells the client how long to wait before trying again |
| Problem details | A standard-style JSON shape for errors: type, title, status, detail |
| Request id / correlation id | A unique id on a request, repeated in logs and error bodies |
| Validation error | A failure because input values break the rules |
| Retryable error | A failure that may succeed if the same request is sent again later |
| Fail fast | Return an error right away instead of letting the caller wait |
| Error budget | How many failures you accept, used to decide if you are reliable enough |
| Choice | We gain | We lose |
|---|---|---|
| Precise codes (409, 422) | Clients can react exactly | More to learn and to document |
| Few coarse codes (400, 500) | Simple for everyone | Clients cannot tell cases apart |
| Detailed error messages | Easy to debug | Risk of leaking internals or hints to attackers |
| Terse messages + request id | Safe and traceable | Support needs log access |
| 404 for hidden resources | Does not reveal that it exists | Slightly harder to debug |
| Retry-After on 429 / 503 | Clients back off politely | You must compute a sensible value |