All framework guides

Python

Get an OpenAPI spec from Django REST Framework

Install drf-spectacular and run one command to write openapi.yaml.

Tool
drf-spectacular
Time
About 5 minutes
You get
openapi.yaml or openapi.json (OpenAPI 3.0)

How it works

drf-spectacular reads your DRF views, viewsets, serializers and URL routes, and builds a detailed spec with fields and types.

Plain Django views that are not part of Django REST Framework are not included. For those, use From code (GitHub) or a Postman collection.

Step by step

  1. Step 1: Install it

    In the environment your project uses:

    Terminal
    pip install drf-spectacular

    Add drf-spectacular to requirements.txt too, so it is installed on your server.

  2. Step 2: Add it to settings.py

    Add the app, tell DRF to use it for schemas, and give the API a title. Keep your existing REST_FRAMEWORK settings and add the one line.

    settings.py
    INSTALLED_APPS = [
        # ...your existing apps
        'rest_framework',
        'drf_spectacular',
    ]
    
    REST_FRAMEWORK = {
        # ...your existing settings
        'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
    }
    
    SPECTACULAR_SETTINGS = {
        'TITLE': 'My API',
        'DESCRIPTION': 'What this API does, in one sentence.',
        'VERSION': '1.0.0',
        'SERVE_INCLUDE_SCHEMA': False,
    }
  3. Step 3: Write the file

    Run one of these in the folder with manage.py. Warnings are normal: they point at views it could not fully understand, and the file is still written.

    Terminal (YAML)
    python manage.py spectacular --color --file openapi.yaml
    Terminal (JSON)
    python manage.py spectacular --format openapi-json --file openapi.json
  4. Step 4: Optional: serve it from your API

    So you can paste a link into MCPEasy instead of uploading. Add to your root urls.py:

    urls.py
    from django.urls import path
    from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView
    
    urlpatterns = [
        # ...your existing urls
        path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
        path('api/schema/docs/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
    ]

    The link is https://your-api.com/api/schema/ (YAML). Add ?format=json for JSON.

  5. Step 5: Optional: fix views it cannot read

    For an APIView without a serializer_class, tell drf-spectacular what goes in and out:

    views.py
    from drf_spectacular.utils import extend_schema
    
    class BookingView(APIView):
        @extend_schema(request=BookingSerializer, responses={201: BookingSerializer}, summary="Create a booking")
        def post(self, request):
            ...

Check that it worked

  • openapi.yaml lists your endpoints under paths:, with request bodies that match your serializers.

Give it to MCPEasy

Have a link? Paste it (best)

In MCPEasy, create a server, choose OpenAPI URL and paste the link, for example https://api.example.com/openapi.json. Later changes to your API can be picked up with a sync.

Have a file? Upload it

Choose Upload file and drop in the .json or .yaml file (up to 5 MB). MCPEasy turns each endpoint into a tool, and you review them all before anything goes live.

If something goes wrong

ModuleNotFoundError: No module named 'drf_spectacular'.
It was installed in a different environment. Activate your project's virtual environment and install again.
Many "unable to guess serializer" warnings.
Those views have no serializer_class. The paths are still in the file; add @extend_schema (step 5) for better fields.
Some endpoints are missing.
They are plain Django views, not DRF views. Use From code (GitHub) in MCPEasy for those.

Still stuck, or no time to set this up? In MCPEasy choose From code (GitHub): it reads your routes straight from the repository, read-only, and tests every tool against your live API before it goes live.

Official documentation

Other frameworks