DRF Token Authentication and Session Auth Securing API endpoints requires identifying the caller on every request. Django REST Framework ships with built-in authentication mechanisms, the two most prominent being SessionAuthentication (ideal for browser web SPAs on the same domain) and TokenAuthentication (ideal for mobile apps, external clients, and third-party integrations).
Feature SessionAuthenticationTokenAuthenticationCredential Cookie-based session ID (sessionid) Static Bearer Token string CSRF Required? Yes (on all mutating requests)No (immune to CSRF)Best Used For Same-domain React/Vue SPAs Mobile apps, CLI tools, server-to-server APIs Storage django_session tableauthtoken_token table
Add rest_framework.authtoken to INSTALLED_APPS:
1 # config/settings.py
2 INSTALLED_APPS = [
3 # ...
4 "rest_framework" ,
5 "rest_framework.authtoken" , # Creates authtoken_token database table
6 ]
7
8 # Run migration to create token table
9 # python manage.py migrate
Configure TokenAuthentication in global settings:
1 REST_FRAMEWORK = {
2 "DEFAULT_AUTHENTICATION_CLASSES" : [
3 "rest_framework.authentication.TokenAuthentication" ,
4 "rest_framework.authentication.SessionAuthentication" ,
5 ] ,
6 }
DRF provides a built-in view (obtain_auth_token) that validates username and password and returns a token:
1 # config/urls.py
2 from django . urls import path , include
3 from rest_framework . authtoken . views import obtain_auth_token
4
5 urlpatterns = [
6 # POST /api/auth/token/ { "username": "...", "password": "..." }
7 path ( "api/auth/token/" , obtain_auth_token , name = "api-token-auth" ) ,
8 ]
Response: 1 {
2 "token" : "9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b"
3 }
Clients transmit the token in the Authorization HTTP header prefixed with Token:
1 GET /api/v1/projects/ HTTP/1.1
2 Host: api.enterprise.com
3 Authorization: Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b
4 Accept: application/json
Use a Django post_save signal to automatically generate an auth token whenever a new user registers:
1 # accounts/signals.py
2 from django . conf import settings
3 from django . db . models . signals import post_save
4 from django . dispatch import receiver
5 from rest_framework . authtoken . models import Token
6
7 @receiver ( post_save , sender = settings . AUTH_USER_MODEL )
8 def create_auth_token ( sender , instance = None , created = False , ** kwargs ) :
9 if created :
10 Token . objects . create ( user = instance )
Interactive Quiz • Knowledge Check
Practice Quiz What HTTP header and prefix format does DRF TokenAuthentication expect?
A Auth: <token>
B Authorization: Token <token_string>
C Bearer: <token>
D Token: <token>
Why does TokenAuthentication not require CSRF token validation on POST requests?
A TokenAuthentication is insecure
B CSRF attacks rely on browsers automatically attaching cookies; since Authorization headers are not automatically sent by cross-origin browser form posts, TokenAuthentication is inherently immune to CSRF
C Django disables security for tokens
D DRF automatically injects CSRF tokens
What database table is created by adding rest_framework.authtoken to INSTALLED_APPS?
A auth_user
B authtoken_token
C django_tokens
D rest_credentials
What view does DRF provide out-of-the-box to exchange credentials for an auth token?
A login_user()
B obtain_auth_token
C generate_token_view
D auth_handshake()
What is a known limitation of DRF's built-in TokenAuthentication in large-scale enterprise systems?
A Tokens can only be 5 characters long
B Built-in tokens do not expire automatically and require a database lookup on every single request, leading many enterprises to adopt stateless JWTs (SimpleJWT)
C It only works with SQLite
D Tokens can only be used once