API Documentation Standards
In This Document
API Documentation Purpose
Comprehensive API documentation serves several critical purposes:
- Developer Usability: Makes APIs easy to understand and use
- Consistency: Ensures consistent API design and usage
- Onboarding: Accelerates new developer productivity
- Testing: Provides a reference for test development
- Integration: Simplifies integration with external systems
API Documentation Requirements
OpenAPI/Swagger Documentation
All REST APIs must be documented using OpenAPI/Swagger:
- Use OpenAPI 3.0 or later specification
- Document all endpoints, including internal ones
- Generate interactive documentation for testing
- Include the specification in the codebase
- Keep documentation in sync with implementation
Required Information for Each Endpoint
Every API endpoint must document:
- Basic Information
- Request Details
- Response Details
- Security Information
Basic Endpoint Information
- URL Path: The full path including version (e.g.,
/api/v3/users) - HTTP Method: GET, POST, PUT, DELETE, etc.
- Summary: Brief description of the endpoint's purpose
- Description: Detailed explanation of the endpoint's functionality
- Tags: Logical grouping of endpoints (e.g., "Users", "Products")
- Deprecated: Flag if the endpoint is deprecated, with migration path
Request Documentation
- Path Parameters: Parameters included in the URL path
- Query Parameters: Parameters appended to the URL
- Request Headers: Required and optional headers
- Request Body: Structure, format, and schema
- Content Types: Supported request formats (e.g., application/json)
- Examples: Sample request payloads
Response Documentation
- Status Codes: All possible response codes with descriptions
- Response Headers: Headers returned by the API
- Response Body: Structure, format, and schema for each status code
- Content Types: Supported response formats
- Examples: Sample response payloads for different scenarios
- Error Responses: Standardized error format and examples
Security Documentation
- Authentication: Required authentication method
- Authorization: Required permissions or scopes
- Rate Limiting: Limits on request frequency
- API Keys: How to obtain and use API keys
- Tokens: Token lifecycle and refresh mechanisms
API Documentation Format
OpenAPI Structure
Our OpenAPI documentation follows this structure:
openapi: 3.0.0
info:
title: API Name
description: Comprehensive description of the API
version: 1.0.0
servers:
- url: https://api.example.com/v3
description: Production server
- url: https://staging-api.example.com/v3
description: Staging server
paths:
/resource:
get:
summary: Brief description
description: Detailed description
parameters:
# Parameters definition
responses:
# Responses definition
components:
schemas:
# Reusable schemas
securitySchemes:
# Security definitions
Naming Conventions
- Endpoints: Use plural nouns for resources (e.g.,
/usersnot/user) - Parameters: Use camelCase for parameter names
- Schemas: Use PascalCase for schema names
- Properties: Use camelCase for property names
- Enums: Use PascalCase for enum names, UPPER_SNAKE_CASE for values
Documentation Tools
- Swagger UI: Interactive documentation for testing
- Redoc: User-friendly documentation for consumers
- Swagger Editor: For creating and editing OpenAPI specs
- Swagger Codegen: For generating client libraries
- Postman: For testing and documenting APIs
Documentation Process
Creating Documentation
- Start with API design before implementation
- Document endpoints as they are implemented
- Include documentation in pull requests
- Review documentation as part of code review
- Generate and publish updated documentation on merge
Maintaining Documentation
- Update documentation when API changes
- Version documentation alongside API versions
- Mark deprecated endpoints with migration paths
- Review documentation quarterly for accuracy
- Collect and incorporate user feedback
API Versioning
Our API versioning strategy:
- Use semantic versioning (MAJOR.MINOR.PATCH)
- Include version in URL path (e.g.,
/api/v3/users) - Document breaking changes between versions
- Maintain documentation for all supported versions
- Include deprecation notices and timelines
API Changelog
Maintain an API changelog that includes:
- Version number and release date
- New endpoints and features
- Modified endpoints with change details
- Deprecated endpoints with migration paths
- Breaking changes with migration guides
- Bug fixes related to API behavior
API Documentation Examples
Example Endpoint Documentation
/users/{id}:
get:
summary: Get user by ID
description: Retrieves a user by their unique identifier
parameters:
- name: id
in: path
required: true
schema:
type: integer
description: The user's unique identifier
responses:
'200':
description: Successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/User'
example:
id: 123
name: John Doe
email: john@example.com
created_at: "2023-01-15T14:30:00Z"
'404':
description: User not found
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
example:
code: 404
message: User not found
security:
- bearerAuth: []
Integration with Development Process
API documentation is integrated with our development process:
- Include API documentation requirements in task definitions
- Generate documentation from code annotations where possible
- Validate API documentation as part of CI/CD pipeline
- Test documented examples in automated tests
- Review API design before implementation