Status Codes and Error Handling
Read the status codes that matter most, return and interpret useful error bodies, and decide which failures a client should retry, fix or report to the user.
Welcome back!
Step 1 of 14Learn
The codes you'll see every day
HTTP defines dozens of status codes, but a small set covers most real traffic.
Success (2xx)
- 200 OK: the request worked; the body has the result.
- 201 Created: a new resource was created (typically after POST); a
Locationheader often points to it. - 204 No Content: success with no body, common after DELETE or some updates.
Redirection (3xx)
- 301 Moved Permanently / 308 Permanent Redirect: use the new URL from now on.
- 304 Not Modified: your cached copy is still current, so no body is sent.
Client errors (4xx): the request was wrong; sending it again unchanged won't help.
- 400 Bad Request: malformed or invalid input.
- 401 Unauthorized: missing or invalid credentials (really "unauthenticated").
- 403 Forbidden: you're identified, but not allowed to do this.
- 404 Not Found: no resource at this URL.
- 409 Conflict: the request clashes with the current state, such as a duplicate username.
- 422 Unprocessable Content: well-formed JSON, but the values fail validation.
- 429 Too Many Requests: you've hit a rate limit; slow down.
Server errors (5xx): the server failed; the request may succeed later.
- 500 Internal Server Error: an unexpected bug on the server.
- 502 Bad Gateway / 504 Gateway Timeout: a server in the middle couldn't get a good answer from the one behind it.
- 503 Service Unavailable: overloaded or down for maintenance.
Step 2 of 14Practice · Match the pairs
Match each situation to the status code a well-designed API would return.
Tap an answer, then tap the place where it belongs (or drag it there).
- The request has no API token at all
- A regular user tries to delete another user's account
- The client asks for
/orders/999999, which doesn't exist - Signing up with a username that is already taken
- The client has sent 600 requests in a minute against a limit of 100
Step 3 of 14Diagram
The status codes you will meet most
Tap a shape with to see more.
- HTTP status codes
- 2xx Success
- 200 OK
- 201 Created
- 3xx Redirect
- 301 Moved
- 304 Not Modified
- 4xx Client error
The request itself has a problem.
- 401 Unauthorized
- 404 Not Found
- 429 Too Many Requests
- 5xx Server error
The server failed on a request that may be fine.
- 500 Internal Error
- 503 Unavailable
Step 4 of 14Learn
Error bodies that actually help
A status code says what kind of problem occurred. A good API also explains what exactly went wrong in the response body, in a consistent format. Compare:
{ "error": "Bad request" }with:
{
"type": "https://api.example.com/errors/validation",
"title": "Validation failed",
"status": 422,
"detail": "2 fields are invalid.",
"errors": [
{ "field": "email", "message": "must be a valid email address" },
{ "field": "age", "message": "must be at least 13" }
]
}The second lets a client highlight the exact form fields and lets a developer fix the request without guessing. Its shape follows Problem Details for HTTP APIs (RFC 9457), a standard format with fields such as type, title, status and detail, served with the content type application/problem+json. Many APIs use it or something close.
Guidelines for good error responses:
- Use the right status code. A body that says
"success": falseon a 200 response forces every client to dig into bodies to detect failure. - Keep the format consistent across every endpoint.
- Make messages specific and actionable: "must be at least 13", not "invalid input".
- Don't leak internals: no stack traces, SQL queries or server paths in production responses. Log those on the server instead.
Step 5 of 14Practice · Spot the mistake
An API team reviews these error responses. Click the ones that follow bad practice.
Step 6 of 14Practice · Chart
A day of traffic on a healthy API, by status family
A sudden rise in which slice usually means the API team itself has a problem to fix?
Tap the slice that answers the question, then press Check.
| Item | Share of responses |
|---|---|
| 2xx success | 93.5% |
| 3xx redirect/cache | 2.5% |
| 4xx client error | 3.7% |
| 5xx server error | 0.3% |
Step 7 of 14Learn
How a client should react
Writing a robust client mostly means deciding what to do with each family of responses:
- 2xx: parse the body (if any) and continue.
- 400 / 422: the request is wrong. Don't retry; show the validation messages to the user or fix the code.
- 401: credentials are missing or expired. Refresh the token or ask the user to sign in again, then retry once.
- 403: don't retry; the user lacks permission. Show a clear message.
- 404: the resource doesn't exist (or was deleted). Handle it as a normal case, for example "this item is no longer available".
- 429: you're sending too fast. Wait, preferably for the number of seconds in the Retry-After header if present, then retry.
- 5xx and network errors: often temporary. Retry a limited number of times with growing delays, but only for idempotent requests, or for POSTs protected by an idempotency key.
Two common mistakes. First, assuming success: JavaScript's fetch does not throw on 404 or 500; it throws only when the network itself fails. You must check response.ok or response.status yourself. Second, retrying forever: a client that instantly re-sends failed requests in a loop can turn a brief outage into a flood that keeps the server down. You'll build a proper retry with backoff in the next module.
Step 8 of 14Practice · Sort into groups
For each response, what should a well-behaved client do?
Tap an item, then tap its group (or drag it there).
Step 9 of 14Practice · Code challenge
Write nextAction(status) that decides what a client should do with a response status code. Return "ok" for 200–299, "retry" for 429 and for 500–599, "reauth" for 401, and "fail" for any other code.
nextAction(200)nextAction(204)nextAction(429)nextAction(503)nextAction(401)nextAction(404)nextAction(422)
Step 10 of 14Practice · Diagram
A client's decision tree for any response
Which branch handles a 503 Service Unavailable?
Tap the shape that answers the question, then press Check.
- Response arrives
- 2xx: use the result
- 401: refresh token, then retry once
- 429 or 5xx: wait and retry
Use backoff, and honor Retry-After if present.
- Other 4xx: stop, report the error
Repeating the same bad request gives the same answer.
Step 11 of 14Note
Step 12 of 14Learn
Redirects and "not modified"
The 3xx family deserves a closer look, because clients meet it more often than people expect.
- 301 Moved Permanently and 308 Permanent Redirect say the resource has a new URL for good, given in the
Locationheader. Clients should update stored links. 308 guarantees the method and body are kept, so a POST stays a POST; with 301, some old clients switch to GET. - 302 Found and 307 Temporary Redirect send the client elsewhere just this once. Again, 307 preserves the method and body; 302 may not.
- 304 Not Modified isn't a redirect at all: it answers a conditional request and tells the client its cached copy is still valid.
Most HTTP libraries follow redirects automatically, which is convenient but can hide surprises. A common one: an API moves from http:// to https:// with a 301, and a client that POSTs to the old URL is silently redirected as a GET, losing its body. Another: some libraries drop the Authorization header when a redirect goes to a different host, as a security measure, so the second request fails with 401.
The safest habit is to use the exact, current HTTPS base URL from the documentation, and treat redirects as a sign that your configuration needs updating.
Step 13 of 14Practice · Match the pairs
Match each 3xx code to its meaning.
Tap an answer, then tap the place where it belongs (or drag it there).
- 301
- 308
- 307
- 304
Step 14 of 14Practice · Choose one
A JavaScript app calls const res = await fetch(url) and the server responds with 500. What happens?
Lesson complete!