Skip to main content

Django API Versioning logo

Django API Versioning

PyPI version Python versions Django versions Python tests License: MIT

Version your Django API by endpoint, not by duplicating URL patterns. Register a view once; unchanged routes carry forward to later API versions, and a newer declaration replaces the implementation from that version onward.

Explore Django API Versioning — documentation, examples, and interactive playground · Install django-api-versioning from PyPI

See how it works

Install the package and Django REST Framework:

pip install django-api-versioning djangorestframework

Declare only the versions where an endpoint's behavior changes:

from rest_framework.response import Response
from rest_framework.views import APIView

from django_api_versioning import endpoint


@endpoint("orders/", version=1, view_name="order-list")
class OrderListV1(APIView):
    def get(self, request):
        return Response({"total": "19.99"})


@endpoint("orders/", version=2, view_name="order-list")
class OrderListV2(APIView):
    def get(self, request):
        return Response({"total": 1999, "currency": "USD"})

With API_MAX_VERSION = 3, the generated routes are:

Request View
GET /api/v1/orders/ OrderListV1
GET /api/v2/orders/ OrderListV2
GET /api/v3/orders/ OrderListV2

The v1 view serves v1 until another declaration replaces it. The v2 view then serves v2 and v3. You write a new view when the endpoint changes—not another copy of every unchanged route.

For the complete setup, including Django settings and URL configuration, see the Django API Versioning getting-started guide.

What problem does it solve?

In a growing API, only some endpoints change between releases. Manually maintaining a separate URL configuration for every version can repeat registrations and obscure which implementation handles each version.

Django API Versioning makes that mapping explicit with @endpoint:

  • Forward route inheritance: version=1 registers the endpoint from v1 through API_MAX_VERSION.
  • Version-specific replacement: a v2 declaration takes over from v2 onward; earlier versions retain the v1 implementation.
  • Explicit registration: use versions=[...] or ver_range(...) when an endpoint should exist only in selected versions.
  • Visibility: inspect the generated route matrix with python manage.py check_versions --fail-on-empty and guard it with assert_version_matrix(snapshot=".version_matrix.json", strict=True).

This is inheritance of route registrations, not automatic backward compatibility. Your application remains responsible for response schemas, business behavior, and compatibility tests.

How it complements Django REST Framework

DRF and this package solve related but different parts of API versioning:

Responsibility Django REST Framework django-api-versioning
Identify a requested version Built-in schemes such as URL path, namespace, query parameter, hostname, and Accept header Selects registered implementations through concrete URL-path routes; VersionedRouterVersioning exposes the matched ViewSet namespace as request.version
Generate ViewSet routes SimpleRouter and DefaultRouter generate actions and URL patterns VersionedRouter integrates DRF SimpleRouter patterns with the package registry
Select a different implementation per version Application code or separately maintained URL configurations Declarative endpoint registration and per-generated-route inheritance/replacement
Carry unchanged endpoints forward Application-managed Built in to the registry
Inspect and guard the effective route matrix Application-managed check_versions and assert_version_matrix

Use DRF's own versioning schemes when request-version identification is all you need. Use ordinary DRF routers when you want to manage each version's URL configuration yourself. Choose this package when you want a central registry to map concrete URL routes to version-specific implementations and carry unchanged routes forward. For ViewSets, use VersionedRouter; for Django views, APIView, and GenericAPIView, use @endpoint.

What it does not do

  • It does not negotiate versions through request headers, query parameters, hostnames, or tenants. The related channel/resolver settings are reserved and do not activate dispatch.
  • It does not transform serializers or response payloads, guarantee semantic backward compatibility, or replace application contract tests.
  • It does not decorate DRF ViewSet classes directly with @endpoint; use VersionedRouter for package-managed SimpleRouter integration.
  • It does not provide DefaultRouter API-root/format-suffix parity, automatic endpoint-module discovery, or a built-in version-discovery endpoint.
  • It does not generate a separate OpenAPI schema for each API version. The repository's executable OpenAPI example is validation tooling, not a package runtime dependency.

