RIPPLE: SRAJAYA BHAKTA PRADHANANGA

An API, or application programming interface, is a set of rules or protocols that enables software applications to communicate with each other to exchange data, features, and functionality.

There are 4 different types of APIs namely: 

  • SOAP APIs 
  • RPC APIs
  • WebSocket APIs
  • REST APIs

The development of applications for different platforms requires different frameworks, for frontend React, Angular, etc, for backend Laravel, Go, Python,etc, for mobile applications Flutter, etc however the one thing that remains constant is the use of data. APIs are created for the purpose for easy and understandable transfer for data between these different frameworks. 

REST API stands for Representational State Transfer Application Programming Interface. A REST API follows certain principles to make the communication smooth and efficient between the client and the server. In REST API, the client and server side should be taken as two separate entities. REST API helps applications send and receive data in a structured way, usually using JSON (JavaScript Object Notation). It also uses standard HTTP methods like : 

  • GET – To retrieve data
  • POST – To create new data
  • PUT/PATCH – To update existing data
  • DELETE – To remove data 

Some of the principles of REST API are : 

  • Statelessness: Each request from a client must contain all the information needed to process it.
  • Cacheability: When possible, resources should be cacheable on the client side for faster processing. 
  • Client-server separation: Client and server applications must be completely independent of each other.

Most of the frameworks have a pre-built library that enables users to create APIs. Similarly in django, to create a REST API, we can use a powerful and flexible library known as Django REST framework or DRF. It is an external package that needs to be installed before creating any sort of REST API.

  1. To install it , we need to run the following code in the terminal:
    pip install djangorestframework
  2.  Add the installed library in settings.py:
    INSTALLED APPS=[
              ……
    ‘rest_framework’,]
  3. Create views in views.py:
    from rest_framework.response import Response
    from rest_framework.views import APIView
    class HelloWorld(APIView):
    def get(self, request):
    return Response({“message”: “Hello, World!”})
  4. Create relevant URL:
    from Django.urls import path
    from .views import Views
    urlpatterns=[
    path(‘home/’, HelloWorld.as_view(),name=’Hello’),
    ]

To ensure the working of our API we can use applications like Postman and Swagger. These third-party applications allow us to test our API through endpoints. An API that covers a wide range of test scenarios, returns proper status codes, and handles possible errors is said to be a well-structured API. So when creating an API, developers must think from the perspective of failure rather than that of success. This leads to better code, better test,s and better results.

Swagger API is a set of open-source tools built to help programmers develop, design, document, and use REST APIs. The tool is built around the OpenAPI specification and contains three components: Swagger Editor, Swagger UI, and Swagger Codegen.

To integrate swagger in django we can the following:

  1. Install python package for drf-spectacular
    pip install drf-spectacular
  2. Add drf-spectacular to our installed apps
    INSTALLED APPS=[
              ……
    ‘drf_spectacular’,]
  3. In the root project urls , the following needs to be added:

from django.urls import include, path
from rest_framework import routers
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView

urlpatterns = [
path(‘’,include(‘myapp.urls’)),


path(‘admin/’, admin.site.urls),
path(‘api/schema/’, SpectacularAPIView.as_view(), name=’schema’),
# Optional UI:
path(‘api/schema/swagger-ui/’,SpectacularSwaggerView.as_view
(url_name=’schema’), name=’swagger-ui’),
path(‘api/schema/redoc/’, SpectacularRedocView.as_view(url_name=’schema’), name=’redoc’),
]

The photo illustrates the implementation of swagger in django. It shows all the different HTTP methods that can be executed.

Knowing REST API is essential for building efficient, scalable, and modern applications that can integrate with other systems and services. It allows developers to create scalable applications which can be incorporated into multiple frameworks easily.

Leave a Reply

Your email address will not be published. Required fields are marked *