Documenting APIs

1. Using OpenAPI Specification

AspectOpenAPI 3.1
FormatYAML / JSON
SchemaFull JSON Schema 2020-12 alignment
Webhook supportNative
VersioningPer spec file
ToolingSwagger 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

FieldRequired Doc
name, in (path/query/header/cookie)Yes
required (true/false)Yes
schema (type, format, enum, min/max)Yes
exampleStrongly recommended
descriptionYes

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

LanguageUse
cURLUniversal baseline
JavaScript (fetch)Frontend audience
Python (requests)Data/scripting audience
Java/Go/RubyBackend integrators
SDK snippetPreferred 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

ElementDocument
Status codePer response
Error code (machine-readable)VALIDATION_FAILED, INSUFFICIENT_FUNDS
SchemaRFC 7807 Problem Details
ExamplesRealistic error payloads
Recovery guidanceHow clients should react

8. Creating Interactive API Documentation

ToolStrength
Swagger UITry-it-out from spec
RedocThree-pane reference
ScalarModern, fast UX
Stoplight ElementsEmbeddable
ReadMe / MintlifyHosted developer portals

9. Documenting Rate Limits

DocContent
Tier limitsFree: 100/min; Pro: 10k/min
Headers returnedRateLimit-Limit, -Remaining, -Reset
429 behaviorRetry-After, exponential backoff
Per-endpoint overridesList exceptions

10. Providing Getting Started Guides

SectionContent
Sign up / API keyStep-by-step
First requestcURL hello-world
SDK installnpm/pip/maven
Common workflowEnd-to-end example
Sandbox / test modeSafe experimentation

11. Documenting Versioning

Doc ElementDetail
Current versionMarked clearly
Supported versions listWith sunset dates
Changelog per versionDiff summary
Migration guidesv1 → v2 walkthrough
Deprecation policyLead time, communication

12. Maintaining Documentation Up-to-Date

StrategyBenefit
Code-first generationspringdoc, FastAPI, NestJS Swagger module
Spec-first (design-first)OpenAPI as source of truth, generate code
CI validationSpec lint (Spectral), drift check
Contract testsDetect implementation/spec mismatch
Reviewed in PRsDoc changes alongside code