Article 023
Install and Configure django-api-versioning
Install django-api-versioning, configure version boundaries, register endpoints, reverse versioned URLs, and verify routing with Django REST Framework.
django-api-versioning registers versioned Django URL patterns through the @endpoint decorator. You declare an endpoint at its starting version, and the package generates its URL patterns through the configured maximum version. Later declarations can replace selected implementations without requiring a separate declaration for every unchanged endpoint.
This guide covers installation, version settings, endpoint registration, URL reversal, lifecycle middleware, and integration testing.
The package is maintained by the author of this article. For current release metadata and compatibility information, consult the package metadata and compatibility report linked below.
Install
Install the package from PyPI:
pip install django-api-versioningCheck the installed version:
pip show django-api-versioningPython, Django, and DRF requirements
The package's runtime requirements are declared in its project metadata. Django REST Framework is not required to install the core package; it is used by the DRF integration examples and the project's development and testing setup.
For the declared Python and Django requirements, see the package metadata. For configured combinations, see tox.ini; for actual test outcomes, inspect the compatibility workflow runs and verify the source commit SHA.
If your project uses DRF, install it separately:
pip install djangorestframeworkThe development dependency does not define a supported DRF version range. Check the package's current project metadata and test the Python, Django, and DRF combination used by your application.
Configure Django settings
The package provides defaults for its version settings:
| Setting | Default | Purpose |
|---|---|---|
API_MIN_VERSION | 1 | Lowest supported API version |
API_MAX_VERSION | 1 | Highest supported API version |
API_BASE_PATH | "api/v{version}/" | Versioned URL prefix template |
To register endpoints for v1 and v2, configure the version range:
# settings.py
API_MIN_VERSION = 1
API_MAX_VERSION = 2Both settings are optional because they have defaults. However, API_MAX_VERSION must be raised when you register endpoints above the default maximum of v1.
For example, with API_MAX_VERSION = 2, an endpoint declared with version=1 generates routes for both v1 and v2 unless a later declaration replaces the v2 implementation.
You can customize the URL prefix:
API_BASE_PATH = "api/v{version}/"This is the default. The pattern must contain {version} and is normalized to end with a slash.
The package uses integer version identifiers in Python configuration and decorator arguments. The v prefix belongs to the generated URL representation: version=2 produces a v2 URL segment.
See the canonical routing contract for the complete rules governing version identifiers, URL construction, registration precedence, and supported view types.
Configure the URLconf
Import the modules containing your @endpoint declarations before importing the package's URL patterns:
# urls.py
from django.urls import include, path
from . import views # noqa: F401
from django_api_versioning.urls import urlpatterns as api_urlpatterns
urlpatterns = [
path("", include(api_urlpatterns)),
]The import order matters.
The @endpoint decorator registers routes when the module containing the decorated view is imported. The package's urlpatterns references the registry's URL patterns. If you import api_urlpatterns before importing the modules containing your views, their declarations have not executed yet and their routes will be missing.
If your endpoints are spread across several modules, import all of those modules before importing api_urlpatterns.
Organize endpoint declarations by version
As an API grows, you can organize view modules by version and resource:
api/
views/
__init__.py
v1/
__init__.py
users.py
v2/
__init__.py
users.py
urls.pyThe version directories are an application organization choice. They do not determine the generated URL. The decorator arguments, API_BASE_PATH, optional app_name, and postfix determine the route.
For this layout, import the declaration modules in api/views/__init__.py:
# api/views/__init__.py
from .v1 import users as v1_users
from .v2 import users as v2_usersThen import api.views before the package URL patterns:
# project/urls.py
from django.urls import include, path
from api import views # noqa: F401
from django_api_versioning.urls import urlpatterns as api_urlpatterns
urlpatterns = [
path("", include(api_urlpatterns)),
]The imports intentionally execute the decorators. They are not ordinary URL includes.
Why mount at the root?
With the default API_BASE_PATH, the package already includes the api/v{version}/ prefix.
Mounting its patterns at the root produces URLs such as:
/api/v1/users/
/api/v2/users/Do not mount the patterns under another api/ prefix unless you intentionally want that prefix added to the generated paths.
The final URL combines the URLconf mount point with the route generated by the package.
Register an endpoint
Import the decorator from the package's public API:
from django_api_versioning import endpointIts public signature is:
def endpoint(
postfix: str,
version: int | None = None,
versions: list[int] | None = None,
app_name: str | None = None,
view_name: str | None = None,
) -> Callable[[ViewT], ViewT]:
...The main arguments are:
postfix: The endpoint path after the configured base path and optionalapp_namesegment.version: The starting version. The endpoint is registered from this version throughAPI_MAX_VERSION, inclusive.versions: An explicit list of versions. It cannot be used together withversion.app_name: An optional path segment for grouping endpoints.view_name: An optional Django URL pattern name, used for URL reversal.
The following example registers a list endpoint and a detail endpoint for v1. The list implementation changes in v2, while the detail implementation remains unchanged.
# views.py
from django_api_versioning import endpoint
from rest_framework.generics import (
ListCreateAPIView,
RetrieveUpdateDestroyAPIView,
)
from .models import User
from .serializers import UserSerializerV1, UserSerializerV2
@endpoint("users/", version=1, view_name="user-list")
class UserListViewV1(ListCreateAPIView):
queryset = User.objects.all()
serializer_class = UserSerializerV1
@endpoint(
"users/<int:pk>/",
version=1,
view_name="user-detail",
)
class UserDetailViewV1(RetrieveUpdateDestroyAPIView):
queryset = User.objects.all()
serializer_class = UserSerializerV1
# Only the list endpoint changes in v2.
@endpoint("users/", version=2, view_name="user-list")
class UserListViewV2(UserListViewV1):
serializer_class = UserSerializerV2With API_MIN_VERSION = 1 and API_MAX_VERSION = 2, the resulting URL patterns are:
| URL | Registered view |
|---|---|
/api/v1/users/ | UserListViewV1 |
/api/v2/users/ | UserListViewV2 |
/api/v1/users/<int:pk>/ | UserDetailViewV1 |
/api/v2/users/<int:pk>/ | UserDetailViewV1 |
The v1 list declaration generates both v1 and v2 routes. The later v2 declaration replaces the v2 implementation.
The detail declaration has no v2 replacement, so its implementation is carried forward.
Now:
- A request to
/api/v1/users/is handled byUserListViewV1. - A request to
/api/v2/users/is handled byUserListViewV2. - A request to
/api/v1/users/42/is handled byUserDetailViewV1. - A request to
/api/v2/users/42/is also handled byUserDetailViewV1.
Python class inheritance and route carry-forward are separate mechanisms:
UserListViewV2inherits Python behavior fromUserListViewV1.- The package generates the v2 detail route from the v1 declaration because no later declaration replaces it.
Scope: routing only. The package selects the view callable registered for each URL. It does not merge serializers, inherit business logic between unrelated view classes, or guarantee that the response contract remains backward compatible.
Reverse URLs with view_name
If you want to reverse a URL by name, provide view_name in the endpoint declaration.
The package does not infer URL names from the Python class name or endpoint postfix. Without view_name, the URL pattern has no name for named reversal.
Import api_reverse from the package:
from django_api_versioning import api_reverseUse one name across versions
You can use the same view_name for the same logical endpoint across multiple versions:
api_reverse("user-list", version=1)
api_reverse("user-list", version=2)The version argument selects the intended registered route.
For a name registered in multiple versions, omitting version raises the package's AmbiguousVersionError. The helper requires an explicit version because more than one route matches the name.
Django's standard reverse() function does not provide this package-specific version-selection argument. Use api_reverse() when you need to select a particular registered version.
Use distinct names instead
Distinct names are an alternative to using one name across versions. Do not combine this alternative with the preceding declarations: replace the view_name values in those declarations with version-specific names.
For example, change the decorators to:
@endpoint("users/", version=1, view_name="user-list-v1")
class UserListViewV1(ListCreateAPIView):
queryset = User.objects.all()
serializer_class = UserSerializerV1
@endpoint("users/", version=2, view_name="user-list-v2")
class UserListViewV2(UserListViewV1):
serializer_class = UserSerializerV2Then reverse each name without specifying a version:
api_reverse("user-list-v1")
api_reverse("user-list-v2")A name registered in exactly one version can be resolved without an explicit version. A name registered in multiple versions requires one.
Register an explicit set of versions
Use versions when an endpoint should exist only in selected versions rather than every version from a starting point onward.
For example:
@endpoint(
"beta/",
versions=[1, 2],
view_name="beta-endpoint",
)
class BetaView(ListCreateAPIView):
...This registers the endpoint for v1 and v2.
The list selects versions within the configured API_MIN_VERSION–API_MAX_VERSION range. It does not extend that range.
The validation behavior is important:
- A version below
API_MIN_VERSIONproduces a warning and is skipped. - A version above
API_MAX_VERSIONraisesInvalidVersionError. - Supplying both
versionandversionsraisesVersionRangeError. - Version identifiers must be integers; booleans are not accepted as version values.
For example, versions=[1, 3] requires API_MAX_VERSION to be at least 3.
Group endpoints with app_name
The app_name argument inserts one path segment between API_BASE_PATH and the endpoint postfix.
@endpoint(
"reports/",
version=1,
app_name="internal",
)
class InternalReportView(ListCreateAPIView):
...With the default base path, the resulting URL is:
/api/v1/internal/reports/app_name is a single grouping segment. It is not an arbitrary URL template and does not replace the endpoint's postfix.
Configure API version lifecycle
Version registration and version lifecycle are separate concerns. The package provides LifecycleMiddleware for deprecation and sunset handling.
Configure lifecycle metadata in Django settings:
# settings.py
API_VERSION_LIFECYCLE = {
1: {
"status": "deprecated",
"deprecated_at": "2026-12-01T00:00:00+00:00",
"sunset": "2027-06-01",
},
2: {
"status": "active",
},
}The date above is an example future date relative to this article's publication. Replace it with the actual sunset date for your API before deploying the configuration.
Add the middleware to Django's MIDDLEWARE setting:
MIDDLEWARE = [
# Other middleware
"django_api_versioning.lifecycle.LifecycleMiddleware",
]For a request whose version has matching lifecycle metadata, the middleware behaves as follows:
| Lifecycle metadata | Behavior |
|---|---|
status="active" | No lifecycle headers are added |
status="deprecated" with deprecated_at | Adds the RFC 9745 date-valued Deprecation header |
status="deprecated" with deprecated_at and a future sunset | Adds Deprecation and a Sunset HTTP-date header |
Any valid lifecycle entry with a past sunset | Returns 410 Gone before calling the downstream view |
status="retired" with a future sunset | Continues serving the version and adds Sunset; also adds Deprecation when deprecated_at is configured |
The middleware identifies the version using this priority:
request.version, if it is already available.request.resolver_match.kwargs["version"], if the URL resolver provides it.- The static URL path, parsed using
API_BASE_PATH.
For the package's generated segment-based URLs, the static path fallback allows the middleware to identify the version from paths such as /api/v1/users/.
There is an important integration detail for DRF: Django middleware runs before an APIView initializes DRF's request object and applies its versioning class. Do not assume that a DRF-populated request.version will be available to this middleware during the request's initial middleware phase. The package's static segment paths can be parsed through API_BASE_PATH.
Deprecation header standards
A version configured as deprecated must include deprecated_at as an ISO 8601 date or datetime. The middleware emits that date as the RFC 9745 Structured Field value, for example:
Deprecation: @1796083200This is the date-valued form specified by RFC 9745, not the legacy Deprecation: true value. Check the v1.0.0 lifecycle implementation for the exact header behavior.
The Sunset header is generated as an HTTP-date from the configured ISO date. The middleware returns 410 Gone when the configured sunset date is in the past. That enforcement is based on the date, not on status="retired".
The middleware does not add a Link header pointing to a deprecation policy or migration guide. Implement those links in your application when clients need a migration guide or resource-specific successor URL.
Verify the setup
A basic integration test can verify that the package registered the expected routes and that each version uses the intended serializer.
The following example assumes:
- Your test project has a
UserFactorythat creates a validUserrecord. - The factory creates a user with a non-empty
phonevalue. - DRF pagination is configured with
PageNumberPagination. UserSerializerV1does not exposephone, whileUserSerializerV2does.
Configure pagination in Django settings:
REST_FRAMEWORK = {
"DEFAULT_PAGINATION_CLASS": (
"rest_framework.pagination.PageNumberPagination"
),
"PAGE_SIZE": 10,
}Then write the test:
# tests/test_api_versions.py
from django.test import TestCase
from rest_framework.test import APIClient
from api.tests.factories import UserFactory
class APIVersioningTestCase(TestCase):
def setUp(self):
self.client = APIClient()
UserFactory()
def test_v1_endpoint(self):
response = self.client.get("/api/v1/users/")
self.assertEqual(response.status_code, 200)
def test_v2_endpoint(self):
response = self.client.get("/api/v2/users/")
self.assertEqual(response.status_code, 200)
def test_v1_does_not_expose_phone(self):
response = self.client.get("/api/v1/users/")
self.assertEqual(response.status_code, 200)
self.assertNotIn("phone", response.data["results"][0])
def test_v2_exposes_phone(self):
response = self.client.get("/api/v2/users/")
self.assertEqual(response.status_code, 200)
self.assertIn("phone", response.data["results"][0])Replace the factory import with the factory used by your project. The important requirement is that the test database contains at least one valid user.
If your project uses a different pagination class or response structure, adapt the response.data["results"] assertions accordingly.
These tests verify both route availability and the serializer contract exposed by each version. A status-code-only test would not establish that v1 and v2 actually expose the intended response fields.
If both versioned requests return 200 but expose the same serializer fields, check the following:
- The v2 view has its own
@endpointdeclaration. - The v2 declaration uses the expected
version. - The module containing the v2 view is imported before
api_urlpatterns. API_MAX_VERSIONincludes v2.- The v2 view uses the intended serializer.
The package generates routes at registration time. A missing import or an incorrect version declaration can therefore result in a route that was never registered.
What the package does not provide
django-api-versioning handles URL registration, provides api_reverse(), and includes lifecycle middleware. It does not implement every part of a production API versioning policy. See the canonical routing contract for its routing responsibilities and limitations.
Version discovery
The package does not provide a discovery endpoint such as /api/versions/.
If clients need to discover supported versions, implement an application-level endpoint that returns the version information clients need.
OpenAPI documentation
The package does not generate OpenAPI documentation or automatically create separate API schemas for each version.
Use a documentation tool such as drf-spectacular or drf-yasg and configure it for your application's versioning strategy.
Direct ModelViewSet support
@endpoint does not support decorating a ModelViewSet class directly.
A ModelViewSet requires an action mapping when it is converted into a view. The package's class-based view registration calls .as_view() without such a mapping.
For package-managed version-aware ViewSet routes, use VersionedRouter. It delegates route generation to DRF's SimpleRouter, registers generated patterns in the package registry, and supports per-route inheritance. To keep routing entirely in DRF, a plain SimpleRouter can be mounted under a version prefix; maintain one router per version when you want to manage those prefixes yourself:
from rest_framework.routers import SimpleRouter
router = SimpleRouter()
router.register("users", UserViewSet, basename="user")For example, package-managed registration looks like this:
from django_api_versioning.routers import VersionedRouter
from .views import UserViewSetV1, UserViewSetV2
router = VersionedRouter()
router.register("users", UserViewSetV1, version=1, basename="user")
router.register("users", UserViewSetV2, version=2, basename="user")
urlpatterns = router.urlsConfigure VersionedRouterVersioning as DRF's DEFAULT_VERSIONING_CLASS when the ViewSets need request.version, reverse_action(), or version-aware hyperlinked serializer URLs. See the dedicated ViewSet guide, plus the DRF documentation for ViewSets and routers.
A manually configured callable such as UserViewSet.as_view({"get": "list"}) can be registered as an individual endpoint, but this does not replace router-based generation of the ViewSet's full set of routes and actions.
Use APIView or generic views with @endpoint when you want to register individual endpoint views. Keep ViewSets with VersionedRouter when you want package-managed inheritance, or use plain DRF routers when you prefer manual per-version routing.
DRF request version configuration
@endpoint registers URL patterns but does not configure DRF's versioning class or automatically populate request.version. For ViewSets registered through VersionedRouter, configure VersionedRouterVersioning to expose the selected namespace through request.version and keep reversal version-aware.
In a standard DRF APIView without a configured versioning class, request.version is None.
If your views depend on request.version, configure DRF versioning separately and make sure its behavior is consistent with your URL structure. See the DRF documentation on versioning.
Migrate from manual per-version routers
If your project currently uses one SimpleRouter per API version, you can migrate ViewSet routing to VersionedRouter without converting the ViewSets into generic views. Moving selected endpoints to @endpoint is a separate per-endpoint decision. Both approaches provide package-managed route carry-forward for their respective view types.
Before: one router per version
Assume UserViewSetV1 and UserViewSetV2 are existing ModelViewSet subclasses:
# urls.py
from django.urls import include, path
from rest_framework.routers import SimpleRouter
from .views import UserViewSetV1, UserViewSetV2
v1_router = SimpleRouter()
v1_router.register(
"users",
UserViewSetV1,
basename="v1-user",
)
v2_router = SimpleRouter()
v2_router.register(
"users",
UserViewSetV2,
basename="v2-user",
)
urlpatterns = [
path("api/v1/", include(v1_router.urls)),
path("api/v2/", include(v2_router.urls)),
]Each router generates its own route set. This is appropriate when the resource relies on ViewSet actions and router-generated URL patterns.
After: register individual endpoint views
If the resource is suitable for generic views, you can replace its router-generated list and detail endpoints with individual declarations.
This is an alternative implementation of the same resource, not an additional set of declarations to keep alongside the router configuration.
# views.py
from django_api_versioning import endpoint
from rest_framework.generics import (
ListCreateAPIView,
RetrieveUpdateDestroyAPIView,
)
from .models import User
from .serializers import UserSerializerV1, UserSerializerV2
@endpoint("users/", version=1, view_name="user-list")
class UserListViewV1(ListCreateAPIView):
queryset = User.objects.all()
serializer_class = UserSerializerV1
@endpoint(
"users/<int:pk>/",
version=1,
view_name="user-detail",
)
class UserDetailViewV1(RetrieveUpdateDestroyAPIView):
queryset = User.objects.all()
serializer_class = UserSerializerV1
@endpoint("users/", version=2, view_name="user-list")
class UserListViewV2(UserListViewV1):
serializer_class = UserSerializerV2
# UserDetailViewV1 is not redeclared for v2.
# Its v1 declaration already generates the v2 route.Update the URL configuration:
# urls.py
from django.urls import include, path
from . import views # noqa: F401
from django_api_versioning.urls import urlpatterns as api_urlpatterns
urlpatterns = [
path("", include(api_urlpatterns)),
]The example preserves the list and detail URL paths, but it does not preserve the original router's URL names. Update any reverse calls, tests, and references that depend on the old names.
Before migrating, check two things.
ViewSets and custom actions need the router integration.
@endpoint cannot decorate a ModelViewSet class directly. Use VersionedRouter when you want package-managed version registration and inheritance for ViewSets and @action methods. Keep plain per-version routers when you intentionally manage URL prefixes yourself or depend on DefaultRouter behavior.
You can convert selected resources to generic views, but that means representing each route as an individual view. A configured ModelViewSet.as_view({...}) callable can also be registered for one route, but it does not replace the router's full route generation.
The views import is required.
The module containing the endpoint declarations must be imported before api_urlpatterns.
The import is intentional even if the imported module is not referenced directly in the URLconf. Its purpose is to execute the decorators and register the routes.
Troubleshooting
VersionTypeError or VersionRangeError during settings initialization
Check that both version settings are integers and that the minimum does not exceed the maximum:
API_MIN_VERSION = 1
API_MAX_VERSION = 2Booleans are not valid version values.
These exceptions are defined by the package in django_api_versioning.exceptions.
InvalidVersionError during endpoint registration
A declaration uses a version greater than API_MAX_VERSION.
For example:
@endpoint("beta/", version=3)
class BetaView(ListCreateAPIView):
...requires:
API_MAX_VERSION = 3Raising API_MAX_VERSION does not automatically create a new implementation for every endpoint. Existing declarations are carried forward, and new declarations can replace selected routes.
AmbiguousVersionError from api_reverse()
This error occurs when the same view_name is registered for multiple versions and api_reverse() is called without an explicit version.
For example:
api_reverse("user-list")If user-list is registered for v1 and v2, select the intended route:
api_reverse("user-list", version=2)The exception is defined by the package and can be imported from:
from django_api_versioning.exceptions import AmbiguousVersionErrorNoReverseMatch from api_reverse()
NoReverseMatch means the requested name/version combination cannot be resolved, or the registered route cannot be reversed with the supplied arguments.
Check that:
- The endpoint declaration includes
view_name. - The requested version is actually registered for that name.
- All required path-converter arguments are supplied.
- The route uses a supported
path()-based pattern.
For example, a detail route containing <int:pk> requires the pk argument:
api_reverse("user-detail", version=2, pk=42)Do not confuse this error with AmbiguousVersionError: an ambiguous name has multiple registered versions and requires an explicit version.
404 on /api/v2/users/
Check the URL configuration and import order:
from . import views
from django_api_versioning.urls import urlpatterns as api_urlpatternsAlso verify that:
API_MAX_VERSIONis at least2.- The endpoint's starting version is not above the configured maximum.
- The module containing the endpoint declaration is imported.
- The URLconf mounts
api_urlpatternsat the intended path. - The endpoint postfix and configured base path produce the URL being requested.
AttributeError for an unknown package attribute
The package's public API exports:
from django_api_versioning import (
api_reverse,
endpoint,
ver_range,
)Do not assume that internal or third-party names such as VersionedEndpoint and register_versioned_routers are part of the package's public API. Verify available imports against the package's current public exports.
Check your imports against the package exports, decorator implementation, and reverse implementation.
Trade-offs and scope
What the package simplifies: unchanged endpoints do not need separate declarations for every API version. A declaration at v1 can generate routes through API_MAX_VERSION, while later declarations replace selected implementations.
What it does not reduce: the number of runtime URL patterns. For the same API surface, the application still needs the corresponding versioned routes.
When the benefit is smaller: a single-version API with no plans for additional versions may be simpler with a hand-written URLconf. A project built primarily around ModelViewSet classes can use VersionedRouter for package-managed inheritance or plain DRF routers for fully manual routing.
The package is most useful when you want to manage individual endpoint implementations across multiple URL-based API versions without repeating declarations for unchanged endpoints. Its route carry-forward behavior should be paired with explicit version-matrix and response-contract tests.
Next steps
Install django-api-versioning, configure the version range, import the endpoint declaration modules before the generated URL patterns, and test the route and response contract for each supported version. The package handles route registration and lifecycle enforcement; discovery and OpenAPI schema generation remain application-level or documentation-tool responsibilities.
For the implementation and version-specific source, see the django-api-versioning repository.