Article 022
What Is django-api-versioning and How Does It Work?
Understand how django-api-versioning registers versioned Django URLs, carries unchanged endpoints forward, resolves route conflicts, and integrates with DRF.
django-api-versioning on PyPI is a Django package that registers URL patterns from view declarations. Instead of manually repeating URL registrations for unchanged endpoints across API versions, you declare an endpoint at the version where its implementation becomes active.
The package uses a registry to turn those declarations into concrete URL patterns. A declaration with version=1, for example, covers version 1 through API_MAX_VERSION unless another declaration takes over at a later version.
This is the central idea behind django-api-versioning: declare an implementation once, then let the registration layer carry it forward across the configured version range.
The package is maintained by the author of this article. Implementation details are linked to the current source code and canonical routing contract.
This article explains the registration model, shows how to use it in a Django REST Framework project, and identifies the boundaries between package behavior and framework behavior.
Scope: routing only. The package registers versioned URL patterns and provides version-aware reverse lookup. It does not decide whether an API change is backward compatible, implement DRF version negotiation, or generate version-specific API schemas.
The problem: repeating unchanged route registrations
A common approach to URL-based API versioning is to maintain a separate DRF router for each API version.
For example, a project with three versions might register its existing ViewSets like this:
# urls.py
from django.urls import include, path
from rest_framework.routers import SimpleRouter
from .views import (
UserViewSetV1,
UserViewSetV2,
UserViewSetV3,
PostViewSet,
CommentViewSet,
)
v1_router = SimpleRouter()
v1_router.register("users", UserViewSetV1, basename="v1-user")
v1_router.register("posts", PostViewSet, basename="v1-post")
v1_router.register("comments", CommentViewSet, basename="v1-comment")
v2_router = SimpleRouter()
v2_router.register("users", UserViewSetV2, basename="v2-user")
v2_router.register("posts", PostViewSet, basename="v2-post")
v2_router.register("comments", CommentViewSet, basename="v2-comment")
v3_router = SimpleRouter()
v3_router.register("users", UserViewSetV3, basename="v3-user")
v3_router.register("posts", PostViewSet, basename="v3-post")
v3_router.register("comments", CommentViewSet, basename="v3-comment")
urlpatterns = [
path("api/v1/", include(v1_router.urls)),
path("api/v2/", include(v2_router.urls)),
path("api/v3/", include(v3_router.urls)),
]Here, PostViewSet and CommentViewSet are unchanged, but they are registered with each version's router. Each router generates routes under its own prefix.
This is a valid DRF approach. However, SimpleRouter.register() registers a ViewSet, which can generate several URL patterns for its actions. It is not necessarily a one-call-per-HTTP-endpoint mechanism.
django-api-versioning uses a different registration model. It registers individual view implementations through @endpoint and carries a declaration forward through the configured version range.
The two approaches organize URL registration differently. They should not be compared by counting router registrations against decorator declarations unless the examples expose exactly the same API surface.
For the router behavior and generated routes, see the DRF routers documentation.
The registration model
The package's public decorator is imported from the top-level namespace:
from django_api_versioning import endpointThe decorator's public signature is:
from typing import Callable, TypeVar
ViewT = TypeVar("ViewT", bound=Callable)
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 arguments control the route being registered:
postfix: The endpoint path after the version prefix, such as"users/"or"users/<int:pk>/".version: The starting version for a forward-extending segment.versions: An explicit list of versions in which the endpoint should be registered.app_name: An optional grouping segment inserted between the version prefix and the postfix.view_name: An optional Django URL pattern name.
version and versions are mutually exclusive. If neither is supplied, the endpoint is registered as an unversioned route.
For example:
from django_api_versioning import endpoint
from rest_framework.generics import ListAPIView
@endpoint("users/", version=1, view_name="user-list")
class UserListView(ListAPIView):
...With API_MAX_VERSION = 3, this declaration registers the view for versions 1, 2, and 3.
Version segments
A declaration using version=N starts a segment at version N and extends it through API_MAX_VERSION.
For example, with:
API_MIN_VERSION = 1
API_MAX_VERSION = 4this declaration:
@endpoint("posts/", version=1)
class PostListViewV1(ListAPIView):
...registers the view for:
/api/v1/posts/
/api/v2/posts/
/api/v3/posts/
/api/v4/posts/If a new implementation is declared at version 3:
@endpoint("posts/", version=3)
class PostListViewV3(ListAPIView):
...the resulting route ownership is:
| Requested version | Implementation |
|---|---|
| v1 | PostListViewV1 |
| v2 | PostListViewV1 |
| v3 | PostListViewV3 |
| v4 | PostListViewV3 |
The v3 declaration takes over from version 3 onward. The earlier implementation continues to serve versions 1 and 2.
This is registration-time behavior. The package generates concrete URL patterns from the registry; it does not search through earlier implementations every time a request arrives.
How route conflicts are resolved
Multiple declarations can target the same route. The registry uses each declaration's source_version to determine which implementation owns a version.
When two registrations collide on the same route:
- If both declarations have integer
source_versionvalues, the higher source version wins. - If their source versions are equal, the last registered declaration wins.
- If either source version is
None, the last registered declaration wins.
For example:
@endpoint("orders/", version=1)
class OrderListViewV1(ListAPIView):
...
@endpoint("orders/", version=3)
class OrderListViewV3(ListAPIView):
...The v3 declaration owns versions 3 and 4, while the v1 declaration remains active for versions 1 and 2.
This is distinct from Python class inheritance. The package's segment behavior determines which view is registered for each URL; Python inheritance determines which implementation details a class reuses from its parent.
For the complete registration and collision contract, see the canonical versioning contract.
Explicit version selection
Not every endpoint should necessarily remain available in every later version.
For non-contiguous version selection, use versions:
from django.http import JsonResponse
from django_api_versioning import endpoint
@endpoint("ranking/", versions=[2, 4])
def ranking(request):
return JsonResponse({"algorithm": "new"})With a configured maximum version of 5, the route exists in v2 and v4 only.
| Requested version | Result |
|---|---|
| v1 | No matching route |
| v2 | ranking |
| v3 | No matching route |
| v4 | ranking |
| v5 | No matching route |
For a contiguous, bounded set, use ver_range():
from django.http import JsonResponse
from django_api_versioning import endpoint, ver_range
@endpoint("legacy/", versions=ver_range(1, 3))
def legacy_endpoint(request):
return JsonResponse({"deprecated": True})ver_range(1, 3) returns [1, 2, 3]. The route is not registered for v4 or later.
This is useful when an endpoint is intentionally available only in a defined set of API versions.
Settings
The version range is controlled by Django settings:
# settings.py
API_MIN_VERSION = 1
API_MAX_VERSION = 3API_MIN_VERSION defines the minimum supported route version, and API_MAX_VERSION defines the upper boundary for forward-extending declarations.
The default API_MAX_VERSION is 1. A project that declares version 2 or higher must increase it.
The package distinguishes between versions below the configured minimum and versions above the configured maximum:
- A declaration starting below
API_MIN_VERSIONproduces a warning and registers no route. - A declaration above
API_MAX_VERSIONraisesInvalidVersionError. - Supplying both
versionandversionsraisesVersionRangeError. - For explicit
versions, values below the minimum are skipped with a warning; values above the maximum are invalid.
The exception classes are defined in django_api_versioning.exceptions:
from django_api_versioning.exceptions import (
InvalidVersionError,
VersionRangeError,
)The package uses integer version identifiers. For example, use version=2, not version="v2".
URL prefix
The default API_BASE_PATH is:
API_BASE_PATH = "api/v{version}/"The {version} placeholder is required. The package substitutes the version number when generating each versioned URL.
For example:
@endpoint("users/", version=1)
class UserListView(ListAPIView):
...generates:
/api/v1/users/
/api/v2/users/
/api/v3/users/You can move the common api/ prefix into the project's URLconf instead:
# settings.py
API_BASE_PATH = "v{version}/"# urls.py
from django.urls import include, path
# Import endpoint modules before reading the registry.
from . import views # noqa: F401
from django_api_versioning.urls import urlpatterns as api_urlpatterns
urlpatterns = [
path("api/", include(api_urlpatterns)),
]The resulting versioned URLs still begin with /api/.
The app_name argument inserts one additional path segment:
@endpoint("reports/", version=1, app_name="internal")
class InternalReportView(ListAPIView):
...With the default prefix, the generated path is:
/api/v1/internal/reports/app_name is a grouping segment, not an arbitrary URL template.
A complete DRF example
The following example shows a project where the product list response changes in v2, but the detail endpoint remains unchanged.
Step 1: Configure the package
# settings.py
INSTALLED_APPS = [
# Other installed apps
"django_api_versioning",
]
API_MIN_VERSION = 1
API_MAX_VERSION = 2Step 2: Organize and declare the views
A project can keep version-specific implementations in separate modules while placing shared endpoints in a common module. This directory structure is an organizational choice; it does not determine the URL path. The @endpoint arguments and API_BASE_PATH determine the generated routes.
For example:
products/
models.py
serializers.py
views/
__init__.py
common.py
v1.py
v2.pyThe common detail view can live in common.py:
# products/views/common.py
from django_api_versioning import endpoint
from rest_framework.generics import RetrieveUpdateDestroyAPIView
from ..models import Product
from ..serializers import ProductSerializerV1
@endpoint(
"products/<int:pk>/",
version=1,
view_name="product-detail",
)
class ProductDetailView(RetrieveUpdateDestroyAPIView):
queryset = Product.objects.all()
serializer_class = ProductSerializerV1The v1 list implementation:
# products/views/v1.py
from django_api_versioning import endpoint
from rest_framework.generics import ListCreateAPIView
from ..models import Product
from ..serializers import ProductSerializerV1
@endpoint("products/", version=1, view_name="product-list")
class ProductListViewV1(ListCreateAPIView):
queryset = Product.objects.all()
serializer_class = ProductSerializerV1The v2 list implementation:
# products/views/v2.py
from django_api_versioning import endpoint
from rest_framework.generics import ListCreateAPIView
from ..models import Product
from ..serializers import ProductSerializerV2
from .v1 import ProductListViewV1
@endpoint("products/", version=2, view_name="product-list")
class ProductListViewV2(ProductListViewV1):
serializer_class = ProductSerializerV2
def get_queryset(self):
queryset = super().get_queryset()
category = self.request.query_params.get("category")
if category:
queryset = queryset.filter(category=category)
return querysetThe v2 list implementation inherits from the v1 class, but overrides its serializer and queryset behavior.
The detail endpoint has only a v1 declaration. Its route therefore remains available in v2 and continues to use ProductDetailView.
The resulting route ownership is:
| URL | Registered view |
|---|---|
/api/v1/products/ | ProductListViewV1 |
/api/v2/products/ | ProductListViewV2 |
/api/v1/products/<int:pk>/ | ProductDetailView |
/api/v2/products/<int:pk>/ | ProductDetailView |
The package carries the detail route forward. It does not create a new Python subclass or copy the view implementation.
Step 3: Include the generated URL patterns
# project/urls.py
from django.urls import include, path
# Import every module containing @endpoint declarations first.
from products.views import common, v1, v2 # noqa: F401
from django_api_versioning.urls import urlpatterns as api_urlpatterns
urlpatterns = [
path("", include(api_urlpatterns)),
]Importing the modules containing @endpoint declarations is important. The decorators register their views when Python imports those modules. The package does not automatically discover every application view module.
Import all relevant declaration modules before importing the package's urlpatterns. Otherwise, the registry may be incomplete when the URL patterns are read.
Step 4: Check the routes
curl http://localhost:8000/api/v1/products/
curl "http://localhost:8000/api/v2/products/?category=electronics"
curl http://localhost:8000/api/v2/products/42/The v1 and v2 list URLs resolve to different view implementations. The v2 detail URL still resolves to the shared detail implementation because no later declaration replaces it.
The actual response bodies depend on the serializers, queryset, and view behavior defined by the application.
Reverse URL lookup
The package exports api_reverse() for reverse lookup with an explicit API version:
from django_api_versioning import api_reverse
url = api_reverse(
"product-detail",
version=2,
pk=42,
)The result is:
/api/v2/products/42/When a URL name is registered in multiple versions, an explicit version avoids ambiguity.
If a name is registered in exactly one version, api_reverse() can resolve it without a version argument. If it is registered in multiple versions and no version is supplied, the package raises AmbiguousVersionError.
The exception is defined in django_api_versioning.exceptions:
from django_api_versioning.exceptions import AmbiguousVersionErrorIf no matching name/version exists, or required route parameters are missing, reverse lookup raises Django's NoReverseMatch:
from django.urls import NoReverseMatch
try:
url = api_reverse("product-detail", version=3, pk=42)
except NoReverseMatch:
# No route with this name is registered for v3.
...A declaration without view_name does not provide a named URL for reverse lookup.
Because the same name can exist in multiple generated version patterns, use api_reverse() with an explicit version rather than relying on plain Django reverse() to select the intended version.
What happens when a request arrives?
The package's registration model can be summarized as follows:
- Django imports the modules containing
@endpointdeclarations. - Each decorator registers the route and its view implementation in the package registry.
- The registry resolves collisions according to source-version precedence.
- The package exposes the resulting URL patterns through
django_api_versioning.urls. - Django resolves an incoming request against those generated patterns and dispatches it to the registered view.
Now:
- A request to
/api/v1/products/resolves toProductListViewV1. - A request to
/api/v2/products/resolves toProductListViewV2. - A request to
/api/v2/products/42/resolves toProductDetailView, carried forward from its v1 declaration.
These paths are derived from API_BASE_PATH, the decorator's postfix, and the configured version range. The package does not dynamically traverse Python classes during request dispatch.
This distinction matters when reviewing a versioned API: route ownership is determined during registration, while response behavior is determined by the selected view and the application code it uses.
What the package provides
The package provides a focused routing and URL-registration layer:
- Declarative registration: Views register their URL patterns through
@endpoint. - Forward-extending segments: A declaration at version N covers N through
API_MAX_VERSION, unless a later segment takes over. - Explicit version sets:
versionsandver_range()support non-contiguous and bounded version selection. - URL prefix configuration:
API_BASE_PATHandapp_namecontrol the generated route structure. - Version-aware reverse lookup:
api_reverse()resolves a named route for a specified API version. - DRF ViewSet integration:
VersionedRouterdelegates route generation to DRFSimpleRouter, registers each generated action in the package registry, and supports per-route inheritance and version-aware reversal. - Routing inspection and regression protection:
check_versionsinspects the root URLconf andassert_version_matrix()can enforce a committed, non-empty route snapshot in strict CI mode. - Lifecycle middleware:
LifecycleMiddlewarecan add version-wide deprecation and sunset headers and enforce a configured version-wide sunset date. - Explicit route removal (available since 1.2.0):
remove_endpoint()andVersionedRouter.remove()stop forward inheritance from an inclusive version boundary. - Endpoint-level lifecycle (available since 1.2.0):
set_endpoint_lifecycle()andVersionedRouter.set_lifecycle()add operation-scoped lifecycle headers; the optional drf-spectacular adapter marks matching operations deprecated in OpenAPI.
These features change how the project declares and generates versioned URLs. They do not replace the application behavior implemented by Django or DRF views.
API lifecycle behavior
The package includes LifecycleMiddleware, configured through API_VERSION_LIFECYCLE. For current lifecycle behavior and configuration details, see the lifecycle implementation.
Add the middleware to Django's middleware settings:
MIDDLEWARE = [
# Other middleware
"django_api_versioning.lifecycle.LifecycleMiddleware",
]Then configure lifecycle metadata:
API_VERSION_LIFECYCLE = {
1: {
"status": "deprecated",
"deprecated_at": "2026-12-01T00:00:00+00:00",
"sunset": "2030-01-01",
},
2: {
"status": "active",
},
}The example sunset date is in the future relative to this article's publication context. Replace it with the actual retirement date before deploying the configuration.
For a request whose version is 1, the middleware:
- When
statusisdeprecatedorretiredanddeprecated_atis configured, adds the RFC 9745 date-valuedDeprecationheader. - Adds a
Sunsetheader when the status isdeprecatedorretiredand a sunset date is configured. - Returns
410 Gonewhen the configured sunset date is in the past.
There are important distinctions:
- A status of
retiredalone does not cause a410 Goneresponse. - A past
sunsetdate causes410 Goneregardless of whether the status isdeprecatedorretired. DeprecationandSunsetresponse headers are added only for thedeprecatedandretiredstatuses;activeversions get none.- The middleware does not automatically add a
Linkheader.
The middleware attempts to identify the version from request.version, then from request.resolver_match.kwargs, and finally from the request path using API_BASE_PATH.
In ordinary DRF request processing, middleware runs before the APIView has initialized its DRF request. Therefore, do not assume that a version selected later through DRF header negotiation is already available as request.version when this middleware executes. The path-based extraction is particularly relevant for the package's URL-segment routing.
A deprecated lifecycle entry must include deprecated_at as an ISO 8601 date or datetime. The middleware serializes it as the RFC 9745 date-valued Structured Field (for example, Deprecation: @1796083200); it does not emit the legacy Deprecation: true value.
The Sunset header uses an HTTP-date value. Its meaning and format are defined by RFC 8594. The package converts a valid configured ISO date into an HTTP-date value. Use a valid date and verify the resulting value before relying on it in a production response.
The lifecycle configuration does not replace an application's deprecation and retirement policy. The application must coordinate its deprecation announcement and sunset schedule. The sunset date should not precede the announced deprecation date. Check the current lifecycle implementation for the configuration fields and behavior supported by the package.
What the package does not provide
The package should not be treated as a complete API compatibility or documentation framework.
The package does not provide:
- Header-, query-, host-, or tenant-based route negotiation: the current registry dispatches by concrete URL-path patterns; reserved channel/resolver settings do not activate other request channels.
- Automatic discovery of arbitrary DRF routers or endpoint modules: use
VersionedRouterfor package-managedSimpleRouterintegration and import declaration modules explicitly. Plain DRF routers remain independent of the package registry. DefaultRouterparity:VersionedRouterdoes not implement the API-root or format-suffix behavior of DRFDefaultRouter.- A version-discovery endpoint: It does not generate an endpoint such as
/api/versions/. - Automatic OpenAPI schema generation: Version-specific schemas remain the responsibility of tools such as drf-spectacular. The optional 1.2.0
EndpointLifecycleAutoSchemaadapter marks registered deprecated operations; it does not generate schemas or replace the schema tool. - Direct
ModelViewSetdecoration: AModelViewSetclass cannot be passed directly to@endpoint, because itsas_view()method requires an action mapping. Use the optionalVersionedRouterintegration for package-managed DRFSimpleRouterroutes; a configuredModelViewSet.as_view({...})callable can still be registered for one specific route. - DRF version negotiation: The decorator-based
@endpointAPI does not negotiate request versions or populaterequest.version. For ViewSets, the optionalVersionedRouterVersioningadapter exposes the matched router namespace throughrequest.version; arbitrary header/query/host negotiation remains outside the package. - Semantic backward-compatibility checks: Carrying a route forward does not prove that its response contract remains compatible with earlier clients.
For decorator-registered @endpoint views, configure a compatible DRF versioning scheme separately if the view needs request.version. For ViewSets registered through VersionedRouter, use VersionedRouterVersioning. See the DRF versioning documentation.
URL registration and request-version identification are different responsibilities.
Integration with existing Django projects
For projects using APIView or generic class-based views, migration can involve moving route declarations from per-version URLconfs into the view modules.
A version-oriented directory structure can help keep implementations discoverable:
products/
views/
common.py
v1.py
v2.py
v3.pyThe directory structure is an organizational convention, not a routing rule. The package generates paths from API_BASE_PATH, app_name, and postfix, regardless of where the Python modules live.
The URLconf then includes the package-generated patterns once, after importing the modules containing the declarations.
Projects using ModelViewSet and @action should choose explicitly between package-managed and manually managed routing. The package provides VersionedRouter for DRF SimpleRouter integration: it registers generated actions in the package registry and applies version selection and inheritance per route. Configure VersionedRouterVersioning when ViewSets need request.version, reverse_action(), or version-aware hyperlinked serializer URLs.
There are four practical choices:
- Use
VersionedRouterfor package-managed ViewSet route generation, per-route inheritance, and version-aware reversal. - Keep plain DRF
SimpleRouterorDefaultRouterinstances when the application wants to manage version prefixes and registrations itself; those patterns remain outside the package registry. - Use
@endpointfor independent function-based views, Django class-based views, DRFAPIView, andGenericAPIViewendpoints. - Register a configured
ModelViewSet.as_view({...})callable with@endpointonly when one explicitly mapped route is sufficient; it does not replace full router generation.
The first and third options can coexist in one URLconf. Avoid mounting duplicate URL patterns. VersionedRouter supports SimpleRouter behavior and does not claim DefaultRouter API-root or format-suffix parity.
For the exact ViewSet boundary and the configured-callable limitation, follow the canonical versioning contract.
Trade-offs and scope
What the package reduces
The package reduces repeated URL-registration declarations for endpoints that remain unchanged across API versions. A declaration at its starting version can cover later versions without another URL registration.
This is useful when endpoints evolve at different version boundaries and the team wants route ownership declared near the corresponding view implementation.
What the package does not reduce
The number of runtime URL patterns does not decrease. If an endpoint must be available under three version prefixes, Django still needs the corresponding three URL patterns.
The package changes how those patterns are declared and generated, not the number of URLs exposed by the API.
It also does not eliminate the need to maintain version-specific serializers, permissions, querysets, or business logic when those behaviors change.
When the benefit is smaller
For a small API with one version, a conventional URLconf may be simpler.
The benefit is also smaller when nearly every endpoint changes at every version boundary, because each version still needs its own implementation declaration.
When the benefit matters most
The registration model is most useful when:
- The API has several supported versions.
- Most endpoints remain unchanged between releases.
- Different endpoints evolve at different version boundaries.
- The team wants route ownership to be explicit near each view implementation.
- The team is prepared to review the version matrix and test the behavior of carried-forward implementations.
The key decision is not simply how many API versions exist. It is whether the project's endpoint-change pattern justifies adopting a separate declarative registration model.
Next steps
django-api-versioning focuses on URL registration and reverse lookup. To evaluate its implementation and adopt the declarative registration model, start with the django-api-versioning repository.