How to Build a RESTful API using the Django REST Framework

The Django REST Framework (DRF) allows you create REST based APIs quickly and simply, providing a range of features such authentication, error handling, serialization and full CRUD  (Create Replace Update Delete) based operations to your database.

Within this article will look at how to create a very basic API. The API will take in a JSON input, validate and return a response back to the client.

Installing DRF

The first thing you will need to do is install DRF.

pip install Django==1.8 
pip install djangorestframework==3.3.2

Once done, lets create a Django project and app.

django-admin startproject myproject
cd myproject/
python startapp myapp

Within your settings file add DRF and your new Django app.

+   'rest_framework',
+   'myapp',

Serializers allow you to build a logical representation of your data, in turn allowing you to,

  • Convert incoming data from JSON to Python data structures and vice-versa.
  • Perform data validation, this is extremely similar to how the Forms and ModelForm classes work within Django.

Below shows you an example. We simply create a serializer, defining a key named ’email’ and a value that must contain a dictionary.

from rest_framework import serializers
 class MyAPISerializer(serializers.Serializer):
    email = serializers.DictField()

Once your data has been passed to your serializer serializer.is_valid()can be called. Errors can then be returned by callingserializer.errors. Below shows can example,

serializer = MyAPISerializer(data={'wrong_key': 'bad_value'})
# False
# {'email': [u'This field is required.']}

Lets next look at First we apply a decorator to only allow GET and POST requests. Our data is then passed to our serializer we check if the data is valid, if it is a HTTP 200 response is returned back to the client. If the data is not valid the error is returned.

from rest_framework.decorators import api_view
def myapi(request):
    serializer = MyAPISerializer(
    if serializer.is_valid():
        return Response(, status=status.HTTP_200_OK)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


Django DRF contains a number of built in exceptions, such as ParseError, ValidationError and PermissionError. When these exceptions are raised, for example due to malformed input, a JSON response with the appropriate details and response code is returned.

Additionally, these exceptions can be manually raised. It is also worth noting, at the point of raising a custom exceptions you can also amend the response details returned.

raise NotAuthenticated(detail='User not authenticated')

Custom exceptions can also be created by subclassing APIException. Below shows an example,

from rest_framework.exceptions import APIException
 class FluxError(APIException):
    status_code = 503
    default_detail = 'Flux Error. Please check flux capacitor'


Based on the previous Django configuration (views, serializers and views) and our new knowledge of the DRF exceptions, lets see some examples. Within our examples we will send the correct input to our API , the incorrect input and then view the returned responses. First all lets run Djangos built in webserver.

python runserver &

Correct Input

Next we use httpie to send in a HTTP POST request. Here we provide the required data, this was previously set

echo '{"email":{"john":"%MINIFYHTMLd512360632b48deea03ec86f69e7f0847%"}}' | http 

[10/Mar/2016 23:17:24]"POST /myapi HTTP/1.1" 200 33
HTTP/1.0 200 OK
Content-Type: application/json
Date: Thu, 10 Mar 2016 23:17:24 GMT
Server: WSGIServer/0.1 Python/2.7.5
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN

    "email": {
        "john": "%MINIFYHTMLd512360632b48deea03ec86f69e7f0848%"

Malformed Input

Now, if we send in some malformed JSON the DRF raises an exception, this generates a HTTP response with the appropriate error.
Below the quotes are removed from ’email’.

echo '{email:{"john":"%MINIFYHTMLd512360632b48deea03ec86f69e7f0849%"}}' | http 

[11/Mar/2016 20:20:56]"POST /myapi HTTP/1.1" 400 107
Content-Type: application/json
Date: Fri, 11 Mar 2016 20:20:56 GMT
Server: WSGIServer/0.1 Python/2.7.5
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN
    "detail": "JSON parse error - Expecting property name enclosed in double quotes: line 1 column 2 (char 1)"

Lets change the data that is sent in. We change the input so that the JSON is different to what is defined within the

echo '{"bananas":{"john":"%MINIFYHTMLd512360632b48deea03ec86f69e7f08410%"}}' | http 

[11/Mar/2016 20:18:35]”POST /myapi HTTP/1.1″ 400 37
Content-Type: application/json
Date: Fri, 11 Mar 2016 20:18:35 GMT
Server: WSGIServer/0.1 Python/2.7.5
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN {
“email”: [
“This field is required.”

Not Found Catchall

Now everything above is all well and good, but what happens when the client sends in a request for a URI that is not configured within ?
Though a 404 is returned (shown below) the response is not presented as JSON due to the request not making it through to DRF, this is because  the error is raised within

echo '{"email":{"john":"%MINIFYHTMLd512360632b48deea03ec86f69e7f08411%"}}' | http | more

[19/Mar/2016 21:49:39]"POST /wronguri HTTP/1.1" 404 2036
<!DOCTYPE html>
<html lang="en">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Page not found at /wronguri</title>
<meta name="robots" content="NONE,NOARCHIVE">

To resolve this a catchall rule is placed within your and assigned to a view that will return the necessary response.
Below is an example of the configuration,

from rest_framework.exceptions import APIException,NotFound
from rest_framework.decorators import api_view

def not_found(request):
    raise NotFound('Resource Not Found')

// (placed at the bottom)
+ url(r'^.*$', 'myproject.views.not_found'),

Lets retest. Now we see that the response is returned as JSON.

echo '{"email":{"john":"%MINIFYHTMLd512360632b48deea03ec86f69e7f08412%"}}' | http

[19/Mar/2016 22:10:44]"POST / HTTP/1.1" 404 31
Content-Type: application/json
Date: Sat, 19 Mar 2016 22:10:44 GMT
Server: WSGIServer/0.1 Python/2.7.5
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN
    "detail": "Resource Not Found"
Rick Donato

Want to become a Django expert?

Here is our hand-picked selection of the best courses you can find online:
The Complete Web Development Bootcamp course
Django Practical Guide course
Django Full Stack Developer Bootcamp
and our recommended certification practice exams:
AlphaPrep Practice Tests - Free Trial