# Choosing the right HTTP status codes

> How do you choose HTTP status codes? Walk through 200, 201, 204, 400, 401, 403, 404, 409, 422, 429, 500 and 503.

Canonical: https://www.internhack.in/learn/interview/api-design-interview/api-status-codes
Last updated: 2026-11-28

Difficulty: Beginner

## Question

How do you choose HTTP status codes? Walk through 200, 201, 204, 400, 401, 403, 404, 409, 422, 429, 500 and 503.

## Answer

Status codes are the first thing a client checks and often the only thing a monitoring dashboard aggregates. They are a shared vocabulary, so the goal is to be accurate and boring. The classes give the headline: 2xx success, 3xx redirection or not modified, 4xx the client did something wrong, 5xx the server failed. Getting the 4xx versus 5xx split right matters because retry logic, alerts and SLOs all key off it.

200 OK for a successful read or an update that returns a body. 201 Created after a successful creation, with a Location header and usually the created resource in the body. 202 Accepted when work is queued but not done. 204 No Content for success with no body, typical for DELETE and some PUT operations.

400 Bad Request means the request is malformed: broken JSON, wrong types, a missing required header. 422 Unprocessable Content means syntax is fine but the semantics fail validation, such as an email without an at sign or an end date before a start date. Many teams use 400 for both; either is acceptable if you are consistent, but 422 lets clients distinguish parse problems from business validation.

401 Unauthorized really means unauthenticated: no or invalid credentials, and it should include a WWW-Authenticate header. 403 Forbidden means we know who you are, but you may not do this. A common choice: if a user requests an order that belongs to someone else, return 404 rather than 403 to avoid leaking that the resource exists.

404 Not Found for a missing resource or a hidden one. 405 Method Not Allowed when the method is not supported on that URL, with an Allow header. 409 Conflict when the request conflicts with current state: duplicate unique value, editing a resource in the wrong lifecycle state, or an idempotency key reused with a different payload. 412 Precondition Failed when an If-Match check fails. 415 Unsupported Media Type for the wrong content type.

429 Too Many Requests when a rate limit is hit, with a Retry-After header. This is the only sane way to signal throttling.

500 Internal Server Error is the unexpected failure bucket: a bug, unhandled exception. Never leak stack traces. 502 and 504 come from gateways and upstream timeouts. 503 Service Unavailable means temporarily overloaded or in maintenance, ideally with Retry-After, so clients know to back off and retry.

Common mistakes worth calling out: returning 200 with an error message in the body (breaks every monitor and client library), returning 500 for validation errors (pages your on-call for user typos), using 404 for empty lists (an empty collection is 200 with an empty array), and using 403 when you mean 401.

For the interview, show a decision flow: is the caller authenticated, is it allowed, does the resource exist, is the payload well formed, does it pass business validation, does it conflict with current state, did we succeed. Naming this order of checks also tells the interviewer how your middleware pipeline is arranged.

## Code examples

### Status code mapping in Express

```
function errorHandler(err, req, res, next) {
  const map = {
    ValidationError: 422,
    AuthError: 401,
    ForbiddenError: 403,
    NotFoundError: 404,
    ConflictError: 409,
    RateLimitError: 429
  };
  const status = map[err.name] ?? 500;
  if (status === 500) console.error(err); // log, do not leak
  res.status(status).json({
    title: status === 500 ? 'Internal Server Error' : err.message,
    status
  });
}
```

Centralised mapping keeps status usage consistent, and unexpected errors are logged but not exposed.

## Key points

- 401 means unauthenticated, 403 means authenticated but not allowed.
- Use 409 for state conflicts and 422 for semantic validation.
- Empty list is still 200.
- Always send Retry-After with 429 and 503.
- Never return 200 with an error body.

## Common follow-up questions

- 401 or 403 for an expired token?
- What status for a duplicate email on signup?
- When would you return 202?
- Why 404 instead of 403 for other users' resources?

## Interview tips

- Give the flow of checks in order; it demonstrates real middleware thinking.
- Call out the 200-with-error anti-pattern.

## Concepts covered

status codes, 4xx vs 5xx, error semantics, REST

Previous: [PUT vs PATCH vs POST: when to use which](https://www.internhack.in/learn/interview/api-design-interview/api-put-vs-patch-vs-post) · Next: [Designing error responses with RFC 9457 problem details](https://www.internhack.in/learn/interview/api-design-interview/api-error-format-problem-details) · [All of API Design](https://www.internhack.in/learn/interview/api-design-interview)

## More API Design questions

- [Pagination: offset vs cursor (keyset) trade-offs](https://www.internhack.in/learn/interview/api-design-interview/api-offset-vs-cursor-pagination)
- [Filtering, sorting, searching and field selection](https://www.internhack.in/learn/interview/api-design-interview/api-filtering-sorting-fields)
- [API versioning strategies and trade-offs](https://www.internhack.in/learn/interview/api-design-interview/api-versioning)
- [Idempotency keys for payments and retries](https://www.internhack.in/learn/interview/api-design-interview/api-idempotency-keys-payments)
- [Rate limiting: algorithms, headers and 429 handling](https://www.internhack.in/learn/interview/api-design-interview/api-rate-limiting-token-bucket)
- [API keys vs OAuth 2.0 vs JWT: choosing authentication](https://www.internhack.in/learn/interview/api-design-interview/api-authn-keys-oauth-jwt)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [API Design](https://www.internhack.in/learn/interview/api-design-interview)
