Harkirat Chahal
Growth
Share this article
Harkirat Chahal
Growth
Share this article

OpenAPI examples show developers what valid requests and responses look like, but they drift when schemas change, payloads are copied across pages, or SDK samples live outside the spec. This guide explains how the example and examples fields work and four ways to keep examples accurate: generate them from the schema, reuse shared examples, validate them, and update them in the same pull request as the spec.
Suppose your team updates POST /invoices to require a currency field. The specification is updated, but the quickstart guide still shows a request without currency, so developers who copy it get a validation error even though the API reference is correct.
That's the challenge with OpenAPI examples. They show what valid requests and responses look like, so developers can understand field values and construct working requests, but they're hard to keep accurate as the API changes.
The OpenAPI Specification is a standard for describing HTTP APIs in JSON or YAML. It defines an API's endpoints, authentication requirements, parameters, request bodies, responses, and data structures. But structure doesn't show developers how to use an API. Examples do.
This guide explains how OpenAPI examples work, why they become outdated, and how to keep them consistent with your API.
Key points on OpenAPI examples
- OpenAPI supports examples for parameters, request bodies, responses, and schemas. The
exampleandexamplesfields serve different purposes depending on where they're defined. - OpenAPI examples become outdated when schemas change, payloads are copied across documentation pages, or SDK examples are maintained separately from the specification.
- Keep OpenAPI examples accurate by automatically generating them from schemas where appropriate, reusing shared examples, validating them against the schema and API, and updating them alongside API specification changes.
How examples work in OpenAPI
An OpenAPI example supplies sample request and response values to help developers understand what data an endpoint accepts or returns.
For instance, an invoice schema might require an amount integer and a currency string. Those definitions specify the data types but don't explain whether 4500 represents $45.00 or $4,500, or whether developers should send USD or US Dollar. Examples provide that missing context.
OpenAPI supports examples in several locations, depending on what developers need to understand.
| Location | Purpose |
|---|---|
| Parameters | Show valid path, query, header, or cookie values |
| Request bodies | Show the data developers should send |
| Responses | Show what an endpoint returns for a given status code |
| Schemas | Provide representative values for fields or data models |
Documentation and API tools can use the examples directly. Documentation generators like Swagger UI can display them in API references, while mock servers such as Prism can use response examples to simulate API behavior, allowing teams to test client integrations before the API implementation is available.
An outdated example can therefore mislead both developers reading the docs and tools consuming the specification.
For example, in the invoice endpoint, the request schema defines customer_id, amount, and currency as required fields. A request example shows how to combine them:
paths:
/invoices:
post:
requestBody:
required: true
content:
application/json:
schema:
type: object
required:
- customer_id
- amount
- currency
properties:
customer_id:
type: string
amount:
type: integer
currency:
type: string
example:
customer_id: cus_123
amount: 4500
currency: USD
This is an excerpt from an OpenAPI document. The schema defines which fields are accepted and their data types. The example shows a sample request that satisfies those requirements, so developers can construct a request without interpreting each field definition separately.
When to use example vs. examples
Use example when one sample value is enough, and examples when an endpoint needs several named cases. An invoice API might accept different currencies or return different invoice states, and a separate example for each shows those variations.
At the media-type level, the entry for a format such as application/json, the two fields work like this:
exampledefines a single sample valueexamplesdefines multiple named examples, each with its own value and optional description
To show more than one case, replace the single request example above with:
examples:
usd:
summary: Invoice in US dollars
value:
customer_id: cus_123
amount: 4500
currency: USD
eur:
summary: Invoice in euros
value:
customer_id: cus_456
amount: 4500
currency: EUR
The OpenAPI specification makes example and examples mutually exclusive at this level. Use one or the other.
There is one important distinction when defining examples inside a schema. OpenAPI 3.0 uses the singular example field in schemas, while OpenAPI 3.1 and later support JSON Schema's list-based examples keyword.
currency:
type: string
examples:
- USD
- EUR
Here, USD and EUR are simply two sample values for the currency field. They are not complete invoice requests, and they don't restrict which currencies the API accepts.
This differs from the earlier examples block, where USD and EUR identify two complete request examples, each containing its own value and description.
Although both use the keyword examples, the schema-level version provides a list of sample values, while the request-level version provides named examples of a complete payload.
Why OpenAPI examples drift from the spec
An OpenAPI example becomes outdated when it no longer matches the API contract it describes. That happens in three ways.
The schema changes, but the example doesn't
Using the same example from above: After currency becomes required, the API implementation and OpenAPI schema are updated, but a manually written request example can still omit it.
The specification now contains conflicting information: Its schema says currency is required, while its example shows a request without it.
The same problem occurs when fields are renamed, data types change, enum values are removed, or response structures are updated.
Developers relying on those examples may encounter errors even when the schema accurately describes the API.
The same example is copied across multiple pages
An invoice request might appear in the API reference, quickstart, billing guide, and an integration tutorial.
If each page contains a separate copy, the team must update every copy whenever the request changes. One missed page is enough to leave inconsistent documentation.
This creates unnecessary maintenance work and makes it harder to determine which example reflects the current API contract.
Guide and SDK examples live outside the OpenAPI specification
Some examples are written in documentation files rather than defined in OpenAPI.
For instance, a Python quickstart might show developers how to create an invoice using the company's SDK:
invoice = client.invoices.create(
customer_id="cus_123",
amount=4500
)
When the API starts requiring currency, this example also needs to change. However, updating the OpenAPI schema doesn't automatically update the Python code in the quickstart. Developers following the old example may receive an error because the required argument is missing.
The same problem affects tutorials and SDK guides maintained separately from the specification. Even when the generated API reference is current, these examples must still be reviewed and updated.
How to keep OpenAPI examples accurate
OpenAPI examples come from two places: docs tools that generate sample payloads from OpenAPI schemas, and technical writers or developers who write them manually. Whichever you use, an example should be compatible with its schema, because readers copy examples into their own requests. The OpenAPI specification says documentation tooling may validate that compatibility, but nothing requires it, so a mismatched example can ship unnoticed.
Four practices reduce the number of independently maintained examples and catch inconsistencies before developers encounter them.
1. Generate examples from the schema where you can
Use schema-generated examples for routine structures, and maintain explicit examples for workflows that need meaningful values, such as declined payments or pagination.
Manually maintaining every sample payload creates unnecessary duplication. For simple endpoints, a documentation generator can use the schema to construct representative requests and responses.
When the schema changes, regenerated examples can reflect the updated fields and data types.
For example, if currency becomes required in the invoice schema, a generator that uses required properties can include it in newly generated requests.
The limitation is that generated values are generic placeholders. A value can satisfy the schema and still break a business rule, such as a placeholder string where the API expects a supported currency code.
2. Write shared examples once and reuse them everywhere
Define an example once using OpenAPI components.examples and reference it with $ref, a pointer to a definition elsewhere in the file, from parameters, request bodies, and responses. Changing the component fixes every operation that points to it.
When the same example appears in several places, every copy becomes another potential source of outdated information.
You can define a response example once under:
components:
examples:
PaidInvoice:
summary: Paid invoice
value:
id: inv_123
status: paid
amount: 4500
currency: USD
Then reference it from an endpoint:
examples:
paid:
$ref: '#/components/examples/PaidInvoice'
The reference points to the shared example rather than maintaining another copy of the payload.
For examples outside OpenAPI, use your docs platform's reusable content features. The principle is the same: Update the original, and every location that references it receives the change.
3. Test examples against the schema and the API
Perform checks that validate these three scenarios:
Validate the OpenAPI document. Check that the specification is structurally valid and its references resolve.
Validate example payloads against their schemas. Confirm that required fields, data types, and allowed values match the contract. For instance, a request without currency may be syntactically valid JSON but fail against a schema that requires the field. Tools such as Spectral provide rules for checking OpenAPI examples.
Test representative examples against the API. Where practical, execute requests in a test environment and verify the responses. For example, an invoice request might contain every required field but reference a customer that doesn't exist.
Each check catches a different failure: a broken document, an example that contradicts its schema, and an example that passes the schema but fails against the live API. Together they keep those examples from reaching readers, who copy them into their own requests and hit the error.
4. Update examples in the same pull request as the spec
Make example maintenance part of the API change review. Checking for affected examples during the same pull request makes omissions easier to catch before publication. When developers modify an endpoint, the same pull request should identify and update affected examples.
If docs updates happen after an API change ships, outdated examples can remain visible until someone notices them.
A practical workflow is to:
- Identify the endpoints, parameters, and schemas that changed.
- Find examples in the OpenAPI specification that depend on them.
- Search guides, SDK documentation, and shared snippets for related examples.
- Update the affected content and run validation checks.
- Review the API and docs changes together.
Add the validation checks from practice 3 to continuous integration (CI), and every pull request runs them automatically, failing the build when an example breaks.
Also read: Our recommendations for creating API documentation with examples
Keep API examples accurate with Mintlify
Mintlify helps you keep OpenAPI examples accurate as your API changes. It generates API reference pages from your OpenAPI specification, lets you reuse examples across documentation, and uses the Mintlify agent to search your docs and revise code examples and API references.
- Generate endpoint examples from your spec: When you connect an OpenAPI specification to Mintlify, Mintlify uses it to generate endpoint reference pages, request examples, and an interactive API playground. When the specification changes, the pages update with it, so you don't maintain request fields and response structures in handwritten pages. To add your own SDK or CLI samples, use
x-codeSamples. - Reuse examples with snippets: Store a payload once in a reusable snippet, such as
snippets/invoice-response.mdx, and import it into every page that needs it. When the snippet changes, each page that imports it shows the update. - Find outdated examples with the Mintlify agent: The Mintlify agent searches your docs for code examples and API references and revises them. After you add
currencyto the invoice endpoint, ask it to find the related request examples and update them. On your own Git provider, it opens a pull request for each change by default, so your team reviews it before it ships.
Together, these reduce the number of places an example can drift as your API evolves.
Start using Mintlify for free to build API docs from your OpenAPI specification.
Frequently Asked Questions
Where can you find good example OpenAPI specs?
The official OpenAPI Specification includes examples of endpoints, schemas, parameters, and responses.
You can also explore the Swagger Petstore project for a sample API definition. Choose examples that match your OpenAPI version. Syntax that works in OpenAPI 3.1 may not work in 3.0.
Can OpenAPI generate examples automatically?
OpenAPI defines the structure and locations for examples but doesn't generate them itself. Documentation tools do that, and generated samples depend on your schema's constraints, so well-typed schemas produce better ones.
Should OpenAPI examples use realistic or placeholder data?
Use realistic, synthetic data that satisfies the schema. Use placeholders only for values developers must supply themselves, such as API keys, and keep real customer data and live keys out of your spec.
How do you show multiple examples for one response?
Use examples instead of example on the response's media type, and give each example a unique name and a summary. A 200 response can hold named paid_invoice and open_invoice examples, each with its own value. You can also reference reusable Example Objects under components.examples, as shown above, instead of repeating the payload.
More to read

