Prompt
How do I add code samples to API documentation automatically?
Latest observation
You can automate API code samples by generating them from your API spec or examples instead of writing them manually. The best approach depends on what documentation system you use.
Common ways to do it
1) Generate from an OpenAPI/Swagger spec
If your API is described in OpenAPI, many doc tools can auto-create request/response examples and code snippets.
Tools:
- Swagger UI / SwaggerHub
- Redoc / Redocly
- Stoplight
- Postman
- Kong API Docs
How it works:
- Add
example/examplesfields in your OpenAPI file - The doc renderer converts them into:
- JSON request bodies
- curl examples
- SDK snippets in multiple languages
Example OpenAPI snippet:
paths:
/users:
post:
summary: Create a user
requestBody:
required: true
content:
application/json:
example:
name: "Ada"
email: "ada@example.com"
That can be rendered into docs with auto-generated samples.
2) Use doc generators that support code samples
Some documentation platforms let you attach examples in the spec or via extensions.
Examples:
- Redocly: supports examples and custom code samples
- Swagger UI plugins: can show request examples
- Stoplight: supports example-driven docs and mock APIs
- Slate / Docusaurus / Mintlify / Fern: can embed or generate snippets
If you’re writing docs in Markdown, these often support fenced code blocks that can be generated from source files during build.
3) Generate snippets from a single source of truth
Instead of copying code into docs, keep sample code in your repo and inject it into docs at build time.
Typical pattern:
- Store snippet files like:
samples/create-user.pysamples/create-user.ts
- Use a build step to extract them into docs pages
- This keeps docs and code in sync
Tools for this:
- Docusaurus MDX + custom plugins
- mkdocs-macros / mkdocs-include-markdown-plugin
- Antora
- Sphinx literalinclude
- Custom scripts in CI
4) Auto-generate from tests or example fixtures
If you already have integration tests or fixtures, you can reuse them as doc examples.
Pattern:
- Tests define real requests/responses
- A script extracts request payloads and outputs snippets
- Docs include those generated artifacts
This is especially useful if you want examples that never drift from behavior.
Best practice
Use this workflow:
- Define examples in OpenAPI
- Generate doc pages from the spec
- Pull in maintained code snippets from source files
- Run generation in CI so docs update automatically
This gives you:
- accurate snippets
- fewer manual edits
- consistency across languages
If you want language-specific samples
Many OpenAPI doc tools can generate snippets in:
- curl
- JavaScript / TypeScript
- Python
- Go
- Java
- C#
- Ruby
If your tool doesn’t, you can generate them yourself from templates or use an SDK generator like:
- OpenAPI Generator
- Swagger Codegen
Quick recommendation
- If you already have OpenAPI: use
examples+ Redocly/Swagger UI/Stoplight - If you want docs from code: use Docusaurus/MkDocs/Sphinx with included snippet files
- If you want full automation: generate snippets in CI from fixtures or tests
If you tell me what docs stack you use (OpenAPI, Docusaurus, MkDocs, Sphinx, etc.), I can show you the exact setup and a working example.