Create API Documentation

Category: Documentation October 1, 2025 Source: awesome-copilot

Generate comprehensive API documentation with endpoints, examples, and best practices for API consumers.

APIDocumentationRESTOpenAPI
# Create API Documentation

Generate comprehensive, developer-friendly API documentation following OpenAPI/Swagger standards and best practices.

## Documentation Sections

### 1. API Overview

**Introduction**
- API purpose and capabilities
- Base URL and versioning strategy
- Supported formats (JSON, XML, etc.)
- Rate limiting and quotas
- API stability and deprecation policy

**Getting Started**
- Prerequisites
- Authentication setup
- Making your first request
- Quick start examples
- SDKs and client libraries

### 2. Authentication

**Authentication Methods**
- API Keys
- OAuth 2.0 / Bearer tokens
- JWT tokens
- Basic Auth
- Custom authentication schemes

**Authentication Details**

Authorization: Bearer


**Security Best Practices**
- Token storage
- Token rotation
- Scope and permissions
- Rate limiting per token

### 3. API Endpoints

For each endpoint, document:

**Endpoint Format:**

POST /api/v1/users

Creates a new user in the system.

Authentication Required: Yes Rate Limit: 100 requests/hour

Request Headers

  • Content-Type: application/json
  • Authorization: Bearer {token}

Request Body

{
  "email": "user@example.com",
  "name": "John Doe",
  "role": "user"
}

Request Parameters

ParameterTypeRequiredDescription
emailstringYesUser’s email address
namestringYesUser’s full name
rolestringNoUser role (default: user)

Response (201 Created)

{
  "id": "user-123",
  "email": "user@example.com",
  "name": "John Doe",
  "role": "user",
  "createdAt": "2025-10-01T12:00:00Z"
}

Response Codes

CodeDescription
201User created successfully
400Invalid request body
401Unauthorized
409User already exists
429Rate limit exceeded
500Internal server error

Error Response

{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "Invalid email format",
    "field": "email"
  }
}

Code Examples

cURL

curl -X POST https://api.example.com/api/v1/users \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "email": "user@example.com",
    "name": "John Doe"
  }'

JavaScript/TypeScript

const response = await fetch('https://api.example.com/api/v1/users', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${token}`
  },
  body: JSON.stringify({
    email: 'user@example.com',
    name: 'John Doe'
  })
});
const data = await response.json();

Python

import requests

headers = {
    'Content-Type': 'application/json',
    'Authorization': f'Bearer {token}'
}
data = {
    'email': 'user@example.com',
    'name': 'John Doe'
}
response = requests.post(
    'https://api.example.com/api/v1/users',
    headers=headers,
    json=data
)

### 4. Data Models

**Schema Definitions**

Define all data models used in requests and responses:

User Model

FieldTypeDescription
idstringUnique identifier
emailstringEmail address
namestringFull name
rolestringUser role (user, admin)
createdAtdatetimeCreation timestamp
updatedAtdatetimeLast update timestamp

### 5. Error Handling

**Error Response Structure**
```json
{
  "error": {
    "code": "ERROR_CODE",
    "message": "Human-readable error message",
    "field": "field_name",
    "details": {}
  }
}

Common Error Codes

  • VALIDATION_ERROR: Invalid input data
  • AUTHENTICATION_ERROR: Invalid or missing authentication
  • AUTHORIZATION_ERROR: Insufficient permissions
  • NOT_FOUND: Resource not found
  • RATE_LIMIT_EXCEEDED: Too many requests
  • SERVER_ERROR: Internal server error

6. Pagination

Pagination Pattern

GET /api/v1/users?page=1&limit=20

Response Format

{
  "data": [...],
  "pagination": {
    "page": 1,
    "limit": 20,
    "total": 150,
    "totalPages": 8
  }
}

7. Filtering and Sorting

Filter Parameters

GET /api/v1/users?role=admin&status=active

Sort Parameters

GET /api/v1/users?sort=createdAt&order=desc

8. Webhooks

Webhook Events

  • Event types
  • Payload structure
  • Signature verification
  • Retry logic

9. Rate Limiting

Rate Limit Headers

X-RateLimit-Limit: 100
X-RateLimit-Remaining: 95
X-RateLimit-Reset: 1633046400

10. Versioning

Versioning Strategy

  • URL-based: /api/v1/, /api/v2/
  • Header-based: Accept: application/vnd.api+json; version=1
  • Deprecation timeline
  • Migration guides

Best Practices

Documentation Quality

  • Use clear, consistent terminology
  • Provide realistic examples
  • Include error scenarios
  • Keep documentation up-to-date with code
  • Use OpenAPI/Swagger spec when possible

API Design

  • Follow RESTful conventions
  • Use appropriate HTTP methods
  • Return meaningful status codes
  • Implement proper error messages
  • Version your API
  • Support pagination for list endpoints
  • Use consistent naming conventions
  • Provide filtering and sorting options

Developer Experience

  • Interactive API explorer (Swagger UI)
  • Postman collection
  • SDK code examples
  • Sandbox environment
  • API changelog
  • Migration guides
  • Support channels

Output Format

Create documentation in Markdown format that can be:

  • Hosted on docs site
  • Converted to OpenAPI/Swagger spec
  • Used to generate interactive documentation
  • Integrated into developer portal