DRF API & WebSocket Documentation Generator

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
INSTALLED_APPS = [
'rest_framework',
'api_docs_generator',
]
2. Generate REST API Documentation
python manage.py generate_api_docs auth
python manage.py generate_api_docs auth users products
python manage.py generate_api_docs auth --format all
python manage.py generate_api_docs --all
3. Generate WebSocket Documentation
python manage.py generate_api_docs --websocket chat/consumers/dm_consumer.py
python manage.py generate_api_docs --websocket chat/consumers/*.py
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...]
--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
python manage.py generate_api_docs auth --title "My Awesome API"
python manage.py generate_api_docs --websocket chat/consumers.py --ws-base-url wss://api.myapp.com
python manage.py generate_api_docs auth --output ./docs/api/
python manage.py generate_api_docs auth --format html --open
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:
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',
'THEME_COLOR': '#2563eb',
'WS_BASE_URL': 'wss://api.example.com',
}
π 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.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature)
- Commit your changes (
git commit -m 'Add amazing feature')
- Push to the branch (
git push origin feature/amazing-feature)
- 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