Building a Fully-Functioning API with Django and and Sharing on RapidAPI (Part -1)

•

Fri Mar 31 2023

•

14 min read

This guide will walk you through the process of developing an API in Django using the Django REST Framework.

You'll learn how to deploy the API on Heroku, and once it's deployed, we'll show you how to make it available on RapidAPI.

RapidAPI Hub

RapidAPI Hub is a platform that provides access to a vast collection of APIs for a broad range of use cases. Whether you require an API to facilitate sending SMS in your project or any other functionality, you can effortlessly browse through the platform and find a relevant API.

For instance, by searching for "sms" on RapidAPI Hub, you can discover multiple APIs that offer SMS-related services, such as Send SMS. Thus, RapidAPI makes it easy to locate any API you need to accomplish your project objectives.

Loading component...

RapidAPI is incredibly user-friendly, and if you're a developer who wants to list your API on the platform, we'll demonstrate how to do it. So, let's dive right into the guide without any further delay.

Setting up the Django API and Django REST Framework

To create our Django API, we'll set up a new project in Visual Studio Code and install the necessary dependencies.

Firstly, I opened the command prompt and navigated to the directory where I intended to create my Django project. Next, I created a new project named "djanorapidapi" using the command prompt.

We need to set up our API to return a response using the Django REST framework. To do so, we must ensure that we have installed the Django REST framework, a required dependency for building RESTful APIs with Django.

To install the Django REST framework, we'll utilize the pip package manager. In the command prompt, we can install the framework by typing the following command:

pip install djangorestframework

If the framework is installed, we will see a message stating, "Requirement already satisfied." However, if it is not installed, we will need to download the necessary files and install them.

In my case, I have already installed the Django REST framework and received the "Requirement already satisfied" message. With that, we can proceed to the next step.

Configuring Django Project Requirements

To begin with, I will navigate to the "apiapp" directory and create a new file named "serializer.py". The purpose of this file is to serialize our data. However, we will come back to this file later.

The next step is to configure the essential elements required for Django projects. To utilize the Django framework, we must promptly focus on configuring some key elements.

The code in the "settings.py" file serves as a partial configuration file for a Django web application. The file specifies various options, including the project's base directory, secret key, installed applications, middleware classes, and template engine configuration.

These settings are crucial for properly configuring and securing a Django application. Here is the code:

py
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings unsuitable for production
# see https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-)u%igrqo6$s b3I%m(h%4l jehnkkq5ues)lbzm9(z=7zm-htk'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'yourproject.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator'},
{'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator'},
{'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator'},
]
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = '/static/'
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

Integrating Django REST Framework App in "settings.py" File

In your Django project's "settings.py" file, you can enable the Django Rest Framework (DRF) and add authentication support to your API application.

This can be performed by adding rest_frameworkand apiapp to the INSTALLED_APPS list, and adding rest_framework.authentication.TokenAuthentication to the DEFAULT_AUTHENTICATION_CLASSES list in the REST_FRAMEWORK configuration block.

This allows you to enable token-based authentication for your API, which lets clients authenticate by sending an access token with each request.

py
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings unsuitable for production
# see https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-)u%igrqo6$s b3I%m(h%4l jehnkkq5ues)lbzm9(z=7zm-htk'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'apiapp'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'yourproject.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator'},
{'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator'},
{'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator'},
]
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = '/static/'
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# Rest Framework
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication',
]
}

Creating a New Model in "models.py" File

After configuring the Django REST framework app, we can proceed to the "models.py" file. Here, we will create a new model which will be very simple in structure.

The code defines a simple Django model called Student that extends the base models.Model class. The Student model has two fields: name and age, both of which are required. name is a character field with a maximum length of 100 characters, while age is an integer field.

The __str__() method is also defined, which specifies the string representation of a Student instance. In this case, it returns the name field of the Student object. This model can be used to store information about students in a database.

py
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
def __str__(self):
return self.name

Migrating the Database with "manage.py makemigrations"

The next step in the process is to migrate the database. This is done by running the command prompt manage.py makemigrations. This will save the "student" model changes into the "0001_initial.py" file in a format that Django can recognize.

The following is an example of the code generated in the "0001_initial.py" file after running the migration command:

py
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = []
operations = [
migrations.CreateModel(
name='Student',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('age', models.IntegerField()),
],
),
]

Migrating the Database and Creating a Superuser

This step involves writing some commands to migrate the Django models to the database, creating a superuser for the Django admin, running the server, and registering the Student model in the admin site.

The first command, python manage.py migrate is used to apply the pending migrations to the database, which includes creating tables for the models specified in models.py.

The second command, python manage.py createsuperuser is used to create a superuser account to access the Django admin site. You will be prompted to enter the superuser's username, email, and password.

The third command, python manage.py runserver starts the development server so you can access the application in the browser.

java
python manage.py migrate
python manage.py createsuperuser
Username (leave blank to use 'larat'): admin
python manage.py runserver

In the admin.py file, the register() method registers the Student model with the Django admin site. This allows you to view, add, edit, and delete instances of the Student model through the admin interface.

js
from django.contrib import admin
from .models import Student
admin.site.register(Student)

Adding data to the student model through the admin panel

To add data to the student model, we need to access the admin panel of our Django web application via this link. Once logged in, we can see the student model from our API app.

We can then add a new object by entering the required fields, such as the name and age of the student. For example, we can add an object named "Tim" with an age of 27. After entering the details, we can save the object, which will be added to the student model in our database. This data can now be retrieved and used in our Django REST API.

Serializing data and creating an endpoint for the API

