Every API fails sometimes. What makes an API pleasant to integrate with is how it fails: a clear status code, a consistent body, a message a developer can act on, and a request ID support can look up.

Use status codes deliberately

SituationStatus
Malformed request400 Bad Request
Missing or invalid credentials401 Unauthorized
Authenticated but not allowed403 Forbidden
Resource does not exist404 Not Found
Conflict (duplicate, version mismatch, in progress)409 Conflict
Validation failed on a well-formed request422 Unprocessable Content
Rate limit exceeded429 Too Many Requests (with Retry-After)
Unexpected server failure500 Internal Server Error
Upstream failed502 Bad Gateway
Temporarily unavailable503 Service Unavailable (with Retry-After)
Upstream timed out504 Gateway Timeout

Never return 200 with an error inside the body for REST APIs; clients, proxies and monitoring all rely on the status code. GraphQL is the exception, and needs its own monitoring.

A consistent error body

{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "The request has 2 invalid fields.",
    "details": [
      { "field": "email", "issue": "must be a valid email address" },
      { "field": "quantity", "issue": "must be at least 1" }
    ],
    "requestId": "req_4a73f92f491b4c64"
  }
}
  • code: a stable, machine-readable identifier clients can switch on.
  • message: human-readable, safe to show to developers.
  • details: per-field problems for validation errors.
  • requestId: the ID support uses to find logs and traces.

The RFC 9457 "problem details" format (application/problem+json) is a standard alternative with type, title, status, detail and instance fields.

Tell clients whether to retry

Clients should retry 429, 503 and 504 (respecting Retry-After), sometimes 502, and never 4xx validation errors. Document this, and include Retry-After where it applies. Pair retryable POSTs with idempotency keys.

Never leak internals

Stack traces, SQL, internal host names and library error messages belong in your logs, not in responses. Return a generic message with the request ID for unexpected 500s, and log the details server-side with the same request ID and the trace ID. See 500 Internal Server Error.

Request IDs and traces

Generate a request ID at the edge (or reuse the trace ID), return it in a header and in error bodies, and log it. When a customer sends you a request ID, support can open the exact trace. See how to add trace IDs to logs.

Worked example

A B2B API returned 500 {"message":"Error"} for everything from validation problems to database outages. Customers retried validation errors in loops, and support could not find anything. The new design returned 422 with field details for validation, 409 for duplicates, 503 with Retry-After for maintenance, and a request ID on every error. Support tickets about "random errors" fell sharply, and retry traffic from integrations dropped because clients stopped retrying 4xx.

Monitoring errors

  • Error rate per endpoint and status class: 5xx are your problem; a 4xx spike may be a client release or an API change you made.
  • Error codes over time: a new code appearing after a deploy is a regression signal.
  • Top clients by errors: one integration failing repeatedly deserves outreach.

VitalRix APM shows status codes per endpoint from traces, error tracking groups exceptions behind 5xx responses, and API monitoring checks that error responses stay correct, for example that a missing resource returns 404 and not 500.

Common mistakes

  • 200 OK with an error body.
  • Different error formats per endpoint or team.
  • Messages that change wording, breaking clients that parse them instead of codes.
  • Leaking stack traces in production responses.

Documenting errors

List every error code your API can return, with the status code, meaning and whether clients should retry. Put the list in your API reference and keep it versioned: removing or renaming a code is a breaking change, just like removing a field. Include an example error body for each endpoint. Good documentation reduces support tickets more than any other change to error handling, because integrators can handle problems without asking. See also rate limiting strategies for how to document 429 responses.

Should validation errors be 400 or 422?

Both are used. A common split: 400 for malformed requests (bad JSON), 422 for well-formed requests that fail validation rules. Be consistent.

Should error messages be localized?

Keep the code stable and the message in English for developers; clients can map codes to localized messages for end users.

Is it safe to return the request ID?

Yes. It contains no sensitive data and lets support find the request quickly.

Try VitalRix free

Uptime monitoring, APM, logs, real user monitoring and status pages in one place. No credit card needed.