How to make Token-Based Auth Rest API using Django rest framework

Photo by Pakata Goh on Unsplash

Hello, Django Developers..!!

Rest API is an important aspect as we see it from a backend point of view. There are many frameworks from where you can make rest API. Django is one of the powerful frameworks to build an API. It provides you a cool library that will help you to build an easy token-based or session-based authentication system. It has many other built-in packages too that will make the developer’s work easy. Let’s build a Django Rest API auth system with all endpoints. Grab your seat and take a cup of coffee and start this Django project.

Set-up of Django Rest Auth API

1. Create a virtual environment and name it “Auth”.
2. Activate the environment.
3. Install Django.
4. Install Django Rest Framework.
You will see something like this

I use Linux so the output is something like this.
5. Start the Django project: django-admin startproject AuthProject
6. Move to the directory “AuthProject”
7. write the command in terminal: python startapp mainApp

The tree format of this project is:
| — AuthProject
| | —
| | — __pycache__
| | | — __init__.cpython-38.pyc
| | ` — settings.cpython-38.pyc
| | —
| | —
| | —
| ` —
| — mainApp
| | —
| | —
| | —
| | — migrations
| | ` —
| | —
| | —
| ` —
` —

8. Install one more package by using this command: pip install django-rest-auth

Code of Django Authentication REST API

Now open this project in your favorite text editor.
Update the Installed Apps in

INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles',#third part apps'rest_framework','rest_framework.authtoken','rest_auth',#Apps'mainApp',]

Now open the in the “AuthProject” folder. and update the following code accordingly.

from django.contrib import adminfrom django.urls import path, includeurlpatterns = [path('admin/',,path('rest-auth/', include('rest_auth.urls'))]

We are good to go for now. Open the terminal and migrate the apps.
python makemigrations
python migrate
python runserver
Something like this:

Now if you want to enable the standard registration process then you have to install some other packages too. So, let’s install it first.

pip install django-rest-auth[with_social]

After the completion of this package. Now, add some third-party apps in Installed apps in
And set the SITE_ID = 1.

INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles',#third part apps'rest_framework','rest_framework.authtoken','rest_auth','django.contrib.sites','allauth','allauth.account','rest_auth.registration',#Apps'mainApp',]SITE_ID = 1

For social authentication, you can add the apps too. Because we already installed the desired package.
Change the too. Add one line of code to it.

urlpatterns = [path('admin/',,path('rest-auth/', include('rest_auth.urls')),path('rest-auth/registration/',include('rest_auth.registration.urls'))]

Now migrate once again and start the localhost server. The API endpoints are:

1. User Registration

2. User Login

3. User Logout

4. Change Password

5. Password Reset

6. Password Reset Confirmation

7. User

Congrats..!! you successfully made a Django Rest API Authentication System. It’s a simple Django project with simple python code. Now you can handle the authentication of Django REST API.

Thank You.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store