Add OpeanAPI documentation and SwaggerUI to your Django Rest Framework project

Here is step-by-step recipe on how to add auto generated documentation for your REST API in OpenAPI format and enable SwaggerUI. The recipe is based on Document your API tutorial from Django Rest Framework.

This article was originally published on my blog. For up to date and maintained version of the article, refer to Add OpeanAPI documentation and SwaggerUI to your Django Rest Framework project.

Step 1: Create documentation application

Step 2. Register documentation endpoints

Create urls.py in the api_docs application:

This will register three endpoints:

  • /openapi.json - OpenAPI documentation in JSON format
  • /openapi.yaml - OpenAPI documentation in YAML format
  • /docs - Swagger UI, based on the openapi.yaml

Step 3. Create the Swagger UI template

Create a swagger-ui.html file in the api_docs application's templates directory:

Step 4. Register the api_docs application with the Django project

To register the api_docs application in the Django project you need to modify the project's settings.py:

Step 5. Add documentation URLs to Django project urls

Modify the urls.py file for your Django project. Make sure in contains include('api_docs.urls'):