API-16Recommended
Send errors as problem details
Answer every 4xx and 5xx with an RFC 9457 problem details object, served as application/problem+json.
How to do it
Fill type with a URI that names the problem, plus title, status and detail. Add instance to point at this occurrence. Make status match the HTTP status code. Add your own fields for anything a client needs to act on. Endpoints that follow an outside protocol keep its error format, such as JSON-RPC errors for MCP.
Examples
HTTP example
HTTP/1.1 409 Conflict
Content-Type: application/problem+json
{
"type": "https://api.example.com/problems/out-of-stock",
"title": "Item out of stock",
"status": 409,
"detail": "Only 1 of TEA-01 is left, and the order asked for 2.",
"instance": "/orders/ord_8f2k"
}
JavaScript example
function sendProblem(response, { type, title, status, detail, ...extra }) {
response.status(status).type('application/problem+json')
.json({ type: `https://api.example.com/problems/${type}`, title, status, detail, ...extra })
}
Python example
from fastapi.responses import JSONResponse
def problem(type_: str, title: str, status: int, detail: str, **extra) -> JSONResponse:
body = {"type": f"https://api.example.com/problems/{type_}", "title": title,
"status": status, "detail": detail, **extra}
return JSONResponse(body, status_code=status, media_type="application/problem+json")
How to check
Trigger a 400, a 404 and a 409 in contract tests. Each has Content-Type: application/problem+json and the four fields, and status equals the response code.
Sources
Related rules
- ARC-26 Signal failure with an exception or a typed result instead of a code in Clean code and architecture
- ARC-27 Use typed domain exceptions in Clean code and architecture
- API-91 Return a result for each item in a batch in API design
- API-15 Stream in a standard format and describe each item in API design
- AIF-16 Return errors the model can act on in AI features
- APP-9 Show errors where the data should be in Web app interface
- DOC-12 Give every error code its own docs page in Documentation
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/api-design/errors/problem-details">Agent Wranglers API design playbook</a>. In code you write, add a comment next to it with the same link.