Guide

Choosing the right HTTP status code: a guide for API authors

Published · 8 minute read

A status code is the part of a response that software reads before anything else. Browsers, caches, proxies, monitoring and retry logic all act on it without looking at the body. Choosing the right one is less about pedantry and more about making those systems do the correct thing automatically.

The first digit is a contract

ClassMeaningWho should act
2xxIt workedNobody
3xxLook somewhere elseThe client, automatically
4xxThe request is wrongThe caller must change something before trying again
5xxThe server failedThe server's owner; the caller may retry later

The most important decision is 4xx versus 5xx, because it assigns responsibility. Alerting is normally built on the 5xx rate. Returning 500 for a malformed request pages someone at night for a client's typo. Returning 400 for a database outage hides a real incident and tells clients not to retry something that would have worked a minute later.

Success: 200, 201, 202, 204

One anti-pattern deserves a mention: returning 200 with {"error": "..."} in the body. Every generic tool between you and the client (caches, retry libraries, dashboards) now records a failure as a success.

Redirects: 301, 302, 303, 307, 308

Two questions pick the code: is the move permanent, and must the method be preserved?

May change POST to GETKeeps the method
Permanent301308
Temporary302307

For historical reasons, browsers follow a 301 or 302 response to a POST by issuing a GET, dropping the body. If an API endpoint moves and clients send POST or PUT, use 307 or 308 so the request is repeated intact. 303 See Other deliberately says "now GET this other URL", which is the right answer after a form submission so that refreshing the page does not submit it again.

Permanent redirects are cached, sometimes indefinitely. Test with a temporary one first; a wrong 301 can be remembered by browsers long after you fix the server.

304 Not Modified is not really a redirect. It answers a conditional request (If-None-Match or If-Modified-Since) by telling the client that its cached copy is still good, saving the transfer.

Client errors people mix up

401 versus 403

401 Unauthorized means "I do not know who you are": credentials are missing, expired or invalid. The client can fix it by authenticating. 403 Forbidden means "I know who you are, and you may not do this". Logging in again will not help. The names are misleading; think of 401 as unauthenticated and 403 as not permitted.

Sometimes an API returns 404 instead of 403 on purpose, so that it does not confirm that a private resource exists. That is a legitimate choice; make it consistently.

400 versus 422

400 Bad Request is for a request the server cannot parse: broken JSON, a missing required header. 422 Unprocessable Content is for a request that is well-formed but fails validation: an end date before the start date, an email without an at sign. Many APIs use 400 for both, which is acceptable as long as the body says exactly which field is wrong and why.

404 versus 410

404 Not Found says nothing about the past or future. 410 Gone says the resource existed and has been removed for good. Search engines drop a 410 page faster, and clients can stop asking.

409 and 412

409 Conflict means the request is valid but clashes with the current state: creating a user with an email that is already taken, or editing a record someone else just changed. 412 Precondition Failed is the specific answer when the client sent an If-Match header and the version no longer matches, which is how optimistic locking is expressed over HTTP.

405 and 415

405 Method Not Allowed: the URL exists but not for this method; list the permitted ones in an Allow header. 415 Unsupported Media Type: the body is in a format the endpoint does not accept, often because Content-Type is missing or wrong.

429 Too Many Requests

The client is being rate limited. Always include a Retry-After header with the number of seconds to wait. Without it, well-behaved clients guess, and badly behaved ones retry at once and make the overload worse.

Server errors: 500, 502, 503, 504

The difference between 502 and 504 is a useful first clue during an incident: 502 points to a process that is down, 504 to one that is slow.

Which errors are safe to retry?

ResponseRetry?
Most 4xx (400, 401, 403, 404, 409, 422)No. The same request will fail the same way
408, 429Yes, after waiting; honour Retry-After
502, 503, 504Yes, with increasing delays and some randomness
500Sometimes; a bug will fail again, a transient fault will not

A retry is only safe if repeating the request cannot do harm. GET, PUT and DELETE are defined to be idempotent: doing them twice leaves the same state as doing them once. POST is not. After a 504 on a payment request, the client cannot know whether the charge happened. The standard remedy is an idempotency key: the client sends a unique value with the request, and the server returns the original result if it sees that value again instead of repeating the work.

Make the body as useful as the code

The code tells software what to do; the body tells a person what to fix. A good error body is machine-readable, stable and specific:

HTTP/1.1 422 Unprocessable Content
Content-Type: application/problem+json

{
  "type": "https://example.com/problems/validation",
  "title": "Validation failed",
  "status": 422,
  "errors": [{ "field": "end_date", "message": "must be after start_date" }]
}

This shape is the "problem details" format standardised in RFC 9457. Whatever format you choose, never return a stack trace or an SQL error to the client: it helps attackers more than users. You can look up any code and its intended meaning in the HTTP status reference, and send test requests with the HTTP client.

Tools used in this guide

More guides