API deprecation guide: timelines, notices, and migration docs
Learn how to deprecate an API without breaking customers. Set a timeline, communicate changes, and write a migration guide that makes switching easy.
October 16, 2026Harkirat Chahal
Growth

SDK documentation best practices: from quickstart to migration guides
Learn how to write SDK documentation with installation guides, authentication examples, troubleshooting steps, and migration guides that stay up to date.
October 16, 2026Harkirat Chahal
Growth

What is an API playground? How interactive API documentation works
Learn how API playgrounds turn OpenAPI specifications into interactive documentation, with live requests, authentication, responses, and code examples.
September 7, 2026Harkirat Chahal
Growth

Redocly alternatives: 6 better OpenAPI documentation tools in 2026
Six Redocly alternatives compared on OpenAPI ingestion, SDK support, hosting, AI readiness, and pricing, with guidance for your API documentation program.
July 31, 2026Harkirat Chahal
Growth

How to write API documentation: a step-by-step guide
A step-by-step guide to planning, writing, testing, and maintaining API documentation, from authentication and quickstarts to references and changelogs.
July 23, 2026Harkirat Chahal
Growth

OpenAPI vs Swagger: what's the difference between the two specs?
Swagger 2.0 and OpenAPI 2.0 are the same spec. Learn how Swagger became OpenAPI, how the versions differ, and which one to use for a new API.
July 23, 2026Harkirat Chahal
Growth