A REST API should spend almost all of its descriptive effort in defining the media type(s) used for representing resources and driving application state.
— Roy Fielding,REST APIs must be hypertext driven
REST framework provides a range of different choices for documenting your API. The followingis a non-exhaustive list of the most popular ones.
drf-spectacular is anOpenAPI 3 schema generation library with explicitfocus on extensibility, customizability and client generation. It is the recommended way forgenerating and presenting OpenAPI schemas.
The library aims to extract as much schema information as possible, while providing decorators and extensions for easycustomization. There is explicit support forswagger-codegen,SwaggerUI andRedoc,i18n, versioning, authentication, polymorphism (dynamic requests and responses), query/path/header parameters,documentation and more. Several popular plugins for DRF are supported out-of-the-box as well.
drf-yasg is aSwagger / OpenAPI 2 generation tool implemented without using the schema generation providedby Django Rest Framework.
It aims to implement as much of theOpenAPI 2 specification as possible - nested schemas, named models,response bodies, enum/pattern/min/max validators, form parameters, etc. - and to generate documents usable with codegeneration tools likeswagger-codegen.
This also translates into a very useful interactive documentation viewer in the form ofswagger-ui:

Deprecation notice: REST framework's built-in support for generating OpenAPI schemas isdeprecated in favor of 3rd party packages that can provide this functionality instead.As replacement, we recommend using thedrf-spectacular package.
There are a number of packages available that allow you to generate HTMLdocumentation pages from OpenAPI schemas.
Two popular options areSwagger UI andReDoc.
Both require little more than the location of your static schema file ordynamicSchemaView endpoint.
Assuming you've followed the example from the schemas documentation for routinga dynamicSchemaView, a minimal Django template for using Swagger UI might bethis:
<!DOCTYPE html><html> <head> <title>Swagger</title> <meta charset="utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" type="text/css" href="//unpkg.com/swagger-ui-dist@3/swagger-ui.css" /> </head> <body> <div id="swagger-ui"></div> <script src="//unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js"></script> <script> const ui = SwaggerUIBundle({ url: "{% url schema_url %}", dom_id: '#swagger-ui', presets: [ SwaggerUIBundle.presets.apis, SwaggerUIBundle.SwaggerUIStandalonePreset ], layout: "BaseLayout", requestInterceptor: (request) => { request.headers['X-CSRFToken'] = "{{ csrf_token }}" return request; } }) </script> </body></html>Save this in your templates folder asswagger-ui.html. Then route aTemplateView in your project's URL conf:
from django.views.generic import TemplateViewurlpatterns = [ # ... # Route TemplateView to serve Swagger UI template. # * Provide `extra_context` with view name of `SchemaView`. path( "swagger-ui/", TemplateView.as_view( template_name="swagger-ui.html", extra_context={"schema_url": "openapi-schema"}, ), name="swagger-ui", ),]See theSwagger UI documentation for advanced usage.
Assuming you've followed the example from the schemas documentation for routinga dynamicSchemaView, a minimal Django template for using ReDoc might bethis:
<!DOCTYPE html><html> <head> <title>ReDoc</title> <!-- needed for adaptive design --> <meta charset="utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet"> <!-- ReDoc doesn't change outer page styles --> <style> body { margin: 0; padding: 0; } </style> </head> <body> <redoc spec-url='{% url schema_url %}'></redoc> <script src="https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"> </script> </body></html>Save this in your templates folder asredoc.html. Then route aTemplateViewin your project's URL conf:
from django.views.generic import TemplateViewurlpatterns = [ # ... # Route TemplateView to serve the ReDoc template. # * Provide `extra_context` with view name of `SchemaView`. path( "redoc/", TemplateView.as_view( template_name="redoc.html", extra_context={"schema_url": "openapi-schema"} ), name="redoc", ),]See theReDoc documentation for advanced usage.
The browsable API that REST framework provides makes it possible for your API to be entirely self describing. The documentation for each API endpoint can be provided simply by visiting the URL in your browser.

The title that is used in the browsable API is generated from the view class name or function name. Any trailingView orViewSet suffix is stripped, and the string is whitespace separated on uppercase/lowercase boundaries or underscores.
For example, the viewUserListView, will be namedUser List when presented in the browsable API.
When working with viewsets, an appropriate suffix is appended to each generated view. For example, the view setUserViewSet will generate views namedUser List andUser Instance.
The description in the browsable API is generated from the docstring of the view or viewset.
If the pythonMarkdown library is installed, thenmarkdown syntax may be used in the docstring, and will be converted to HTML in the browsable API. For example:
class AccountListView(views.APIView): """ Returns a list of all **active** accounts in the system. For more details on how accounts are activated please [see here][ref]. [ref]: http://example.com/activating-accounts """Note that when using viewsets the basic docstring is used for all generated views. To provide descriptions for each view, such as for the list and retrieve views, use docstring sections as described inSchemas as documentation: Examples.
OPTIONS methodREST framework APIs also support programmatically accessible descriptions, using theOPTIONS HTTP method. A view will respond to anOPTIONS request with metadata including the name, description, and the various media types it accepts and responds with.
When using the generic views, anyOPTIONS requests will additionally respond with metadata regarding anyPOST orPUT actions available, describing which fields are on the serializer.
You can modify the response behavior toOPTIONS requests by overriding theoptions view method and/or by providing a custom Metadata class. For example:
def options(self, request, *args, **kwargs): """ Don't include the view description in OPTIONS responses. """ meta = self.metadata_class() data = meta.determine_metadata(request, self) data.pop('description') return Response(data=data, status=status.HTTP_200_OK)Seethe Metadata docs for more details.
To be fully RESTful an API should present its available actions as hypermedia controls in the responses that it sends.
In this approach, rather than documenting the available API endpoints up front, the description instead concentrates on themedia types that are used. The available actions that may be taken on any given URL are not strictly fixed, but are instead made available by the presence of link and form controls in the returned document.
To implement a hypermedia API you'll need to decide on an appropriate media type for the API, and implement a custom renderer and parser for that media type. TheREST, Hypermedia & HATEOAS section of the documentation includes pointers to background reading, as well as links to various hypermedia formats.