Swagger UI Integration
OpenAPI 3 and Swagger UI with drf-spectacular
Interactive API documentation is essential for modern frontend-backend collaboration. DRF OpenAPI 3 documentation is powered by drf-spectacular, the officially recommended documentation generator for Django REST Framework.
1. Why drf-spectacular?
Older tools like coreapi and drf-yasg only supported OpenAPI 2.0 (Swagger 2.0). drf-spectacular generates fully compliant OpenAPI 3.0 schemas, supporting:
- Rich typing and JSON Schema validation
- Accurate component schemas for nested serializers and Polymorphic models
- Interactive Swagger UI and Redoc endpoints
- Client code generation (TypeScript fetch, Axios, Python SDKs)
2. Installation and Settings Configuration
Install the package:
Add to INSTALLED_APPS and configure DRF default schema class in settings.py:
3. Configuring URL Endpoints
In config/urls.py, register the raw schema download endpoint and the interactive Swagger UI:
Visiting /api/docs/swagger/ in your browser provides an interactive dashboard where developers can test endpoints, view request bodies, inspect status codes, and authenticate using the "Authorize" button.
Practice Quiz
drf-spectacular generate documentation for?REST_FRAMEWORK in settings.py for drf-spectacular to inspect views?SpectacularAPIView in urls.py?'SERVE_INCLUDE_SCHEMA': False in SPECTACULAR_SETTINGS do?Redoc Documentation
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Automated API Tests | Redoc Documentation |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.