# API Documentation

> Document REST APIs and GraphQL schemas with examples

## Introduction

Create comprehensive API documentation that developers actually want to read. Document REST APIs, GraphQL schemas, authentication flows, and endpoint specifications using Markdown, Mermaid diagrams, and AI assistance.

Modern API documentation needs to be more than just a list of endpoints. It should include interactive examples, visual flows, and clear explanations that help developers integrate quickly and correctly.

## Why AutEng for API Documentation?

### Interactive Examples

Include code examples in multiple languages with syntax highlighting. Show request/response examples that developers can copy and use immediately.

### Visual API Flows

Use sequence diagrams to show authentication flows, request lifecycles, and error handling. Make complex APIs easy to understand at a glance.

### Always Up-to-Date

Version history tracks every change. Update docs as your API evolves and maintain accuracy with visual diffs.

## REST API Endpoints

Document your REST API with clear endpoint specifications including HTTP methods, URLs, parameters, and response formats.

```markdown

# GET /api/users/:id

Retrieve a user by ID.

## Request

\`\`\`http
GET /api/users/123
Authorization: Bearer <token>
\`\`\`

## Response

\`\`\`json
{
  "user_id": "123",
  "name": "John Doe",
  "email": "john@example.com",
  "created_at": "2025-01-01T00:00:00Z"
}
\`\`\`

## Status Codes

- **200 OK** - User found
- **401 Unauthorized** - Invalid or missing token
- **404 Not Found** - User does not exist

```

## GraphQL Schemas

Document GraphQL APIs with schema definitions, queries, mutations, and field-level documentation.

```markdown

# User Type

\`\`\`graphql
type User {
  id: ID!
  name: String!
  email: String!
  posts: [Post!]!
}
\`\`\`

## Query Example

\`\`\`graphql
query GetUser($id: ID!) {
  user(id: $id) {
    id
    name
    email
    posts {
      title
      content
    }
  }
}
\`\`\`

## Variables

\`\`\`json
{
  "id": "123"
}
\`\`\`

```

## Authentication Flows

Visualize authentication and authorization processes with sequence diagrams. Show OAuth flows, JWT token handling, and API key management.

```markdown

# OAuth 2.0 Authorization Code Flow

\`\`\`mermaid
sequenceDiagram
    participant User
    participant Client
    participant AuthServer
    participant API

    User->>Client: Click "Login"
    Client->>AuthServer: Redirect to /authorize
    AuthServer->>User: Show login page
    User->>AuthServer: Enter credentials
    AuthServer->>Client: Redirect with code
    Client->>AuthServer: POST /token with code
    AuthServer->>Client: Return access_token
    Client->>API: Request with Bearer token
    API->>Client: Return protected resource
\`\`\`

The authorization code flow is the most secure OAuth 2.0 flow for web applications.

```

## Error Handling

Document error responses, status codes, and troubleshooting guides to help developers handle edge cases.

```markdown

# Error Responses

All errors follow a consistent format:

\`\`\`json
{
  "error": {
    "code": "INVALID_REQUEST",
    "message": "The request is missing required parameters",
    "details": {
      "missing_fields": ["email", "password"]
    }
  }
}
\`\`\`

## Common Error Codes

| Code | Status | Description |
|------|--------|-------------|
| INVALID_REQUEST | 400 | Missing or invalid parameters |
| UNAUTHORIZED | 401 | Invalid or expired token |
| FORBIDDEN | 403 | Insufficient permissions |
| NOT_FOUND | 404 | Resource does not exist |
| RATE_LIMITED | 429 | Too many requests |

```

## Best Practices

- **Start with the basics:** Document endpoints, methods, and parameters before adding advanced features
- **Include real examples:** Show actual request/response payloads that developers can copy
- **Use sequence diagrams:** Visualize complex flows like authentication and multi-step processes
- **Document error cases:** Show what happens when things go wrong and how to handle it
- **Keep it updated:** Use version history to track changes as your API evolves
