Writing clear and comprehensive OpenAPI documentation is critical for developer adoption and API longevity. Below are best practices for versioning, model management, and endpoint documentation.
Models (schemas) define the structure of request/response data. Consistency here reduces confusion.
Leverage JSON Schema: Use OpenAPI’s schema syntax to enforce validation.
components:
schemas:
User:
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
example: "Jane Doe"
components
to avoid duplication.Provide Examples: Include sample data for clarity.
examples:
UserExample:
value:
id: 1
name: "Jane Doe"
Document Enums and Constraints: Specify allowed values, formats, and limits.
status:
type: string
enum: [active, inactive, pending]
minLength: 4
maxLength: 8
UserV2
) for breaking changes.Clear endpoint docs help developers integrate quickly.
Summarize and Describe:
/users/{id}:
get:
summary: Get a user by ID
description: Retrieves a single user's details, including name and contact info.
parameters:
- in: path
name: id
required: true
schema:
type: integer
example: 42
responses:
200:
content:
application/json:
schema:
$ref: "#/components/schemas/User"
404:
description: User not found
Use Tags for Grouping: Organize endpoints by functionality (e.g., Users
, Auth
).
tags:
- name: Users
description: Manage user accounts
Show Request/Response Examples:
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/User"
examples:
UserExample:
value:
name: "Jane Doe"
Standardize Error Formats:
components:
schemas:
Error:
type: object
properties:
code:
type: integer
message:
type: string
Good question, cowboy. We use serverless.yml to manage API declarations and deployments to APIGateway. So, it made sense to add a block for endpoint documentation.
All documentation is added in the docs
folder. The yaml file follows the OpenAPI standards. Read about OpenAPI spec here openAPI
Next, edit serverless open serverless.yml
in be-api-test
serverless
Under functions, see the documentation block documentation: ${file(./docs/api/documentation.yml):endpoints.api-endpoint}
Make sure you declare the documentation block for every endpoint.
Well-documented APIs reduce support overhead and empower developers to build faster. 📘🚀