Next, we need to serialize the data and create an endpoint for the API. To do this, we first need to modify the urlpatterns in the "urls.py" file.

The urlpatterns lists the URL patterns the Django application will respond to. Each pattern is represented by a path() function, which takes the URL pattern as the first argument and a view function as the second argument.

By including the "rest_framework.urls" module, we can use the built-in views for login and logout provided by the Django REST framework. This will allow users to authenticate themselves when accessing our API.

py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('api-auth/', include('rest_framework.urls')),
path('', include('apiapp.urls')),
]

Next, we can navigate to the "serializers.py" file and import the Serializers module from the REST framework. It involves creating a serializer for the Student model, which will convert Student objects into JSON format. The serializers.ModelSerializer class is utilized as the base class for the serializer.

The Meta class is also defined within the serializer to specify the model and fields that should be included in the serialized output. In this example, the serializer only consists of two fields from the Student model: name and age.

py
from rest_framework import serializers
from .models import Student
class StudentSerializer(serializers.ModelSerializer):
class Meta:
model = Student
fields = ('name', 'age')

Creating a Simple View to Serialize Objects

Let's create a simple view to retrieve objects from the serializer, serialize the data, and then send it as a response whenever a specific URL is queried.

Firstly, we will import render, then we need to import APIView and Response. We will also need to import the StudentSerializer and the Student model.

Next, we will set up a class named FirstView, which will inherit from the APIView class. Inside this class, we will create a get() method which will include self, request, *args, and **kwargs parameters.

After that, we will define a query set to retrieve all objects from the Student model using Student.objects.all().

Then, we will set up a serializer variable that will inherit from the StudentSerializer class and take in the query set, which will be all the objects in the Student model.

Finally, we will return a Response with the serialized data from the serializer variable using serializer.data. The code will look like this:

py
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .serializers import StudentSerializer
from .models import Student
class FirstView(APIView):
def get(self, request, *args, **kwargs):
# retrieve all objects from the Student model
qs = Student.objects.all()
# serialize the data using the StudentSerializer class
serializer = StudentSerializer(qs, many=True)
# send the serialized data as a response
return Response(serializer.data)

Setting up URL pattern for FirstView

To set up a URL pattern for the FirstView defined in the views.py module, we use the urlpatterns list in the urls.py file. We can match the root URL of the apiapp application by using the empty string ''.

We call the as_view() method on the FirstView class to create a view function associated with this URL pattern. This method returns a callable that can be used as a view function.

Finally, we provide the name argument to give the URL pattern a name, which can be used to refer to it in other parts of the Django application. We can set up the URL pattern in the urls.py file as follows:

py
from django.urls import path
from .views import FirstView
urlpatterns = [
path('', FirstView.as_view(), name='first_view'),
]

To run the project, enter the following command in the command prompt:

python manage.py runserver

After running the server, navigate to the corresponding URL in a web browser to check if the API is working correctly. The expected response is as follows:

HTTP 200 OK Allow: GET, HEAD, OPTIONS Content-Type: application/json Vary: Accept { "name": "Tim", "age": 27 }

Let's add a new student through the admin interface; we need to log in to the Django admin site and navigate to the Students section. Then click the Add Student button, and fill in the necessary details for the new student, such as name and age. Once done, click the Save button to add the student.

After adding the new student, we can go back to our API endpoint and test it again, and this time we should see both students in the response. The response could look like this:

HTTP 200 OK Allow: GET, HEAD, OPTIONS Content-Type: application/json Vary: Accept [ { "name": "Tim", "age": 27 }, { "name": "Tom", "age": 16 } ]

Creating an Additional Endpoint in the views.py file

Now that we have created our simple API, let's add one more endpoint to our "views.py" file before we proceed to host it on Heroku. This endpoint will retrieve all the students from the database and serialize the data before returning a response.

The new endpoint is defined as follows:

py
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .serializers import StudentSerializer
from .models import Student
class FirstView(APIView):
def get(self, request, *args, **kwargs):
qs = Student.objects.all()
serializer = StudentSerializer(qs, many=True)
return Response(serializer.data)
class SecondView(APIView):
def get(self, request, *args, **kwargs):
qs = Student.objects.all()
serializer = StudentSerializer(qs, many=True)
return Response(serializer.data)

Next, we need to update our "urls.py" file with the new endpoint. Here, we added a new URL pattern for the SecondView endpoint at the second-endpoint/ URL. The as_view() method is called on the SecondView class to create a view function that can be associated with the new URL pattern.

Finally, the name argument is provided to give the new URL pattern a name, which can be used to refer to it in other parts of the Django application.

py
from django.urls import path
from . import views
urlpatterns = [
path('', views.FirstView.as_view(), name='first'),
path('second-endpoint/', views.SecondView.as_view(), name='second'),
]

We can test the second endpoint by accessing the URL https://127.0.0.1:8000/second-endpoint, which will respond similarly to the first endpoint. The response will have an HTTP status code of 200, and its content type will be in JSON format. Here is a similar response for the second endpoint:

HTTP 200 OK Allow: GET, HEAD, OPTIONS Content-Type: application/json Vary: Accept [ { "name": "Tim", "age": 27 }, { "name": "Tom", "age": 16 } ]

Wrap Up

To sum up, this guide has covered the basics of creating a simple API using the Django REST Framework. It has provided a detailed, step-by-step walkthrough of creating a model, serializer, and endpoints for GET requests.

The second part of the guide will focus on deploying the Django REST Framework on Heroku and RapidAPI, and you can access it through this link. By following these steps, you can create your API and make it easily accessible to others through RapidAPI.