Status codes are half of an API contract
An OpenAPI document is only as honest as its responses object. A path that
returns 201 with a Location header but only documents 200 will mislead
every consumer and code generator. This reference maps the HTTP status codes you
actually return to the response schema, media type and headers OpenAPI 3.1
expects under each one.
How it works
Each status code carries semantics: some must have a body, some must not, and
some require specific headers. The table below pairs every common code with its
recommended OpenAPI media type (for example application/json for resources,
application/problem+json for errors per RFC 9457), the schema shape to declare,
and the headers worth documenting. Search by code or keyword to find the row.
A modern error convention is to define a single reusable component:
components:
schemas:
Problem:
type: object
properties:
type: { type: string, format: uri }
title: { type: string }
status: { type: integer }
detail: { type: string }
instance: { type: string }
Then reference it under every error status with $ref: '#/components/schemas/Problem'.
Tips and conventions
204and304carry no body — omit thecontentobject entirely.201should document aLocationheader pointing at the created resource.429and503should document aRetry-Afterheader.- Prefer enumerating concrete codes over relying solely on
default. - Keep error bodies consistent across the whole API using one component schema.