Available since 1.2.0: Explicit endpoint removal is supported through remove_endpoint() for decorator endpoints and VersionedRouter.remove() for generated DRF actions. Earlier versions retain the route, removed versions return 404, and a later declaration can explicitly reintroduce it. Version 1.2.0 also adds set_endpoint_lifecycle() for decorator endpoints and VersionedRouter.set_lifecycle() for DRF operations. The optional EndpointLifecycleAutoSchema integration marks matching deprecated OpenAPI operations. See the endpoint-removal and endpoint-lifecycle design documents.

Why use it with Django REST Framework?

DRF already provides request-version identification and ViewSet route generation. Django API Versioning adds the missing connection between those capabilities: mapping each concrete API version to the correct ViewSet implementation while inheriting unchanged routes independently.

For Django-native function-based and class-based views, use @endpoint. For DRF ViewSet and ModelViewSet classes, use the optional VersionedRouter. It delegates route generation to DRF's SimpleRouter, preserving standard actions, custom @action routes, lookup behavior, and action-level configuration.

The optional VersionedRouterVersioning adapter exposes the selected version as DRF's request.version and keeps reverse_action() and hyperlinked serializers version-aware. DRF remains optional for projects that use only Django-native views. For those projects, install just:

pip install django-api-versioning

Quick configuration

Add the package (and DRF, if used) to INSTALLED_APPS and configure the supported version range:

# settings.py
INSTALLED_APPS = [
    "rest_framework",  # omit when using Django-native views only
    "django_api_versioning",
]

API_MIN_VERSION = 1
API_MAX_VERSION = 3
# API_BASE_PATH defaults to "api/v{version}/"

Import modules containing endpoint declarations before including the generated patterns:

# project/urls.py
from django.urls import include, path

from orders import views as _order_views  # noqa: F401
from django_api_versioning.urls import urlpatterns as api_urlpatterns

urlpatterns = [
    path("", include(api_urlpatterns)),
]

Run the route-matrix check:

python manage.py check_versions

Registration options

Declaration Routes created
@endpoint("orders/", version=2) v2 through API_MAX_VERSION
@endpoint("orders/", versions=[1, 3, 5]) Only v1, v3, and v5
@endpoint("health/") One unversioned route

For an inclusive range, use ver_range(1, 3). Other APIs include api_reverse() for version-aware URL reversal and assert_version_matrix() for regression tests.

See the versioning contract for exact URL construction, import order, collision precedence, validation, and edge cases.

Versioned DRF ViewSets

Use VersionedRouter when you want DRF's normal ViewSet workflow with version-aware implementation dispatch:

# settings.py — configure before importing ViewSet modules
REST_FRAMEWORK = {
    "DEFAULT_VERSIONING_CLASS": (
        "django_api_versioning.routers.VersionedRouterVersioning"
    ),
}
# project/urls.py
from django_api_versioning.routers import VersionedRouter
from orders.views import OrderViewSetV1, OrderViewSetV2

router = VersionedRouter()
router.register("orders", OrderViewSetV1, version=1, basename="order")
router.register("orders", OrderViewSetV2, version=2, basename="order")

urlpatterns = router.urls

With API_MAX_VERSION = 3, the routes are served at /api/v1/orders/, /api/v2/orders/, and /api/v3/orders/. V2 replaces only the generated routes it declares. If a custom action exists in V1 but is not redeclared in V2, that action remains inherited in v2 and v3 while list/detail can use V2.

For endpoints that should exist only in selected versions, pass versions=[1, 3]; v2 remains a 404. The router also supports trailing_slash=False and use_regex_path=False for Django path converters.

The adapter is required when ViewSets need request.version, reverse_action(), or hyperlinked serializer support. You can configure it globally through REST_FRAMEWORK as shown above, or set versioning_class = VersionedRouterVersioning on selected ViewSets.

