Creating APIs

Creating APIs allows your application to communicate with mobile apps, frontend frameworks, and other systems using JSON data.

In Django, APIs are commonly built using Django REST Framework (DRF).

Step 1: Install and Configure DRF

Install DRF:

pip install djangorestframework

Add it in settings.py:

INSTALLED_APPS = [
...
'rest_framework',
]

Step 2: Create a Model

Example: Product Model

from django.db import modelsclass Product(models.Model):
name = models.CharField(max_length=100)
price = models.IntegerField()
description = models.TextField() def __str__(self):
return self.name

Run migrations:

python manage.py makemigrations
python manage.py migrate

Step 3: Create a Serializer

Serializer converts model data into JSON format.

Create serializers.py:

from rest_framework import serializers
from .models import Productclass ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = '__all__'

Step 4: Create API Views

Option 1: Function-Based API View

from rest_framework.decorators import api_view
from rest_framework.response import Response
from .models import Product
from .serializers import ProductSerializer@api_view(['GET'])
def product_list(request):
products = Product.objects.all()
serializer = ProductSerializer(products, many=True)
return Response(serializer.data)

Add URL

In urls.py:

from django.urls import path
from .views import product_listurlpatterns = [
path('products/', product_list),
]

Now visit:

You will see JSON data.

Step 5: Create Full CRUD API

Using ModelViewSet (Recommended)

In views.py:

from rest_framework import viewsets
from .models import Product
from .serializers import ProductSerializerclass ProductViewSet(viewsets.ModelViewSet):
queryset = Product.objects.all()
serializer_class = ProductSerializer

Register Router

In urls.py:

from rest_framework.routers import DefaultRouter
from .views import ProductViewSetrouter = DefaultRouter()
router.register(r'products', ProductViewSet)urlpatterns = router.urls

Now you automatically get:

GET /products
POST /products
GET /products/1
PUT /products/1
PATCH /products/1
DELETE /products/1

This provides complete CRUD functionality.

Example JSON Response

{
"id": 1,
"name": "Laptop",
"price": 80000,
"description": "High performance laptop"
}

Adding Authentication (Optional)

In settings.py:

REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
]
}

Now only logged-in users can access the API.

Testing APIs

You can test APIs using:

  • Browser (Browsable API)
  • Postman
  • Thunder Client (VS Code extension)
  • Frontend applications

Why Creating APIs is Important

APIs allow:

Mobile app integration
Frontend-backend separation
Third-party integrations
Microservice architecture
Scalable system design

Key Takeaway

Creating APIs in Django REST Framework involves:

  1. Creating a Model
  2. Creating a Serializer
  3. Creating a View or ViewSet
  4. Configuring URLs

DRF simplifies the process and provides automatic CRUD functionality, authentication support, and JSON responses — making backend development fast and professional.

Home » PYTHON FOR WEB DEVELOPMENT (PYWEB) > REST API Development > Creating APIs