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)

CodeUse it when
200 OKA request succeeded and returns data
201 CreatedA POST created something — add a Location header pointing at it
202 AcceptedThe work was queued and will finish later
204 No ContentSuccess with nothing to return, e.g. after DELETE

Redirects (3xx)

CodeUse it when
301 / 308The resource moved permanently (308 keeps the method and body)
302 / 307A temporary redirect (307 keeps the method and body)
304 Not ModifiedThe client's cached copy (ETag) is still valid

Client errors (4xx) — "you need to change the request"

CodeUse it when
400 Bad RequestThe request is malformed: invalid JSON, wrong types, missing fields
401 UnauthorizedNo valid credentials — "who are you?"
403 ForbiddenValid credentials, but not allowed — "I know who you are, and no"
404 Not FoundThe resource doesn't exist (or the caller mustn't know it does)
405 Method Not AllowedWrong method for this URL — send an Allow header
409 ConflictThe request conflicts with current state: a duplicate, a stale version
410 GoneIt existed and was removed for good
413 Content Too LargeThe body is too big
415 Unsupported Media TypeWrong Content-Type
422 Unprocessable ContentWell-formed, but fails validation rules (some APIs use 400 for all validation errors — pick one and be consistent)
429 Too Many RequestsRate limit hit — send Retry-After

Server errors (5xx) — "it's not you, it's us"

CodeUse it when
500 Internal Server ErrorAn unexpected failure in your code
502 Bad GatewayAn upstream service returned something invalid
503 Service UnavailableTemporarily down or overloaded — send Retry-After
504 Gateway TimeoutAn upstream service didn't answer in time

Three rules that prevent most confusion

  1. Never return 200 with an error inside. { "ok": false } with 200 breaks retries, monitoring and every client library.
  2. 4xx is the caller's fault, 5xx is yours. Clients retry 5xx and 429; they shouldn't retry other 4xx.
  3. 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.

Related