Resources / Guides
HTTP status codes for APIs: which to return and when
A practical guide to HTTP status codes for REST APIs — 200 vs 201 vs 204, 400 vs 422, 401 vs 403, 404, 409, 429 and 5xx — with error body examples.
Status codes are the first thing a client — or an AI agent — checks. Using the right one tells callers what happened and what to do next without reading your docs.
Success (2xx)
| Code | Use it when |
|---|---|
200 OK | A request succeeded and returns data |
201 Created | A POST created something — add a Location header pointing at it |
202 Accepted | The work was queued and will finish later |
204 No Content | Success with nothing to return, e.g. after DELETE |
Redirects (3xx)
| Code | Use it when |
|---|---|
301 / 308 | The resource moved permanently (308 keeps the method and body) |
302 / 307 | A temporary redirect (307 keeps the method and body) |
304 Not Modified | The client's cached copy (ETag) is still valid |
Client errors (4xx) — "you need to change the request"
| Code | Use it when |
|---|---|
400 Bad Request | The request is malformed: invalid JSON, wrong types, missing fields |
401 Unauthorized | No valid credentials — "who are you?" |
403 Forbidden | Valid credentials, but not allowed — "I know who you are, and no" |
404 Not Found | The resource doesn't exist (or the caller mustn't know it does) |
405 Method Not Allowed | Wrong method for this URL — send an Allow header |
409 Conflict | The request conflicts with current state: a duplicate, a stale version |
410 Gone | It existed and was removed for good |
413 Content Too Large | The body is too big |
415 Unsupported Media Type | Wrong Content-Type |
422 Unprocessable Content | Well-formed, but fails validation rules (some APIs use 400 for all validation errors — pick one and be consistent) |
429 Too Many Requests | Rate limit hit — send Retry-After |
Server errors (5xx) — "it's not you, it's us"
| Code | Use it when |
|---|---|
500 Internal Server Error | An unexpected failure in your code |
502 Bad Gateway | An upstream service returned something invalid |
503 Service Unavailable | Temporarily down or overloaded — send Retry-After |
504 Gateway Timeout | An upstream service didn't answer in time |
Three rules that prevent most confusion
- Never return 200 with an error inside.
{ "ok": false }with200breaks retries, monitoring and every client library. - 4xx is the caller's fault, 5xx is yours. Clients retry 5xx and 429; they shouldn't retry other 4xx.
- Return a useful error body — consistent across every endpoint:
{
"error": "email is required",
"code": "missing_field",
"field": "email"
}
Many APIs use the standard application/problem+json format (RFC 9457) with type, title, status and detail fields.
For AI agents, too
Agents read your errors to decide what to do next. "400: date must be YYYY-MM-DD" lets a model fix its own call; a bare 500 makes it guess.
On Wisp
Your handler returns { status, headers, body }, so any code is one line. Endpoints with input schemas reject invalid input with a clear 400, and Wisp itself answers 401 for missing API keys, 404 for private endpoints, 405 for the wrong method and 429 with Retry-After when a rate limit is hit — before your code runs.
Build it with Wisp — describe an API, get it live.