API Architecture and Integration Design · Designing Robust APIs

Errors and Validation

Consistent, actionable error responses with Problem Details.

Use one error format everywhere — RFC 9457 Problem Details (type, title, status, detail, instance) plus extensions like field errors and a request ID. Validate input at the boundary and return all field errors at once. Distinguish client errors (4xx, fixable by the caller) from server errors (5xx), and never leak stack traces or SQL.

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

{
  "type": "https://api.example.com/problems/validation",
  "title": "Your request is invalid",
  "status": 422,
  "errors": [
    {"field": "items[0].qty", "message": "must be at least 1"},
    {"field": "currency", "message": "must be a 3-letter ISO code"}
  ],
  "request_id": "req_7f3a9c"
}

Common pitfalls

  • Different error shapes per endpoint.
  • Only returning the first validation error.
  • 500 for client mistakes (or 400 for server bugs).

Key points

  • Problem Details everywhere
  • All field errors at once
  • Request IDs for support

Further reading: RFC 9457: Problem Details

Practice questions

What's the value of a stable type URI in error responses?

Answer

Clients can programmatically recognize error categories without parsing human-readable messages, and the URI can document the problem.

Why include a request ID in errors?

Answer

Users and client developers can report it, letting support correlate the exact request in logs and traces.

Open in the app to mark lessons complete, take notes and track your progress.