Scope: the initial integration supports DRF SimpleRouter; it does not claim DefaultRouter API-root or format-suffix parity. It does not transform serializers or response payloads. A route omitted from a later ViewSet remains inherited; remove it explicitly with VersionedRouter.remove() (available since 1.2.0). Header/query/tenant negotiation is not enabled by the reserved settings and remains a separate design decision.

For DRF installation, use a version compatible with your Django release:

# Django 4.2 compatibility
pip install "djangorestframework>=3.17.2,<3.18"

# Django 5.2, 6.0, or 6.1
pip install "djangorestframework>=3.18.3,<3.19"

See the Versioned DRF ViewSet guide for route inheritance, reversal, namespaces, and the exact integration boundary.

For the repository's executable OpenAPI workflow, see the versioned OpenAPI contract example. This consumer example and its oasdiff CI gate were added after the published release; they are repository-level tooling, not part of the package wheel, and do not make drf-spectacular or oasdiff runtime dependencies.

Requirements

  • Python 3.10+
  • Django 4.2+ (retained for compatibility with existing 1.x installations)
  • Django REST Framework is optional.

For new deployments, use a currently supported Django release, preferably Django 5.2 LTS or later. Django 4.2 reached the end of extended support on 7 April 2026. It remains in this package's compatibility matrix for existing projects, but no longer receives Django security fixes. See Django's supported versions.

The configured CI matrix covers selected combinations of Python 3.10–3.15 and Django 4.2, 5.2, 6.0, and 6.1. DRF is tested against versions supported by each upstream combination: DRF 3.17.2 on Django 4.2, and DRF 3.18.x on Django 5.2, 6.0, and 6.1. DRF 3.18 dropped Django 4.2, so these lines are deliberately pinned separately rather than relying on an unbounded latest version.

See tox.ini for configured environments and GitHub Actions for results tied to a specific commit.

Upgrading to 1.0.0

Version 1.0.0 tightened API lifecycle configuration and changed the Deprecation response header format. If you use API_VERSION_LIFECYCLE:

  • Every entry must have a status of active, deprecated, or retired.
  • A deprecated version must define deprecated_at as an ISO 8601 date or datetime.
  • A retired version must define a sunset date.
  • The Deprecation header now uses the RFC 9745 date-valued Structured Field (for example, Deprecation: @1767225600), not the legacy Deprecation: true value.

For migration steps, see the 1.0.0 changelog.

Documentation

Contributing

Bug reports, corrections, and pull requests are welcome. Read the contribution guidelines and security policy.

License

Released under the MIT License.

Metadata

Release files for django-api-versioning 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-api-versioning 1.2.1
File Size Uploaded Provenance Actions
django_api_versioning-1.2.1.tar.gz 83.4 kB With provenance Details for django_api_versioning-1.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-api-versioning 1.2.1
File Interpreter ABI Platform Actions
django_api_versioning-1.2.1-py3-none-any.whl Python 3 none any Details for django_api_versioning-1.2.1-py3-none-any.whl

Total release size: 123.8 kB

Release files / django_api_versioning-1.2.1.tar.gz

Download URL django_api_versioning-1.2.1.tar.gz
Size 83.4 kB
Tags Source
SHA-256 checksum
How to use checksums
70c79950488a98ae9b5fc63e9f369e9916807eadc632723a523d1db57b6f0e1a
BLAKE2b-256 checksum
How to use checksums
3512b119a7f9c4d3c131e7c3dc7a7d2130a5403e25243722cb193ac7638bdac8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.

Transparency log

Release files / django_api_versioning-1.2.1-py3-none-any.whl

Download URL django_api_versioning-1.2.1-py3-none-any.whl
Size 40.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
77ffbc525ddc39d76dcb6b201bb90b2721967d3bede343934fed00e15cb59dec
BLAKE2b-256 checksum
How to use checksums
0e9e996fdfa73833145fd0cef2e5a458674865fffaa9d02d8b92f5f188a95896
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.

Transparency log

Security for django-api-versioning 1.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for django_api_versioning-1.2.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page