OpenAPI Example: A Complete, Annotated Spec (YAML)
A complete OpenAPI 3.1 example in YAML for a small Bookings API, with pagination, a request body, enums, oneOf, allOf, security schemes and webhooks. Copy it, then read the section-by-section notes.
Morgan Kotter · Updated December 3, 2026
Most OpenAPI examples online are either a three-line fragment or the Petstore. This one is a small Bookings API in a single OpenAPI 3.1 document: a paginated list, create, read, update and delete, reusable parameters and errors, an enum, a nullable field, oneOf with a discriminator, allOf, two security schemes and a webhook.
The full spec comes first. Each section after it quotes a piece and explains it. The document passes redocly lint with the recommended ruleset and swagger-cli validate with no errors or warnings.
The complete OpenAPI 3.1 example
Save this as openapi.yaml.
openapi: 3.1.0
info:
title: Bookings API
version: 1.2.0
description: Book rooms and desks, then change or cancel the booking.
contact:
email: support@bookings.test
license:
name: MIT
identifier: MIT
servers:
- url: https://api.bookings.test/v1
description: Production
- url: https://sandbox.bookings.test/v1
description: Sandbox, test data only
tags:
- name: Bookings
description: Create, read, update and cancel bookings.
security:
- bearerAuth: []
- apiKeyAuth: []
paths:
/bookings:
get:
operationId: listBookings
summary: List bookings
tags: [Bookings]
parameters:
- name: status
in: query
description: Only return bookings with this status.
schema: {$ref: '#/components/schemas/BookingStatus'}
- $ref: '#/components/parameters/Limit'
- $ref: '#/components/parameters/Cursor'
responses:
'200':
description: A page of bookings.
content:
application/json:
schema: {$ref: '#/components/schemas/BookingList'}
'401':
$ref: '#/components/responses/Unauthorized'
post:
operationId: createBooking
summary: Create a booking
tags: [Bookings]
requestBody:
required: true
content:
application/json:
schema: {$ref: '#/components/schemas/NewBooking'}
example:
resourceId: room_42
startsAt: '2026-12-10T09:00:00Z'
endsAt: '2026-12-10T10:00:00Z'
notes: null
payment:
method: card
cardToken: tok_visa_4242
responses:
'201':
description: The booking was created.
content:
application/json:
schema: {$ref: '#/components/schemas/Booking'}
'422':
$ref: '#/components/responses/ValidationError'
/bookings/{bookingId}:
parameters:
- $ref: '#/components/parameters/BookingId'
get:
operationId: getBooking
summary: Get a booking
tags: [Bookings]
responses:
'200':
description: The booking.
content:
application/json:
schema: {$ref: '#/components/schemas/Booking'}
'404':
$ref: '#/components/responses/NotFound'
patch:
operationId: updateBooking
summary: Update a booking
tags: [Bookings]
requestBody:
required: true
content:
application/json:
schema: {$ref: '#/components/schemas/BookingUpdate'}
responses:
'200':
description: The updated booking.
content:
application/json:
schema: {$ref: '#/components/schemas/Booking'}
'404':
$ref: '#/components/responses/NotFound'
'422':
$ref: '#/components/responses/ValidationError'
delete:
operationId: cancelBooking
summary: Cancel a booking
tags: [Bookings]
responses:
'204':
description: The booking was canceled.
'404':
$ref: '#/components/responses/NotFound'
webhooks:
bookingCreated:
post:
operationId: onBookingCreated
summary: A booking was created
tags: [Bookings]
requestBody:
required: true
content:
application/json:
schema: {$ref: '#/components/schemas/Booking'}
responses:
'200':
description: Return 200 to acknowledge the event.
'400':
description: The receiver rejected the payload.
components:
securitySchemes:
bearerAuth: {type: http, scheme: bearer, bearerFormat: JWT}
apiKeyAuth: {type: apiKey, in: header, name: X-API-Key}
parameters:
BookingId:
name: bookingId
in: path
required: true
schema: {type: string, examples: [bkg_8f2c1a]}
Limit:
name: limit
in: query
schema: {type: integer, minimum: 1, maximum: 100, default: 20}
Cursor:
name: cursor
in: query
description: The nextCursor value from the previous page.
schema: {type: string}
responses:
Unauthorized:
description: Missing or invalid credentials.
content:
application/json:
schema: {$ref: '#/components/schemas/Error'}
NotFound:
description: No booking with that ID.
content:
application/json:
schema: {$ref: '#/components/schemas/Error'}
example: {code: not_found, message: Booking bkg_8f2c1a was not found}
ValidationError:
description: The request body failed validation.
content:
application/json:
schema: {$ref: '#/components/schemas/Error'}
example: {code: validation_failed, message: endsAt must be later than startsAt, field: endsAt}
schemas:
BookingStatus:
type: string
enum: [pending, confirmed, canceled]
NewBooking:
type: object
required: [resourceId, startsAt, endsAt, payment]
properties:
resourceId: {type: string, description: The room or desk to book.}
startsAt: {type: string, format: date-time}
endsAt: {type: string, format: date-time}
notes: {type: [string, 'null'], maxLength: 500}
payment: {$ref: '#/components/schemas/Payment', writeOnly: true}
Booking:
allOf:
- $ref: '#/components/schemas/NewBooking'
- type: object
required: [id, status, createdAt]
properties:
id: {type: string, readOnly: true}
status: {$ref: '#/components/schemas/BookingStatus'}
createdAt: {type: string, format: date-time, readOnly: true}
BookingUpdate:
type: object
minProperties: 1
properties:
startsAt: {type: string, format: date-time}
endsAt: {type: string, format: date-time}
notes: {type: [string, 'null'], maxLength: 500}
BookingList:
type: object
required: [data, nextCursor]
properties:
data: {type: array, items: {$ref: '#/components/schemas/Booking'}}
nextCursor:
type: [string, 'null']
description: Pass as cursor to get the next page. Null on the last page.
Payment:
oneOf:
- $ref: '#/components/schemas/CardPayment'
- $ref: '#/components/schemas/InvoicePayment'
discriminator:
propertyName: method
mapping:
card: '#/components/schemas/CardPayment'
invoice: '#/components/schemas/InvoicePayment'
CardPayment:
type: object
required: [method, cardToken]
properties:
method: {type: string, const: card}
cardToken: {type: string}
InvoicePayment:
type: object
required: [method, billingEmail]
properties:
method: {type: string, const: invoice}
billingEmail: {type: string, format: email}
poNumber: {type: string}
Error:
type: object
required: [code, message]
properties:
code: {type: string}
message: {type: string}
field: {type: string}
The API books rooms and desks, and calls the client back with a webhook when a booking is created.
info, servers and tags
openapi: 3.1.0
info:
title: Bookings API
version: 1.2.0
description: Book rooms and desks, then change or cancel the booking.
contact:
email: support@bookings.test
license:
name: MIT
identifier: MIT
servers:
- url: https://api.bookings.test/v1
description: Production
- url: https://sandbox.bookings.test/v1
description: Sandbox, test data only
tags:
- name: Bookings
description: Create, read, update and cancel bookings.
openapi: 3.1.0 tells tools which version of the specification to apply. Writing 3.0.3 here while using 3.1 keywords is a common source of confusing errors.
info.title and info.version are the only required fields in info, and version is your API's version, not OpenAPI's. contact and license are optional, but linters often warn when they are missing. license.identifier takes an SPDX code and is new in 3.1.
servers lists base URLs. Every path is appended to one, so GET /bookings on the first server is https://api.bookings.test/v1/bookings. A sandbox entry lets docs tools offer a server picker in the playground.
tags groups operations. Docs renderers use them to build the sidebar, and declaring the tag at the top level gives it a description.
Paths and operations
Each key under paths is a URL template, and each HTTP method under it is an operation. Every operation here has an operationId, a summary and tags. None are required, but code generators turn operationId into function names such as listBookings(), and docs use summary as the page title.
List endpoint with query parameters and pagination
/bookings:
get:
operationId: listBookings
summary: List bookings
tags: [Bookings]
parameters:
- name: status
in: query
description: Only return bookings with this status.
schema: {$ref: '#/components/schemas/BookingStatus'}
- $ref: '#/components/parameters/Limit'
- $ref: '#/components/parameters/Cursor'
responses:
'200':
description: A page of bookings.
content:
application/json:
schema: {$ref: '#/components/schemas/BookingList'}
'401':
$ref: '#/components/responses/Unauthorized'
The status parameter is defined inline. It reuses the BookingStatus schema, so the allowed values live in one place. limit and cursor are pulled from components/parameters because any future list endpoint will need the same two.
Pagination is cursor-based: the response contains data and nextCursor, and the client passes nextCursor back as cursor until it is null. Offset pagination is described the same way, with page and pageSize integer parameters.
Responses are keyed by status code, quoted as strings, and each needs a description. The 401 is a $ref to a reusable response.
OpenAPI requestBody example
post:
operationId: createBooking
summary: Create a booking
tags: [Bookings]
requestBody:
required: true
content:
application/json:
schema: {$ref: '#/components/schemas/NewBooking'}
example:
resourceId: room_42
startsAt: '2026-12-10T09:00:00Z'
endsAt: '2026-12-10T10:00:00Z'
notes: null
payment:
method: card
cardToken: tok_visa_4242
responses:
'201':
description: The booking was created.
content:
application/json:
schema: {$ref: '#/components/schemas/Booking'}
'422':
$ref: '#/components/responses/ValidationError'
requestBody replaced the Swagger 2.0 in: body parameter. It has three parts:
required: truesays the body cannot be omitted. The default isfalse, which surprises people.contentmaps media types to schemas. Addmultipart/form-datanext toapplication/jsonif the endpoint accepts it.examplesits on the media type, next toschema. Docs tools use it to fill in the playground and code samples. For several named examples, useexampleswith a map ofvalueobjects.
Path-level parameters
/bookings/{bookingId}:
parameters:
- $ref: '#/components/parameters/BookingId'
get:
operationId: getBooking
summary: Get a booking
tags: [Bookings]
responses:
'200':
description: The booking.
content:
application/json:
schema: {$ref: '#/components/schemas/Booking'}
'404':
$ref: '#/components/responses/NotFound'
bookingId is declared once under the path, so it applies to GET, PATCH and DELETE. An operation can override it by declaring a parameter with the same name and in. A path parameter must have required: true, and its name must match the {bookingId} placeholder exactly. DELETE returns 204 with no content because there is no body.
Reusable components
Everything under components is defined once and referenced with $ref. The Limit and Cursor parameters work the same way as the responses below.
responses:
Unauthorized:
description: Missing or invalid credentials.
content:
application/json:
schema: {$ref: '#/components/schemas/Error'}
NotFound:
description: No booking with that ID.
content:
application/json:
schema: {$ref: '#/components/schemas/Error'}
example: {code: not_found, message: Booking bkg_8f2c1a was not found}
ValidationError:
description: The request body failed validation.
content:
application/json:
schema: {$ref: '#/components/schemas/Error'}
example: {code: validation_failed, message: endsAt must be later than startsAt, field: endsAt}
Reusable responses are the part most specs skip. Without them, every operation repeats the error schema and the copies drift apart. Here the 404 for three operations is one definition, and its example is the sample error body in the docs.
Schemas
OpenAPI enum example
BookingStatus:
type: string
enum: [pending, confirmed, canceled]
An enum restricts a value to a fixed list. As a named schema, it is shared by the query parameter, the Booking schema and generated clients. A TypeScript generator turns it into "pending" | "confirmed" | "canceled".
Nullable fields in OpenAPI 3.1
NewBooking:
type: object
required: [resourceId, startsAt, endsAt, payment]
properties:
resourceId: {type: string, description: The room or desk to book.}
startsAt: {type: string, format: date-time}
endsAt: {type: string, format: date-time}
notes: {type: [string, 'null'], maxLength: 500}
payment: {$ref: '#/components/schemas/Payment', writeOnly: true}
notes uses type: [string, 'null']. In 3.1 a schema is a JSON Schema 2020-12 schema, and JSON Schema expresses "string or null" by listing both types. The quotes around 'null' matter: unquoted, YAML reads null as an actual null value rather than the string "null".
Nullable and optional are different. notes is not in required, so a client may omit it, and because it is nullable a client may also send "notes": null.
payment shows another 3.1 change: a $ref can have sibling keywords. writeOnly: true marks the payment details as something the client sends but the server never returns.
OpenAPI oneOf example with a discriminator
Payment:
oneOf:
- $ref: '#/components/schemas/CardPayment'
- $ref: '#/components/schemas/InvoicePayment'
discriminator:
propertyName: method
mapping:
card: '#/components/schemas/CardPayment'
invoice: '#/components/schemas/InvoicePayment'
CardPayment:
type: object
required: [method, cardToken]
properties:
method: {type: string, const: card}
cardToken: {type: string}
InvoicePayment:
type: object
required: [method, billingEmail]
properties:
method: {type: string, const: invoice}
billingEmail: {type: string, format: email}
poNumber: {type: string}
oneOf means the value must match exactly one of the listed schemas. A booking is paid either by card or by invoice, and each has different required fields.
The discriminator tells tools which property decides the variant, so they do not have to try each schema in turn. propertyName: method plus the mapping says: if method is card, use CardPayment; if it is invoice, use InvoicePayment. The discriminator property must be listed in required in every variant, which is why both schemas require method. const pins the value in each variant, so the schemas stay correct even for a validator that ignores the discriminator.
allOf for composition
Booking:
allOf:
- $ref: '#/components/schemas/NewBooking'
- type: object
required: [id, status, createdAt]
properties:
id: {type: string, readOnly: true}
status: {$ref: '#/components/schemas/BookingStatus'}
createdAt: {type: string, format: date-time, readOnly: true}
allOf means the value must match every listed schema. Booking is everything in NewBooking plus the fields the server sets: id, status and createdAt. The extra fields are readOnly, so docs show them in responses and leave them out of request examples.
allOf is composition, not inheritance. Generators usually turn it into an intersection type.
OpenAPI security scheme example
securitySchemes:
bearerAuth: {type: http, scheme: bearer, bearerFormat: JWT}
apiKeyAuth: {type: apiKey, in: header, name: X-API-Key}
securitySchemes defines how a client authenticates but does not turn anything on.
bearerAuthis HTTP bearer authentication. The client sendsAuthorization: Bearer <token>.bearerFormat: JWTis a hint for readers and tools; the server still has to validate the token.apiKeyAuthis an API key in a header namedX-API-Key.incan also bequeryorcookie.
The global security block applies them:
security:
- bearerAuth: []
- apiKeyAuth: []
Each item in the list is an alternative, so this reads "bearer token OR API key". To require both on the same request, put them in one item: - {bearerAuth: [], apiKeyAuth: []}. The empty arrays are scope lists, which only OAuth2 and OpenID Connect schemes use.
To make one operation public, give it security: []. An operation's own security block replaces the global one.
Webhooks
webhooks:
bookingCreated:
post:
operationId: onBookingCreated
summary: A booking was created
tags: [Bookings]
requestBody:
required: true
content:
application/json:
schema: {$ref: '#/components/schemas/Booking'}
responses:
'200':
description: Return 200 to acknowledge the event.
'400':
description: The receiver rejected the payload.
webhooks is a top-level field added in 3.1. Each entry describes a request your API sends to the client, not one the client sends to you. The request body is what the client will receive, and the responses are what the client should send back. bookingCreated is a name, not a URL, because the client registers its own URL somewhere outside this spec.
The same thing in OpenAPI 3.0
A few parts of this example only work in 3.1. If a tool in your pipeline still requires 3.0, change these, set openapi: 3.0.3, and the rest stays the same. The OpenAPI 3.1 vs 3.0 comparison covers the full list of differences.
Nullable fields use nullable: true instead of a type array:
notes:
type: string
nullable: true
maxLength: 500
Webhooks do not exist in 3.0. The closest feature is callbacks, which describes a request the server sends to a URL the client supplied in an earlier request. Webhooks registered out of band are often described with an x-webhooks extension instead.
The other 3.1-only pieces in this spec:
const: cardbecomesenum: [card].examples: [bkg_8f2c1a]inside a schema becomesexample: bkg_8f2c1a.license.identifierdoes not exist. Uselicense.url.- Keywords next to a
$refare ignored in 3.0. Wrap it instead:allOf: [{$ref: '#/components/schemas/Payment'}]withwriteOnly: truebeside theallOf.
If you are coming from a Swagger 2.0 file rather than writing from scratch, convert it first with the Swagger 2 to OpenAPI 3 converter, then apply the changes above to move it to 3.1. OpenAPI vs Swagger explains how the names and versions relate.
Validate and preview it
Validate any edited copy before you build on it.
- The OpenAPI Validator checks the document against the OpenAPI schema for 2.0, 3.0 and 3.1, resolves internal
$refs, and shows the version and the number of paths, operations and schemas it found. It runs in the browser and does not fetch external files, so bundle a multi-file spec first. - The OpenAPI Linter checks what validation allows but tools need: operationIds that are present and unique, a summary on every operation, tags, a 2xx and a 4xx response, and an
infodescription and contact. This example is written to pass those rules. - The OpenAPI Viewer lists every operation with its method, path, summary and operationId, which is a quick way to confirm nothing went missing.
- The OpenAPI to TypeScript converter writes a type for each schema.
Paymentcomes out as a union ofCardPaymentandInvoicePayment, andBookingas an intersection.
If validation fails with an error you do not recognize, OpenAPI validation errors explained goes through the common ones and how to fix each.
To see what this spec looks like as published documentation, you can import it into Specway. It becomes a docs site with a page per endpoint, a request playground, code samples and the webhook listed alongside the REST operations. Create a free account and paste the YAML in.
Morgan Kotter is a product manager and indie founder in Utah. He spent seven years forward-deployed in enterprise SaaS at Lucid, now Marq, and he builds and runs Specway himself.