Documenting APIs
1. Using OpenAPI Specification
| Aspect | OpenAPI 3.1 |
|---|---|
| Format | YAML / JSON |
| Schema | Full JSON Schema 2020-12 alignment |
| Webhook support | Native |
| Versioning | Per spec file |
| Tooling | Swagger UI, Redoc, Stoplight, Scalar |
2. Documenting Endpoints
Example: OpenAPI Path
paths:
/users/{userId}:
get:
summary: Retrieve user by ID
description: Returns full user profile if requester has access.
operationId: getUser
tags: [Users]
parameters:
- $ref: '#/components/parameters/UserId'
responses:
'200': { $ref: '#/components/responses/User' }
'404': { $ref: '#/components/responses/NotFound' }
3. Documenting Request Parameters
| Field | Required Doc |
|---|---|
| name, in (path/query/header/cookie) | Yes |
| required (true/false) | Yes |
| schema (type, format, enum, min/max) | Yes |
| example | Strongly recommended |
| description | Yes |
4. Documenting Request and Response Schemas
Example: Schema Component
components:
schemas:
User:
type: object
required: [id, email]
properties:
id: { type: string, format: uuid }
email: { type: string, format: email, example: "alice@example.com" }
role: { type: string, enum: [admin, user, guest] }
createdAt: { type: string, format: date-time, readOnly: true }
5. Providing Code Examples
| Language | Use |
|---|---|
| cURL | Universal baseline |
| JavaScript (fetch) | Frontend audience |
| Python (requests) | Data/scripting audience |
| Java/Go/Ruby | Backend integrators |
| SDK snippet | Preferred path |
6. Documenting Authentication
Example: Security Scheme
components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
oauth2:
type: oauth2
flows:
authorizationCode:
authorizationUrl: https://auth.example.com/authorize
tokenUrl: https://auth.example.com/token
scopes:
read:users: Read user data
write:users: Modify user data
security:
- bearerAuth: []
7. Documenting Error Responses
| Element | Document |
|---|---|
| Status code | Per response |
| Error code (machine-readable) | VALIDATION_FAILED, INSUFFICIENT_FUNDS |
| Schema | RFC 7807 Problem Details |
| Examples | Realistic error payloads |
| Recovery guidance | How clients should react |
8. Creating Interactive API Documentation
| Tool | Strength |
|---|---|
| Swagger UI | Try-it-out from spec |
| Redoc | Three-pane reference |
| Scalar | Modern, fast UX |
| Stoplight Elements | Embeddable |
| ReadMe / Mintlify | Hosted developer portals |
9. Documenting Rate Limits
| Doc | Content |
|---|---|
| Tier limits | Free: 100/min; Pro: 10k/min |
| Headers returned | RateLimit-Limit, -Remaining, -Reset |
| 429 behavior | Retry-After, exponential backoff |
| Per-endpoint overrides | List exceptions |
10. Providing Getting Started Guides
| Section | Content |
|---|---|
| Sign up / API key | Step-by-step |
| First request | cURL hello-world |
| SDK install | npm/pip/maven |
| Common workflow | End-to-end example |
| Sandbox / test mode | Safe experimentation |
11. Documenting Versioning
| Doc Element | Detail |
|---|---|
| Current version | Marked clearly |
| Supported versions list | With sunset dates |
| Changelog per version | Diff summary |
| Migration guides | v1 → v2 walkthrough |
| Deprecation policy | Lead time, communication |
12. Maintaining Documentation Up-to-Date
| Strategy | Benefit |
|---|---|
| Code-first generation | springdoc, FastAPI, NestJS Swagger module |
| Spec-first (design-first) | OpenAPI as source of truth, generate code |
| CI validation | Spec lint (Spectral), drift check |
| Contract tests | Detect implementation/spec mismatch |
| Reviewed in PRs | Doc changes alongside code |