Integrating SendGrid With Django Rest Framework

Whenever we develop Web or Mobile Applications, there is always a need for sending emails for various reasons through the developed platform. Some of the reasons why we need to integrate email service are:

  1. Sending email for email id verification.
  2. Sending an email to the registered email id to regain access after the loss of password.
  3. Sending an invitation email to join the platform.

You can find a few use cases here.

There are multiple services around through which we can integrate Email Service in our platform. This article will be covering the integration of one of the most popular emailing services Twilio’s SendGrid.


This article assumes that you’ve theoretical and practical knowledge of Python, Django Rest Framework, and implementing third party libraries in it.


This article is going to be covering how to set-up a SendGrid account and implement a SendGrid Emailing Service at the server-side.

We will follow the steps below in order:-

  1. Setting up a SendGrid account.
  2. Designing a dynamic transactional template in SendGrid.
  3. Installing SendGrid library in Django.
  4. Setting up SendGrid in Django.
  5. Creating API for sending available email templates in SendGrid.

Now let’s start the integration process.

Setting Up A SendGrid Account

For getting started with sending email through SendGrid, we will require an API key which will be used in our Django SendGrid integration. We need to create a SendGrid account by Signing up or by Logging in to your SendGrid account. In the left sidebar, select Settings > API keys > Create API Key.

Enter a name for your API key. Choose the API key access whichever is suitable for your requirement. Full Access permissions give the key the ability to perform all the necessary email sending functions. If you prefer to create a key that has very specific permissions, then select “Restricted Access” and configure your access as needed.

When you click the “Create & View” button you will be presented with your key. This key will never be shown again, so you need to click on it to copy it to the clipboard, and then paste it in a secure document so that you can use it later. If you lose your key, you will need to generate a brand new one. Once you have saved your key, you can click the “Done” button.

Note: Do not share your SendGrid API Key with anyone as it may be misused.

Designing A Dynamic Transactional Template

For creating a dynamic template, In the left sidebar, select Email API > Dynamic Templates > Create a Dynamic Template. Enter a name for your template and click on “Save”.

To begin with, editing your new template, click on “Add Version”. Then Select a Design template as per your requirements or you can create new blank templates, Lastly, you need to Select an Editor (how you will be editing the template by Code Editor or by Design Editor)

Now that you have the version we can start editing the template as per our need.

Starting With The Development Process

1. Installation

pip3 install sendgrid

2. Adding required values in file for PayU 

# Twilio Sendgrid API key


# setting default email for sending email through sendgrid


3. Creating a class-based view for sending email using Mail helper Class

import sendgrid

from sendgrid.helpers.mail import (Mail, Email,Personalization)

from python_http_client import exceptions

from twilio_sendgrid_integration.settings import DEFAULT_FROM_EMAIL, SENDGRID_API_KEY

sg = sendgrid.SendGridAPIClient(SENDGRID_API_KEY)

class MailSenderAPIView(GenericAPIView):

def send_mail(self, template_id, sender, recipient, data_dict):

mail = Mail()

mail.template_id = template_id

mail.from_email = Email(sender)

personalization = Personalization()


personalization.dynamic_template_data = data_dict



response =

except exceptions.BadRequestsError as e:







def post(self, request):

recepient_email =['recepient_email']

subject =['subject']

fullname =['fullname']

body =['body']

template_id = "d-1772e8ac6b5442e68975394ea71a4957"


data_dict = {"subject": subject, "user_name": fullname, "body": body}

MailSenderAPIView.send_mail(self, template_id, sender, recepient_email, data_dict)

return Response({"status_code": status.HTTP_200_OK, "message": "Mail sent successfully."})

4. Creating endpoint for the created Class view for sending an email

from django.conf.urls import url

from .views import MailSenderAPIView

urlpatterns = [

url('sendTemplateEmail', MailSenderAPIView.as_view(), name="mail-sender"),


You can find the whole implementation here.


Tech Expert

Shubham is a Lead Python developer with 3+ years of experience. He is very passionate about his work. He is always eager to learn new programming skills and technologies and looking for new ways to optimize the development process. His areas of expertise are in Building Machine Learning models, Creating REST APIs in Django/Flask, Web Scraping and Writing Automation Scripts for businesses.

The founder's survey report on "What Matters For Startup" is released - Get your copy and learn the trends of successful companies :)

Download Free eBook Now!

Get in touch for a detailed discussion.

Hear From Our 100+ Customers

Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.


We had very close go live timeline and MindBowser team got us live a month before.

Shaz Khan
CEO, BuyNow WorldWide

They were a very responsive team! Extremely easy to communicate and work with!

Kristen M.
Founder & CEO, TotTech

We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.

Chacko Thomas
Co-Founder, TEAM8s

Mindbowser is one of the reasons that our app is successful. These guys have been a great team.

Dave Dubier
Founder & CEO, MangoMirror

"Mindbowser was very helpful with explaining the development process and started quickly on the project."

Hieu Le
Executive Director of Product Development, Innovation Lab

"The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs."

Alex Gobel
Co-Founder, Vesica

Mindbowser is professional, efficient and thorough. 

MacKenzie R
Consultant at XPRIZE

Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.

Laurie Mastrogiani
Founder, S.T.A.R.S of Wellness

MindBowser was great; they listened to us a lot and helped us hone in on the actual idea of the app.” “They had put together fantastic wireframes for us.

Bennet Gillogly
Co-Founder, Flat Earth

"They're very tech-savvy, yet humble."

Uma Nidmarty
CEO, GS Advisorate, Inc.

"Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier"

Katie Taylor
Founder, Child Life On Call

“As a founder of a budding start up, it has been a great experience working with Mindbower Inc under Ayush's leadership for our online digital platform design and development activity."

Radhika Kotwal
Founder of Courtyardly

The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!

Michael Wright
Chief Executive Officer, SDOH2Health LLC

They are focused, patient and; they are innovative. Please give them a shot if you are looking for someone to partner with, you can go along with Mindbowser.

David Cain
CEO, thirty2give