The structure of an OpenAPI document, part by part
An OpenAPI document looks long, but it has only a handful of top-level parts, and each one answers a plain question about your API. Once you know the questions, any document becomes easy to read, and your own becomes easier to keep in order. This post goes through the parts with one small, complete example, and ends with the habits that keep a document maintainable as it grows.
The parts and the questions they answer
| Field | Question | Required |
|---|---|---|
openapi | Which version of the specification is this written in? | Yes |
info | What is this API called, which version is it, who do I contact? | Yes |
servers | Where does it run? | No, but without it tools assume the document's own address |
paths | What can I call, and what do I get back? | Yes in 3.0; in 3.1 a document may have paths, webhooks or components |
components | Which pieces are defined once and reused? | No |
security | What credential does a call need by default? | No |
tags | How are the operations grouped for a reader? | No |
externalDocs | Where is the longer documentation? | No |
A complete small document
openapi: 3.0.3
info:
title: Orders API
version: 1.2.0
description: Create and read orders of the Acme shop.
contact:
name: Acme API team
email: api@acme.example
servers:
- url: https://api.acme.example/v1
description: Production
tags:
- name: Orders
description: Everything about an order
security:
- apiKey: []
paths:
/orders/{orderId}:
get:
operationId: getOrder
summary: Get an order
description: Returns one order by its identifier.
tags: [Orders]
parameters:
- $ref: "#/components/parameters/OrderId"
responses:
"200":
description: The order
content:
application/json:
schema:
$ref: "#/components/schemas/Order"
example:
id: 6f1c0b1e-8a55-4c0e-9a5b-2f6f3f1d9b10
status: paid
total: 42.5
"404":
$ref: "#/components/responses/NotFound"
components:
parameters:
OrderId:
name: orderId
in: path
required: true
schema:
type: string
format: uuid
schemas:
Order:
type: object
required: [id, status, total]
properties:
id:
type: string
format: uuid
status:
type: string
enum: [open, paid, shipped, cancelled]
total:
type: number
minimum: 0
Error:
type: object
required: [message]
properties:
message:
type: string
responses:
NotFound:
description: No such order
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
securitySchemes:
apiKey:
type: apiKey
in: header
name: X-API-Key
About seventy lines, and every part is there. A few things worth noticing:
info.versionis the version of your API, not of the specification.openapiis the version of the specification.- A path holds operations, one per HTTP method. Each operation has its own parameters, an optional request body, and responses keyed by status code. Status codes are quoted because YAML would read
200as a number. operationIdis the name of the operation for machines. Code generators turn it into a function name and documentation tools into an address, so make it unique and stable.securityat the top is the default for every operation. An operation can replace it, andsecurity: []on an operation makes that one public.- The response has an
example. Examples are what documentation shows and what a mock server answers with, so they earn their keep twice.
How the parts refer to each other
The value of components is that nothing is written twice. The parameter, the schema and the error response above are defined once and pulled in with $ref. When the Error shape changes, it changes in one place.
A $ref can also point at another file, which is how large APIs are split: $ref: "./schemas/order.yaml". Tools that need a single file can bundle them again. Reusing OpenAPI components goes deeper into this.
Five habits that keep a document healthy
- Describe everything a reader meets. A summary on every operation, a description on every parameter and property. This is the difference between a reference and a list of URLs.
- Give every operation an
operationIdand a tag. Navigation, code generation and search all depend on them. - Declare the errors. A
4xxfor bad input and the errors a client must handle, with a shared error schema. - Constrain the data.
required,enum,format,minimum,maxLength. They document the contract, they make mocks realistic, and they are what security tooling reads. - Put examples in. One realistic example per response is worth more than a paragraph.
A linter checks most of this for you. In APIGit, the Governance tab applies the API Best Practice rule set while you edit, and apigit lint openapi.yaml does the same on your machine. See API governance in APIGit.
Writing it as a form
You do not have to type the YAML. In APIGit an OpenAPI file opens in a form where the same parts are sections: general information, servers, paths, and the components. Adding a path parameter means filling in its name, location and type. The Code view shows the file the form wrote, and you can switch between the two at any time.

The form is a good way to learn the structure: change a field, switch to Code, and see which lines moved.
Going further
- Composition with
oneOf,allOfandanyOf - Reusing components within a file and across files
- What a document says about security: OWASP API Top 10 for your OpenAPI document
- The API design documentation for the editor