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
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.pdfapi_docs_YYYYMMDD_HHMMSS.htmlapi_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_messagehandle_typing_starthandle_mark_read
๐ Output Formats
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
- ๐ Documentation
- ๐ Issue Tracker
- ๐ฌ Discussions
Made with โค๏ธ for Django developers
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
30ec94e9b389b87804814b9dfd4afd1aa3a23639bd30d6c3f660cd403bed1fd7
|
|
| MD5 |
be61e50a6765dd564b5fcccbdc304a90
|
|
| BLAKE2b-256 |
67a7cb77f02d5267b83928db7cbcaedc2fa069f5f60030e92eeb657799a33eb7
|
File details
Details for the file drf_api_doc_generator-3.1.7-py3-none-any.whl.
File metadata
- Download URL: drf_api_doc_generator-3.1.7-py3-none-any.whl
- Upload date:
- Size: 44.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4954f32452b73d788d4050367b5bb41e96a53d99a666bc9174cb54c90e4b1151
|
|
| MD5 |
597394f7c4da608dbd62e560d36360f9
|
|
| BLAKE2b-256 |
665a7c59954bdd0d61d526eb2e8efcc5429008a46be6b1e9450211b0bcb20a6e
|