Version your API by declaring it, not copying it.
Version-aware routing for Django and DRF. Define versioned endpoints with @endpoint.
from django_api_versioning import endpoint
from rest_framework.views import APIView
from rest_framework.response import Response
@endpoint("invoices/<int:invoice_id>/", version=2, view_name="invoice-detail")
class InvoiceDetailView(APIView):
def get(self, request, invoice_id):
return Response({"id": invoice_id, "status": "paid"})GENERATED ROUTES · API_MAX_VERSION = 3
- /api/v1/invoices/<id>/404
- /api/v2/invoices/<id>/InvoiceDetailView
- /api/v3/invoices/<id>/InvoiceDetailView ↺ v2
Why
One source of truth for every API version
Stop duplicating URLconfs and view modules for each release. Declare where an implementation starts, and let the router fill in the rest.
Declare once, serve forward
Decorate a view with @endpoint and a starting version. Every later version reuses it until a newer implementation takes over.
Generated URL patterns
The package builds versioned routes from API_BASE_PATH. No hand-maintained v1/, v2/, v3/ URLconfs.
Django & DRF
Use @endpoint for Django views and APIViews, or VersionedRouter for DRF ViewSets. DRF remains optional for Django-native projects.
Route matrix & gap checks
check_versions prints every logical endpoint across versions and warns about gaps inside a span.
CI snapshot guard
assert_version_matrix pins the matrix to a JSON snapshot so accidental route changes fail the build.
Deprecation & Sunset
LifecycleMiddleware handles version-wide deprecation and retirement. Version 1.2.0 adds endpoint-level lifecycle headers and optional OpenAPI deprecation metadata.
Versioned DRF ViewSets
VersionedRouter delegates URL generation to DRF SimpleRouter and inherits or replaces implementations per generated action.
Explicit route removal
Version 1.2.0 adds remove_endpoint() and VersionedRouter.remove() so inherited routes can be removed from a chosen version onward.
Quick start
Three steps to versioned routes
Result: /api/v1/invoices/42/ returns 404, while v2 and v3 are both served by InvoiceDetailView.
# settings.py
INSTALLED_APPS = [
# ...
"django_api_versioning",
]
API_BASE_PATH = "api/v{version}/"
API_MIN_VERSION = 1
API_MAX_VERSION = 3Registration patterns
Four ways to say which versions a view serves
Shown with API_MAX_VERSION = 5. Combining version and versions raises VersionRangeError.
Forward segment
v3 → v5
@endpoint("reports/", version=3)Explicit set
v2 and v4 only
@endpoint("ranking/", versions=[2, 4])Bounded range
v1 → v3 inclusive
@endpoint("legacy/", versions=ver_range(1, 3))Unversioned
/health/
@endpoint("health/")Playground
Watch declarations become a version matrix
Pick a scenario or edit the declarations. The matrix updates live using the package’s documented rules: forward segments hand over to newer ones, collisions go to the higher source version, and gaps get flagged.
A new view takes over from v3; v1–v2 keep the original.
- v1
@endpoint("invoices/<int:invoice_id>/", version=1) - v3
@endpoint("invoices/<int:invoice_id>/", version=3)
$ python manage.py check_versions
invoices/<int:invoice_id>/
✓ No gaps. Matrix is consistent.
Interactive illustration of the documented resolution rules. Run check_versions in your project for the real matrix.
Lifecycle
Deprecate and sunset versions over HTTP
LifecycleMiddleware tells clients what’s coming. Deprecated versions get an RFC 9745 Deprecation header and an RFC 8594 Sunset header; once the sunset date passes, requests receive 410 Gone.
MIDDLEWARE = [
# ...
"django_api_versioning.lifecycle.LifecycleMiddleware",
]
API_VERSION_LIFECYCLE = {
1: {"status": "retired", "sunset": "2025-01-01"},
2: {
"status": "deprecated",
"deprecated_at": "2026-01-01T00:00:00+00:00",
"sunset": "2027-06-01",
},
3: {"status": "active"},
}Try a request against each version:
GET /api/v2/invoices/42/ HTTP/1.1
HTTP/1.1 200 OK
Content-Type: application/json
Deprecation: @1767225600
Sunset: Tue, 01 Jun 2027 00:00:00 GMT
Deprecated entries require a valid deprecated_at. The middleware reads its settings on every request. Version 1.2.0 also adds operation-scoped lifecycle declarations for decorator endpoints and VersionedRouter actions, with optional OpenAPI deprecation metadata. Endpoint-level Sunset remains headers-only; it does not retire the route or return 410.
Reliability
Know exactly what each version serves
Versioning bugs are silent: a route disappears from v4 and nobody notices until a client breaks. These tools make the matrix visible and testable.
check_versions
Prints the route matrix grouped by logical endpoint and warns about gaps.
$ python manage.py check_versions
users/:
v1 → users_list_v1
v2 → users_list_v2
v3 → users_list_v2
v4 → users_list_v2assert_version_matrix
Snapshot the matrix in CI and fail with a diff on unexpected changes.
from django_api_versioning.testing import assert_version_matrix
def test_version_matrix():
# Creates the snapshot on first run.
# Raises VersionMatrixError with a diff when routes change.
assert_version_matrix(snapshot=".version_matrix.json")
# Accept an intentional change:
# assert_version_matrix(snapshot=".version_matrix.json", update=True)api_reverse
Reverse a named route for a specific version.
from django_api_versioning import api_reverse
url = api_reverse("order-list", version=2)
# version can be omitted when only one route has the name
url = api_reverse("health-check")
# ...but raises AmbiguousVersionError if several match.Good to know
Before you ship
Configure request.version for ViewSets
VersionedRouterVersioning exposes the active router namespace as request.version and keeps reverse_action() and hyperlinked serializer URLs version-aware. Configure it globally or on selected ViewSets.
Route settings and lifecycle settings have different timing
Changing route settings after URL patterns are imported does not rebuild the registry. LifecycleMiddleware reads lifecycle settings per request.
Import order matters
Import your endpoint modules in the root URLconf before including django_api_versioning.urls.
ViewSet integration scope
VersionedRouter supports DRF SimpleRouter, not DefaultRouter API-root or format-suffix behavior. It selects route callbacks; it does not transform serializers or response payloads.
Full settings, arguments, and collision rules are on the reference page.
Articles
Go deeper
25 long-form guides on routing models, inheriting endpoints, deprecation and migrations.
Ship v2 without copying v1.
Open source and MIT licensed. Tested in CI against Django 4.2, 5.2, 6.0 and 6.1.