In modern web development, APIs are the backbone of communication between front-end and back-end systems. Whether you’re building a mobile app, a JavaScript-based frontend, or a third-party integration, Django Rest Framework (DRF) makes it incredibly easy to build robust and secure REST APIs in Python.
This guide walks you through creating your first REST API step by step using DRF.
1. What is Django Rest Framework (DRF)?
Django Rest Framework (DRF)
is a powerful library built on top of Django that helps developers quickly create APIs for their applications.
Why use DRF?
Automatic serialization and deserialization of data.
Built-in authentication and permission handling.
Easy integration with Django ORM.
Browsable API interface for testing.
2. Install and Set Up DRF
Start with a working Django project. Then install DRF:
pip install djangorestframework
In settings.py, add it to your installed apps:
INSTALLED_APPS = [
...,
'rest_framework',
]
3. Create a Simple Model
Let’s say we’re building an API for blog posts.
In models.py:
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
Run migrations:
python manage.py makemigrations
python manage.py migrate
4. Create a Serializer
Serializers convert model instances to JSON so APIs can return structured data.
In serializers.py:
from rest_framework import serializers
from .models import Post
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = '__all__'
5. Create a View
In views.py:
from rest_framework import viewsets
from .models import Post
from .serializers import PostSerializer
class PostViewSet(viewsets.ModelViewSet):
queryset = Post.objects.all()
serializer_class = PostSerializer
6. Add URLs
In your app’s urls.py:
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import PostViewSet
router = DefaultRouter()
router.register(r'posts', PostViewSet)
urlpatterns = [
path('', include(router.urls)),
]
Now your API endpoints are ready!
Visit:
http://127.0.0.1:8000/posts/
You’ll see a browsable API interface that lets you GET, POST, PUT, and DELETE blog posts easily.
7. Add Authentication (Optional but Recommended)
Enable authentication globally in settings.py:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticatedOrReadOnly',
]
}
Now only authenticated users can modify data while anyone can read it.
You can integrate JWT authentication using libraries like djangorestframework-simplejwt:
pip install djangorestframework-simplejwt
8. Test Your API
Use tools like Postman, Insomnia, or the built-in DRF UI to test endpoints:
GET /posts/ – fetch all posts
POST /posts/ – create a post
GET /posts// – fetch a single post
PUT /posts// – update
DELETE /posts// – delete
đź’ˇ Final Thought
Django Rest Framework makes API development fast, clean, and scalable. Whether you’re building a blog, an eCommerce backend, or a mobile app API, DRF gives you a powerful foundation that’s both developer-friendly and production-ready.
Start small, experiment, and soon you’ll be creating powerful APIs that connect your Django backend with any frontend you can imagine!
Technical Insights
By Abishek Khawas • Nov 08, 2025
Building a REST API with Django Rest Framework