All ArticlesWeb Standards & APIs

HTTP Status Codes & RESTful Error Handling Architecture: A Senior Engineer's Guide

Vijay Aravindh
· Last updated:
5 min read

TL;DR

Best practices for designing semantic REST API error responses using RFC 9457 (Problem Details), standard HTTP status codes, and resilient client error handling.

Try the Interactive Tool

Look up 400, 401, 403, 422, and 429 in HTTP Status Codes while designing an API error response.

Open HTTP Status Codes →

Proper HTTP status code selection is fundamental to building reliable, predictable web APIs. Returning generic 200 OK responses with { "error": true } or indiscriminately throwing 500 Internal Server Error breaks standard HTTP caching, monitoring alerts, and client retry logic.


1. Key Status Code Categories

  • 2xx Success: 200 OK (standard success), 201 Created (resource created via POST), 204 No Content (successful DELETE/PUT with no response body).
  • 3xx Redirection: 301 Moved Permanently (SEO permanent redirect), 304 Not Modified (cached asset revalidation).
  • 4xx Client Errors: 400 Bad Request (malformed syntax), 401 Unauthorized (missing or invalid auth token), 403 Forbidden (authenticated but lacking permission), 404 Not Found (resource does not exist), 409 Conflict (state conflict, e.g. duplicate email), 422 Unprocessable Content (semantic validation failure), 429 Too Many Requests (rate limit exceeded).
  • 5xx Server Errors: 500 Internal Server Error (unhandled exception), 502 Bad Gateway (upstream failure), 503 Service Unavailable (temporary overload/maintenance), 504 Gateway Timeout (upstream timeout).

2. Standardized Error Payloads: RFC 9457

Rather than inventing ad-hoc error formats, modern APIs adopt RFC 9457 (Problem Details for HTTP APIs):

Worked example

{
  "type": "https://api.example.com/problems/invalid-parameter",
  "title": "Invalid Request Parameters",
  "status": 422,
  "detail": "The 'limit' query parameter must be an integer between 1 and 100.",
  "instance": "/problem-instances/request-17",
  "invalid_params": [
    {
      "name": "limit",
      "reason": "Value 500 exceeds maximum allowed limit of 100"
    }
  ]
}

3. Reference and Testing Tools

Explore the complete registry of standard and non-standard HTTP status codes with our interactive HTTP Status Code Reference and inspect request headers with our HTTP Header Parser.

Choose the response that tells the client what to do

Suppose a client creates an order. If its JSON ends in a trailing comma and the server cannot parse the request, respond with 400. If the JSON parses and the server recognizes the content type, but the submitted quantity is zero and the contract requires a positive quantity, 422 can distinguish a field validation failure from a malformed request. Some APIs document 400 for both cases. The important decision is to use a consistent contract so the client can tell whether to fix the wire representation or highlight an invalid field in a form.

An absent or invalid access token calls for a 401 response and an appropriate WWW-Authenticate challenge. A valid token lacking permission to create an order normally calls for 403. A refresh flow may help the first request. Retrying a forbidden action with the same identity and permissions will not fix it. The client can ask the user to switch accounts or request access. Where disclosing the resource would itself reveal sensitive information, a server can choose 404 to conceal its existence rather than exposing an authorization distinction to that caller.

Choose 404 when the server cannot find a current representation or will not disclose whether it exists. Use 410 when the server knows a resource used to exist and intends to tell clients it is gone, such as a retired public endpoint with no replacement. Do not send 410 for every database miss. A record may appear later, or the caller may have misspelled an identifier. For a documented endpoint retirement, tell clients how to migrate in the error detail if an alternative exists. If the resource moved to a permanent replacement, consider a redirect instead and check how the client handles its method.

Fix a validation error that arrived as 200

One common failure is a server that catches a validator exception and returns HTTP 200 with a field named error. The UI may show a message if its client knows that convention, while middleware, status metrics, and other clients record success. Change the response status and send a machine-readable body. RFC 9457 supersedes RFC 7807 and defines the problem details representation. This sample contract adds an errors extension with a JSON Pointer to the invalid field.

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

{
  "type": "https://api.example.com/problems/order-validation",
  "title": "Order validation failed",
  "status": 422,
  "detail": "At least one order item has an invalid quantity.",
  "instance": "/problem-instances/request-42",
  "errors": [
    {"pointer": "/items/0/quantity", "message": "Must be at least 1."}
  ]
}

Use the actual response status for HTTP behavior and keep the body's status consistent with it. Clients that understand this problem type can map the pointer to the field and retain the rest of the form. Other clients can still handle 422 and present the title. Keep type stable when wording changes. A client that matches an English sentence will break when the server improves or translates that sentence. A stable type can be part of a versioned API contract, while detail can explain the individual occurrence.

Keep diagnostic information appropriate for the caller. An instance identifier can help support correlate a request without exposing a stack trace, connection details, or a copy of the order. A client should tolerate an HTML response from an intermediary even when the API usually produces problem JSON. Check Content-Type before parsing, and retain the response status when parsing fails.

Retry 429 and 503 according to the request

A server can use 429 when a client has exceeded a request budget. It can use 503 when a service is temporarily unavailable, for example during maintenance or overload. Both can carry Retry-After, whose value can be a delay in seconds or an HTTP date. These are two example responses to inspect in HTTP Header Parser.

HTTP/1.1 429 Too Many Requests
Retry-After: 30
Content-Length: 0
HTTP/1.1 503 Service Unavailable
Retry-After: Wed, 21 Oct 2026 07:28:00 GMT
Content-Length: 0

For the first response, wait at least thirty seconds before retrying that request. For a date value, calculate the delay relative to the current time and account for clock skew. A date already in the past is not permission to create a tight retry loop. If the header is absent or unusable, apply a bounded backoff with jitter and a total retry budget. If several requests share a rate limit, coordinate their retries rather than scheduling all of them for the same instant.

Before retrying a write, establish whether repeating it is safe. A dropped connection can leave the client unsure whether the server committed the operation. A create request can use an idempotency key if the API supports it, or the client may need to query the resource state before trying again. Never retry every 4xx or 5xx automatically. The status explains one part of the failure, and the method, operation contract, and client deadline determine the next action. Compare the status definitions in HTTP Status Codes when documenting that behavior.

Found this guide helpful?

Explore our 50+ privacy-first developer utility tools.

Explore Tools