Skip to main content

Auto-generate production-quality API & WebSocket documentation (PDF, HTML, JSON/OpenAPI) for Django REST Framework with a single command

Project description

DRF API & WebSocket Documentation Generator

PyPI version Python Versions Django Versions License: MIT

Auto-generate production-quality API & WebSocket documentation for Django REST Framework with a single command! ๐Ÿš€

Stop manually writing API documentation! This tool automatically discovers your DRF endpoints, serializers, authentication, permissions, and WebSocket consumers, then generates beautiful documentation in PDF, HTML, or OpenAPI JSON format.


โœจ Features

REST API Documentation

  • ๐Ÿ“„ PDF Documentation - Professional multi-page PDF with cover page, TOC, and detailed endpoints
  • ๐ŸŒ HTML Documentation - Interactive docs with sidebar navigation and syntax highlighting
  • ๐Ÿ“‹ OpenAPI 3.0 JSON - Swagger UI and Postman compatible specification
  • ๐Ÿ” Auto-Discovery - Automatically finds all API endpoints from your URL patterns
  • ๐ŸŽฏ Serializer Extraction - Extracts field types, validations, and descriptions
  • ๐Ÿ” Auth Detection - Documents authentication and permission requirements

๐Ÿ”Œ WebSocket Documentation (NEW!)

  • ๐Ÿ“ก Consumer Parsing - Automatically parses Django Channels consumers
  • ๐Ÿ”„ Action Handlers - Documents all client-to-server actions
  • ๐Ÿ“จ Server Events - Documents server-to-client broadcast events
  • ๐Ÿ”— Connection Lifecycle - Documents connect/disconnect flow with error codes
  • ๐Ÿ’ก Example Messages - Auto-generates example request/response JSON
  • ๐ŸŽจ Syntax Highlighting - Beautiful code blocks with proper highlighting

๐Ÿ“ฆ Installation

pip install drf-api-doc-generator

๐Ÿš€ Quick Start

๐Ÿš€ Quick Share (Magic Command)

Generate complete documentation for all apps and WebSockets, zipped and ready to share:

python manage.py generate_api_docs complete-project-zip-html

This single command will:

  • Auto-discover all Django Apps
  • Auto-discover all consumers.py (WebSocket) files
  • Generate HTML documentation
  • Create a ZIP package for easy sharing

๐Ÿ“š Detailed Usage

1. Add to INSTALLED_APPS

# settings.py

INSTALLED_APPS = [
    # ... your other apps
    'rest_framework',
    'api_docs_generator',  # Add this line
]

2. Generate REST API Documentation

# Generate PDF for a specific app
python manage.py generate_api_docs auth

# Generate for multiple apps
python manage.py generate_api_docs auth users products

# Generate all formats (PDF + HTML + JSON)
python manage.py generate_api_docs auth --format all

# Generate for all apps
python manage.py generate_api_docs --all

3. Generate WebSocket Documentation

# Document a WebSocket consumer file
python manage.py generate_api_docs --websocket chat/consumers/dm_consumer.py

