Django REST Framework

Install

pip install drf-spectacular

Setup

Add drf-spectacular to your Django settings:

# settings.py
INSTALLED_APPS = [
    ...
    'rest_framework',
    'drf_spectacular',
]

REST_FRAMEWORK = {
    'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
}

SPECTACULAR_SETTINGS = {
    'TITLE': 'My API',
    'VERSION': '1.0.0',
    'SERVE_INCLUDE_SCHEMA': False,
}

Add the spec URL to your URL configuration:

# urls.py
from django.urls import path
from drf_spectacular.views import SpectacularAPIView

urlpatterns = [
    ...
    path('openapi.json', SpectacularAPIView.as_view(), name='schema'),
]

The spec is now available at:

http://localhost:8000/openapi.json

Add descriptions

Use docstrings and @extend_schema to enrich your spec:

from drf_spectacular.utils import extend_schema
from rest_framework.decorators import api_view
from rest_framework.response import Response

@extend_schema(
    operation_id='getItem',
    summary='Get a single item',
    responses={200: ItemSerializer},
)
@api_view(['GET'])
def get_item(request, pk):
    item = Item.objects.get(pk=pk)
    return Response(ItemSerializer(item).data)

Make it publicly accessible

Deploy your app or use a tunnel for local testing:

ngrok http 8000
# then use: https://<random>.ngrok.io/openapi.json

Verify

Visit /openapi.json in your browser. You should see a valid OpenAPI 3.x JSON document.

Paste this URL into Automiel’s import step.