You keep using that word “REST”. I do not think it means what you think it means.— Mike Amundsen, REST fest 2012 keynote
Disclaimer
The name “Django REST framework” was chosen in early 2011 simply to ensure the project would be easily found by developers. Throughout this documentation, we use the more accurate terminology of “Web APIs”.If you’re serious about designing a Hypermedia API, you should look to resources outside this documentation to help inform your design choices.
Required Reading
Before building a true hypermedia API, familiarize yourself with these foundational resources:Core Resources
Roy Fielding's Dissertation
Roy Fielding's Dissertation
Architectural Styles and the Design of Network-based Software ArchitecturesThe original dissertation that defined REST. Essential reading for understanding the architectural constraints and principles behind RESTful design.Key chapters:
- Chapter 5: Representational State Transfer (REST)
- Chapter 6: REST Applied to the Web
Roy Fielding's Blog Post
Roy Fielding's Blog Post
REST APIs must be hypertext-drivenA clarifying post where Fielding explains that APIs without hypermedia are not RESTful. Addresses common misconceptions about REST.
RESTful Web APIs (Book)
RESTful Web APIs (Book)
RESTful Web APIs by Leonard Richardson & Mike AmundsenComprehensive guide to designing practical RESTful APIs with hypermedia. Covers media types, hypermedia formats, and real-world implementation strategies.
Building Hypermedia APIs (Book)
Building Hypermedia APIs (Book)
Building Hypermedia APIs with HTML5 and Node by Mike AmundsenPractical guide to building hypermedia APIs, with focus on HTML5 as a hypermedia format.
Designing Hypermedia APIs
Designing Hypermedia APIs
Designing Hypermedia APIs by Steve KlabnikFocused guide on the principles of hypermedia API design.
Additional Resources
- Richardson Maturity Model - A model for understanding REST API maturity levels
- Hypermedia API Reading List - Comprehensive background reading
Understanding REST Principles
The REST Constraint
A truly RESTful API must follow these architectural constraints:- Client-Server - Separation of concerns
- Stateless - Each request contains all information needed
- Cacheable - Responses must define themselves as cacheable or not
- Uniform Interface - Resource identification, manipulation through representations
- Layered System - Client cannot tell if connected directly to end server
- Hypermedia as the Engine of Application State (HATEOAS) - Clients interact through hypermedia provided dynamically by the server
Richardson Maturity Model
APIs can be classified by their REST maturity: Level 0: The Swamp of POX- Single URI, single HTTP method (usually POST)
- All operations tunneled through POST
- Multiple URI-addressable resources
- Still using single HTTP method
- Uses HTTP methods appropriately (GET, POST, PUT, DELETE)
- Uses HTTP status codes
- Most “REST APIs” stop here
- Uses HATEOAS
- Responses include links to related resources
- True REST
What is HATEOAS?
Hypermedia as the Engine of Application State (HATEOAS) means that:- API responses include links to related resources and available actions
- Clients navigate the API by following links, not constructing URLs
- The API is self-documenting through the links and forms it provides
- API evolution doesn’t break clients (if they follow links)
HATEOAS Example
Traditional API Response:_links to discover available operations rather than hardcoding URLs.
Building Hypermedia APIs with REST Framework
REST framework is an agnostic Web API toolkit. It helps guide you toward building well-connected APIs but doesn’t strictly enforce any particular design style.What REST Framework Provides
Browsable API (HTML Hypermedia)
Browsable API (HTML Hypermedia)
The browsable API is built on HTML - the original hypermedia language of the web:The browsable API provides:
- Clickable links between resources
- Forms for available actions
- Self-documenting interface
Serialization for Media Types
Serialization for Media Types
Build appropriate media types using serializers:
HyperlinkedModelSerializer automatically includes links to related resources.Hyperlinked Relations
Hyperlinked Relations
Use hyperlinked fields to connect resources:
Content Negotiation
Content Negotiation
Support multiple media types:
What REST Framework Doesn’t Provide
REST framework does not provide by default:- Machine-readable hypermedia formats (HAL, Collection+JSON, JSON API, HTML microformats)
- Auto-magical fully HATEOAS-style APIs
- Hypermedia-based form descriptions
- Semantically labeled hyperlinks
Providing these features would involve making opinionated choices about API design that should remain outside the framework’s scope.
Hypermedia Formats
Several standardized hypermedia formats exist:HAL (Hypertext Application Language)
HAL is a simple format that uses_links and _embedded conventions:
Collection+JSON
Collection+JSON is designed for managing collections of resources:JSON API
JSON API is a specification for building APIs with standardized request/response formats:HTML Microformats
Microformats embed semantic information in HTML:Implementing Hypermedia in REST Framework
Using HyperlinkedModelSerializer
The easiest way to add hypermedia to your API:urlfield pointing to the resource- Related fields as hyperlinks instead of IDs
Custom Hypermedia Renderer
Create a custom renderer for your chosen hypermedia format:Adding Actions to Responses
Include available actions in responses:Best Practices
Start with HyperlinkedModelSerializer
Start with HyperlinkedModelSerializer
Use
HyperlinkedModelSerializer as a starting point:Choose an Appropriate Media Type
Choose an Appropriate Media Type
Select a hypermedia format that fits your use case:
- HAL - Simple, widely supported
- JSON API - Rich specification with strong conventions
- Collection+JSON - Good for collection-oriented APIs
- HTML - For human-readable APIs (browsable API)
Include Links to Related Resources
Include Links to Related Resources
Document Your Media Type
Document Your Media Type
Provide clear documentation about your chosen hypermedia format and how clients should interact with it.
Test with Real Clients
Test with Real Clients
Ensure your hypermedia API works well with actual client implementations. The true test of a hypermedia API is whether clients can navigate it without hardcoded URLs.
Related Resources
- Serializers - Including
HyperlinkedModelSerializer - Renderers - Custom renderer implementation
- Parsers - Custom parser implementation
- Relations - Hyperlinked and nested relationships
- Content Negotiation - Media type selection
- Documenting Your API - API documentation strategies
