Creating Your First API with Django REST Framework

Now that Django REST Framework (DRF) is installed and configured, it’s time to create your first API. In this tutorial, you’ll build a simple API endpoint that returns JSON data to the client.

What is an API Endpoint?

An API endpoint is a URL where clients can send requests and receive responses. For example:

/api/users/
/api/products/
/api/employees/

When a request is made to an endpoint, the server processes it and returns data, usually in JSON format.

Creating Your First API View

Open views.py inside your app:

from rest_framework.views import APIView
from rest_framework.response import Response

class WelcomeAPIView(APIView):
    def get(self, request):
        return Response({
            "message": "Welcome to Django REST Framework",
            "status": "success"
        })

Understanding the Code

  • APIView is the base class for DRF views.
  • get() handles HTTP GET requests.
  • Response() returns data in JSON format automatically.

Configure URL Routing

Create or update urls.py in your app:

from django.urls import path
from .views import WelcomeAPIView

urlpatterns = [
    path('welcome/', WelcomeAPIView.as_view(), name='welcome-api'),
]

Include the app URLs in the project’s urls.py:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include('api.urls')),
]

Run the Server

Start the Django development server:

python manage.py runserver

Open the endpoint in your browser:

http://127.0.0.1:8000/api/welcome/

API Response

The API returns the following JSON response:

{
    "message": "Welcome to Django REST Framework",
    "status": "success"
}

Returning Dynamic Data

You can also return dynamic data:

from rest_framework.views import APIView
from rest_framework.response import Response

class UserAPIView(APIView):
    def get(self, request):
        data = {
            "id": 1,
            "name": "John Doe",
            "email": "john@example.com"
        }
        return Response(data)

Response:

{
    "id": 1,
    "name": "John Doe",
    "email": "john@example.com"
}

Handling Query Parameters

DRF allows you to access query parameters easily:

from rest_framework.views import APIView
from rest_framework.response import Response

class GreetingAPIView(APIView):
    def get(self, request):
        name = request.query_params.get('name', 'Guest')

        return Response({
            "message": f"Hello, {name}"
        })

Request:

/api/greeting/?name=Tarun

Response:

{
    "message": "Hello, Tarun"
}

Benefits of Using DRF APIs

  • Returns JSON responses automatically
  • Handles request parsing efficiently
  • Supports authentication and permissions
  • Easy integration with frontend frameworks
  • Provides a browsable API interface

Conclusion

Creating your first API in Django REST Framework is simple. By extending APIView, defining request methods, and returning data using Response, you can quickly build RESTful APIs for web and mobile applications.