django-rest-framework: api versioning

By | February 18, 2014

Django Rest Framework is a very solid api framework, but it doesn’t provide out-of-box versioning mechanism, here is my attempt to implement version specific APIs.

The goal is to achieve something like


http://localhost:8000/api/(resource)/
http://localhost:8000/api/v1/(resource)/

plus allowing clients to specify the version in request header (X-Version), here is how we did it.

Structure in side the API app:


├── __init__.py
├── middlewares.py
├── urls.py
├── v1
│ ├── __init__.py
│ ├── account
│ │ ├── __init__.py
│ │ ├── serializers.py
│ │ └── views.py
│ └── urls.py
└── v2
├── __init__.py
├── account
│ ├── __init__.py
│ ├── serializers.py
│ └── views.py
└── urls.py

project urls.py:

api app level urls.py:

version level urls.py

create a middleware to switch to the correct code by changing the path_info, please note there is a caveat that namespace (‘api’) defined in project level urls is not flexible and needs to be known in middleware:

Sample url:

My original answer on stackoverflow.

2 thoughts on “django-rest-framework: api versioning

Leave a Reply

Your email address will not be published. Required fields are marked *