drf-yasg – Yet another Swagger generator

Travis CI  Codecov  ReadTheDocs  PyPI

Generate real Swagger/OpenAPI 2.0 specifications from a Django Rest Framework API.

Compatible with

  • Django Rest Framework: 3.10, 3.11, 3.12
  • Django: 2.2, 3.0, 3.1
  • Python: 3.6, 3.7, 3.8, 3.9

Only the latest patch version of each major.minor series of Python, Django and Django REST Framework is supported.

Only the latest version of drf-yasg is supported. Support of old versions is dropped immediately with the release of a new version. Please do not create issues before upgrading to the latest release available at the time. Regression reports are accepted and will be resolved with a new release as quickly as possible. Removed features will usually go through a deprecation cycle of a few minor releases.

Resources:

Heroku deploy button

OpenAPI 3.0 note

If you are looking to add Swagger/OpenAPI support to a new project you might want to take a look at drf-spectacular, which is an actively maintained new library that shares most of the goals of this project, while working with OpenAPI 3.0 schemas.

OpenAPI 3.0 provides a lot more flexibility than 2.0 in the types of API that can be described. drf-yasg is unlikely to soon, if ever, get support for OpenAPI 3.0.

Features

  • full support for nested Serializers and Schemas
  • response schemas and descriptions
  • model definitions compatible with codegen tools
  • customization hooks at all points in the spec generation process
  • JSON and YAML format for spec
  • bundles latest version of swagger-ui and redoc for viewing the generated documentation
  • schema view is cacheable out of the box
  • generated Swagger schema can be automatically validated by swagger-spec-validator
  • supports Django REST Framework API versioning with URLPathVersioning and NamespaceVersioning; other DRF or custom versioning schemes are not currently supported
redoc screenshot

Fully nested request and response schemas.

swagger-ui screenshot

Choose between redoc and swagger-ui.

model definitions screenshot

Real Model definitions.

Table of contents

Usage

0. Installation

The preferred installation method is directly from pypi:

pip install -U drf-yasg

Additionally, if you want to use the built-in validation mechanisms (see 4. Validation), you need to install some extra requirements:

pip install -U drf-yasg[validation]

1. Quickstart

In settings.py:

INSTALLED_APPS = [
   ...
   'django.contrib.staticfiles',  # required for serving swagger ui's css/js files
   'drf_yasg',
   ...
]

In urls.py:

<div class="highlight highlight-source-python position-relative" data-snippet-clipboard-copy-content="…
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi

schema_view = get_schema_view(
openapi.Info(
title="Snippets API",
default_version='v1',
description="Test description",
terms_of_service="https://www.google.com/policies/terms/",
contact=openapi.Contact(email="[email protected]"),
license=openapi.License(name="BSD License"),
),
public=True,
permission_classes=[permissions.AllowAny],
)

urlpatterns = [
url(r'^swagger(?P\.json|\.yaml)$’, schema_view.without_ui(cache_timeout=0), name=’schema-json’),
url(r’^swagger/$’, schema_view.with_ui(‘swagger’, cache_timeout=0), name=’schema-swagger-ui’),
url(r’^redoc/$’, schema_view.with_ui(‘redoc’, cache_timeout=0), name=’schema-redoc’),

]
“>

...
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi

...

schema_view = get_schema_view(
   openapi.Info(
      title="Snippets API",
      default_version='v1',
      description="Test description",
      terms_of_service="https://www.google.com/policies/terms/",
      contact=openapi.Contact(email="[email protected]"),
      license=openapi.License(name="BSD License"),
   ),
   public=True,
   permission_classes=[permissions.AllowAny],
)

urlpatterns = [
   url(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
   url(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
   url(r'^redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
   ...
]