# Document multiple WebSocket files
python manage.py generate_api_docs --websocket chat/consumers/*.py

# Combine REST API and WebSocket documentation
python manage.py generate_api_docs auth --websocket chat/consumers/dm_consumer.py --format all

4. Find Your Docs

Documentation will be generated in ./api_docs/ directory:

  • api_docs_YYYYMMDD_HHMMSS.pdf
  • api_docs_YYYYMMDD_HHMMSS.html
  • api_docs_YYYYMMDD_HHMMSS.json

๐Ÿ“– Command Options

python manage.py generate_api_docs [OPTIONS] [APPS...]
Option Short Description
--all Generate docs for all installed apps
--websocket -ws Path(s) to WebSocket consumer files
--format -f Output format: pdf, html, json, or all (default: pdf)
--output -o Output directory (default: ./api_docs/)
--title Custom documentation title
--api-version API version string
--description API description
--ws-base-url WebSocket base URL (default: ws://domain)
--open Open file after generation
-v 2 Verbose output (show all endpoints)

Examples

# Generate PDF with custom title
python manage.py generate_api_docs auth --title "My Awesome API"

# Generate with WebSocket and custom base URL
python manage.py generate_api_docs --websocket chat/consumers.py --ws-base-url wss://api.myapp.com

# Generate to custom directory
python manage.py generate_api_docs auth --output ./docs/api/

# Generate and open automatically
python manage.py generate_api_docs auth --format html --open

# Verbose mode - show all discovered endpoints
python manage.py generate_api_docs auth -v 2

๐Ÿ”Œ WebSocket Documentation Format

The generated documentation for WebSocket consumers includes:

Connection Lifecycle

Client Action:

const ws = new WebSocket('ws://domain/ws/dm/123/?token=YOUR_JWT_TOKEN');

Server Response (on success):

{
  "type": "connection_established",
  "user_id": 123,
  "timestamp": "2025-01-15T10:30:00.000Z"
}

Client Actions

Each action includes:

  • Action name and description
  • Request schema/example
  • Response/broadcast example

Server Events

Events broadcast from server to clients:

  • Event type and description
  • Payload example

โš™๏ธ Configuration

Add optional configuration to your Django settings:

# settings.py

API_DOCS_CONFIG = {
    'TITLE': 'My API Documentation',
    'VERSION': '2.0.0',
    'DESCRIPTION': 'Complete API reference for developers',
    'CONTACT_EMAIL': 'api@example.com',
    'LOGO_PATH': 'path/to/logo.png',  # Optional logo for PDF cover
    'THEME_COLOR': '#2563eb',  # Primary theme color
    'WS_BASE_URL': 'wss://api.example.com',  # WebSocket base URL
}

๐Ÿ“Š What Gets Documented?

From REST Views/ViewSets:

  • โœ… URL paths and patterns
  • โœ… HTTP methods (GET, POST, PUT, PATCH, DELETE)
  • โœ… View docstrings as descriptions
  • โœ… Authentication classes
  • โœ… Permission classes

From Serializers:

  • โœ… Field names and types
  • โœ… Required/optional status
  • โœ… Read-only/write-only fields
  • โœ… Field help text
  • โœ… Choices/enums
  • โœ… Validation constraints

From WebSocket Consumers:

  • โœ… Connection URL pattern
  • โœ… Connection parameters (from URL)
  • โœ… Connection response schema
  • โœ… Disconnect/error codes
  • โœ… Action handlers (client โ†’ server)
  • โœ… Server events (server โ†’ client)
  • โœ… Message schemas and examples
  • โœ… Features from docstrings

๐Ÿ’ก Tips for Better WebSocket Documentation

1. Add Docstrings to Consumers

class DMConsumer(AsyncJsonWebsocketConsumer):
    """
    DM Consumer

    Features:
    - Lazy conversation creation (only on first message)
    - Anyone can message anyone (unknown users allowed)
    - Spam protection for unknown users
    - Typing & recording indicators
    - Read receipts
    """
    pass

2. Document Action Handlers

async def handle_send_message(self, content):
    """
    Send message

    Client sends:
    {
        "action": "send_message",
        "content": "message text",
        "msg_type": "text"
    }
    """
    pass

3. Use Clear Handler Naming

Name your handlers with handle_ prefix:

  • handle_send_message
  • handle_typing_start
  • handle_mark_read

๐Ÿ“„ Output Formats

PDF

Professional documentation with:

  • Cover page with title, version, and generation date
  • Table of contents
  • Detailed endpoint documentation
  • Syntax-highlighted code examples
  • Clean, user-friendly design

HTML

Interactive documentation with:

  • Light theme UI
  • Sidebar navigation
  • Syntax-highlighted JSON/JS examples
  • Method badges (GET=green, POST=blue, WS=cyan)
  • Responsive design

JSON (OpenAPI 3.0)

Standard specification compatible with:

  • Swagger UI
  • Postman
  • Redoc
  • OpenAPI Generator

๐Ÿ”ง Requirements

  • Python 3.8+
  • Django 3.2+
  • Django REST Framework 3.12+
  • ReportLab 4.0+ (for PDF generation)
  • Pillow 9.0+ (for image handling)

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.


๐Ÿ™ Acknowledgments

  • Built for the Django REST Framework & Channels community
  • Inspired by Swagger/OpenAPI specification
  • PDF generation powered by ReportLab

๐Ÿ“ง Support


Made with โค๏ธ for Django developers

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf_api_doc_generator-3.1.7.tar.gz (70.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

drf_api_doc_generator-3.1.7-py3-none-any.whl (44.0 kB view details)

Uploaded Python 3

File details

Details for the file drf_api_doc_generator-3.1.7.tar.gz.

File metadata

  • Download URL: drf_api_doc_generator-3.1.7.tar.gz
  • Upload date:
  • Size: 70.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for drf_api_doc_generator-3.1.7.tar.gz
Algorithm Hash digest
SHA256 30ec94e9b389b87804814b9dfd4afd1aa3a23639bd30d6c3f660cd403bed1fd7
MD5 be61e50a6765dd564b5fcccbdc304a90
BLAKE2b-256 67a7cb77f02d5267b83928db7cbcaedc2fa069f5f60030e92eeb657799a33eb7

See more details on using hashes here.

File details

Details for the file drf_api_doc_generator-3.1.7-py3-none-any.whl.

File metadata

File hashes

Hashes for drf_api_doc_generator-3.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 4954f32452b73d788d4050367b5bb41e96a53d99a666bc9174cb54c90e4b1151
MD5 597394f7c4da608dbd62e560d36360f9
BLAKE2b-256 665a7c59954bdd0d61d526eb2e8efcc5429008a46be6b1e9450211b0bcb20a6e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page