DRF Spectacular

0.29.0 · active · verified Thu Apr 09

DRF Spectacular is a powerful and flexible OpenAPI 3 schema generation tool for Django REST Framework. It automates API documentation, offering robust support for Swagger UI and ReDoc. Actively maintained, it receives frequent updates, typically every 1-2 months, incorporating new features, bug fixes, and compatibility with the latest Django and DRF versions.

Warnings

Install

Imports

Quickstart

To integrate DRF Spectacular, add `drf_spectacular` to your `INSTALLED_APPS` and define `SPECTACULAR_SETTINGS` in your `settings.py`. Then, include the schema and UI views in your `urls.py`. The `SpectacularAPIView` serves the raw OpenAPI schema, while `SpectacularSwaggerView` and `SpectacularRedocView` provide interactive documentation UIs.

import os
from django.urls import path, include
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView, SpectacularRedocView

# --- In your Django settings.py ---
# INSTALLED_APPS = [
#     # ...
#     'rest_framework',
#     'drf_spectacular',
#     # ...
# ]
#
# SPECTACULAR_SETTINGS = {
#     'TITLE': os.environ.get('DRF_SPECTACULAR_TITLE', 'Your Project API'),
#     'DESCRIPTION': os.environ.get('DRF_SPECTACULAR_DESCRIPTION', 'Your project description'),
#     'VERSION': os.environ.get('DRF_SPECTACULAR_VERSION', '1.0.0'),
#     'SERVE_INCLUDE_SCHEMA': False, # Keep off for production, serve via SpectacularAPIView
#     'SWAGGER_UI_SETTINGS': {
#         'deepLinking': True,
#         'displayRequestDuration': True,
#     },
# }

# --- In your Django urls.py ---
urlpatterns = [
    path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
    # Optional UI: path('api/schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
    # Optional UI: path('api/schema/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='redoc'),
    # path('api/', include('your_app.urls')), # Example for your API endpoints
]

# To run a minimal example (requires Django setup)
# from django.conf import settings
# if not settings.configured:
#     settings.configure(
#         INSTALLED_APPS=['django.contrib.auth', 'django.contrib.contenttypes', 'rest_framework', 'drf_spectacular'],
#         SPECTACULAR_SETTINGS={'TITLE': 'Test API'},
#         ROOT_URLCONF=__name__,
#         DEBUG=True
#     )
# This is a runnable snippet illustrating the `urls.py` config for drf-spectacular.

view raw JSON →