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
Third-Party OpenAPI Packages
REST framework recommends using third-party packages for generating and presenting OpenAPI schemas, as they provide more features and flexibility than the built-in (deprecated) implementation.drf-spectacular (Recommended)
drf-spectacular is the recommended OpenAPI 3 schema generation library with explicit focus on extensibility, customizability, and client generation.drf-spectacular aims to extract as much schema information as possible while providing decorators and extensions for easy customization.
- OpenAPI 3.0 support
- Explicit support for Swagger UI, ReDoc, and swagger-codegen
- Internationalization (i18n) support
- API versioning support
- Authentication schemes
- Polymorphism (dynamic requests and responses)
- Query/path/header parameters
- Popular DRF plugins supported out-of-the-box
drf-yasg
drf-yasg is a Swagger/OpenAPI 2 generation tool with comprehensive feature support. Features:- Nested schemas and named models
- Response bodies
- Enum/pattern/min/max validators
- Form parameters
- Interactive Swagger UI documentation
Built-in OpenAPI Schema (Deprecated)
Swagger UI Integration
If you’re using the deprecated built-in schema generation, you can integrate Swagger UI with a minimal template:ReDoc Integration
ReDoc provides a clean, responsive three-panel documentation layout:Self-Describing APIs
The browsable API that REST framework provides makes your API self-documenting. Documentation for each endpoint is provided simply by visiting the URL in your browser.
Setting the Title
The title displayed in the browsable API is generated from the view class name or function name:- Trailing
VieworViewSetsuffixes are stripped - Names are whitespace-separated on uppercase/lowercase boundaries or underscores
Setting the Description
Descriptions are generated from the docstring of the view or viewset:If the Python
Markdown library is installed, markdown syntax in docstrings will be converted to HTML in the browsable API.The OPTIONS Method
REST framework APIs support programmatically accessible descriptions using theOPTIONS HTTP method:
- View name
- View description
- Accepted media types
- Available
POST/PUTactions with field information
Hypermedia Approach
For fully RESTful APIs, you should present available actions as hypermedia controls in responses.In the hypermedia approach, rather than documenting API endpoints up front, the description concentrates on the media types used. Available actions are made available through link and form controls in the returned document.
- Choose an appropriate media type (HAL, JSON API, Collection+JSON, etc.)
- Implement a custom renderer for that media type
- Implement a custom parser for that media type
- REST, Hypermedia & HATEOAS - Background reading and hypermedia format links
- API Guide: Renderers - Custom renderer implementation
- API Guide: Parsers - Custom parser implementation
Best Practices
Keep Documentation in Sync with Code
Keep Documentation in Sync with Code
Use schema generation tools like drf-spectacular that automatically extract documentation from your code. This ensures your documentation stays up-to-date as your API evolves.
Write Comprehensive Docstrings
Write Comprehensive Docstrings
Document each view, viewset, and serializer with clear docstrings. Include:
- Purpose and behavior
- Parameters and their types
- Return values
- Example usage
- Error conditions
Provide Example Requests and Responses
Provide Example Requests and Responses
Use tools like Swagger UI or ReDoc to provide interactive examples that developers can try directly from the documentation.
Document Authentication Requirements
Document Authentication Requirements
Clearly indicate which endpoints require authentication and what permissions are needed:
Version Your API Documentation
Version Your API Documentation
Maintain separate documentation for each API version to help developers migrate between versions smoothly.
Related Resources
- Browsable API - Understanding the self-documenting HTML interface
- Schemas - Generating API schemas
- Metadata - Programmatic API information
- REST, Hypermedia & HATEOAS - Hypermedia API design
