API error payloads

Common JSON error response patterns (and why they help)

A consistent error shape makes it easier for clients to show good messages, retry safely, and debug issues.

1) Simple error object

{
  "error": {
    "code": "UNAUTHORIZED",
    "message": "Missing or invalid token"
  }
}

2) Include a request / trace ID

Trace IDs let support and SRE teams find the exact server-side logs.

{
  "error": {
    "code": "INTERNAL",
    "message": "Unexpected error"
  },
  "traceId": "b4f8f8e7d7d44b1a"
}

3) Field-level validation errors

Great for forms and API clients:

{
  "error": {
    "code": "VALIDATION_FAILED",
    "message": "Some fields are invalid",
    "details": [
      {"field": "email", "issue": "invalid_format"},
      {"field": "age", "issue": "must_be_greater_than_or_equal_to_18"}
    ]
  }
}

Best practices

Related: Privacy & safety · Escape JSON strings