Agent Wranglers

API-3Recommended

Answer with the right status code

Pick the status code that matches what happened. Never answer 200 with an error inside the body.

How to do it

Follow the table below, from RFC 9110 and RFC 6585. Use 5xx only for faults on your side, never for bad input. When two codes could fit, pick the more specific one. Honest codes let the error rate count failures.

Examples

CodeWhen
200A read or update that returns a body
201A create, with a Location header pointing to the new resource
202Work accepted but not done yet
204Success with no body
400A body that does not parse, such as broken JSON, or a bad query or path parameter
401Credentials missing, expired or wrong
403Valid credentials that may not do this
404Unknown resource, or one the caller may not know exists
409Conflict with the current state, such as a duplicate
412An If-Match precondition failed
415A body in a media type you don't accept
422A body that parses but breaks the schema or a rule, such as an unknown field, a wrong type or a value out of range
429Too many requests
500An unexpected fault in your code
503Temporarily unavailable, with Retry-After

HTTP example

POST /orders HTTP/1.1
Content-Type: application/json

{"items":[{"sku":"TEA-01","quantity":2}]}

HTTP/1.1 201 Created
Location: /orders/ord_8f2k

How to check

A contract test sends a malformed body, a missing token, a token without access, an unknown ID and a valid create. It gets 400, 401, 403, 404 and 201 with Location, in that order.

Sources

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/resources-and-methods/status-codes">Agent Wranglers API design playbook</a>. In code you write, add a comment next to it with the same link.