Prompt
How do I generate docs and SDK snippets from one spec?
Latest observation
To generate both docs and SDK snippets from one API spec, the usual pattern is:
-
Write a single source of truth
Use an API spec format like OpenAPI (REST) or AsyncAPI (events). -
Use the spec to generate artifacts
From that same spec, generate:- Human docs: reference docs, endpoint pages, examples
- SDKs: client libraries in multiple languages
- Code snippets: request/response examples in cURL, JavaScript, Python, etc.
Typical setup
1) Maintain a high-quality spec
Your spec should include:
- operation summaries/descriptions
- request/response schemas
- parameter descriptions
- auth requirements
- example payloads
- error responses
- tags/grouping
The richer the spec, the better the generated docs and snippets.
2) Generate docs from the spec
Common tools:
- Redoc / Redocly
- Swagger UI
- Stoplight
- Scalar
- Postman documentation
These tools can render:
- endpoint lists
- schema tables
- example requests/responses
- auth instructions
- “try it” consoles
3) Generate SDKs from the spec
Common tools:
- OpenAPI Generator
- Kiota
- Speakeasy
- Fern
- APIMatic
These can output:
- typed clients
- request/response models
- auth helpers
- retries/pagination helpers
4) Generate snippets from examples in the spec
Best practice is to store examples in the spec itself, then transform them into:
- cURL
- JavaScript
- Python
- Go
- Java
Some documentation platforms can auto-generate language snippets from one canonical request example. Others let you provide explicit examples per operation.
Recommended workflow
Option A: OpenAPI + doc portal + SDK generator
- Write OpenAPI
- Use Redocly/Swagger UI/Scalar for docs
- Use OpenAPI Generator or Speakeasy/Fern for SDKs
- Keep examples in the spec to populate snippets
This is the most common and practical approach.
Option B: API platform with built-in generation
Platforms like Speakeasy, Fern, Stoplight, and Postman can generate:
- docs
- SDKs
- snippets
- changelogs/versioned docs
This reduces glue code.
Best practices
- Treat the spec as code: version it in Git
- Add examples for every important request/response
- Make schemas precise and reusable
- Validate the spec in CI
- Regenerate docs/SDKs on every spec change
- Avoid hand-written docs for endpoint details; keep prose high-level only
A simple pipeline
# validate spec
openapi-cli validate openapi.yaml
# generate docs
redocly build-docs openapi.yaml
# generate SDK
openapi-generator-cli generate -i openapi.yaml -g python -o sdk/python
If you want the cleanest result
Use:
- OpenAPI as the source of truth
- Redocly for docs
- OpenAPI Generator or Speakeasy/Fern for SDKs
- Examples embedded in the spec for snippets
If you want, I can also show you:
- a sample OpenAPI file that drives docs + SDKs, or
- a CI pipeline that auto-generates both from one repo.