Integration of Mailchimp with Django Rest Framework

In This Modern Era, Email marketing is an important tool to build relationships with customers because it gives freedom to directly communicate with customers.

For successful email marketing, we need to implement some email marketing tools on our website. In this article, I am going to highlight one of an email marketing tool named ‘Mailchimp.’

Mailchimp is a prominent email marketing tool that allows its customers to add email to their subscriber list from websites to use these emails for email campaigns. Mailchimp API provides you to send email to an audience who subscribed to your website. The audiences are also known as Lists.


Preface:
This blog is going to cover how to set up a Mailchimp account and implementation on the server-side. We will cover the steps in the following order:

  • Setting up a Mailchimp account and creating the API key
  • Create a new audience and generating audience id
  • Installing Mailchimp library for Django
  • Setting up Mailchimp in Django settings.py file
  • Create API to add member email to audience


Setting up Mailchimp account:

Here you need to make a Mailchimp Account. It is free of cost. After that, you will navigate to the Dashboard of Mailchimp.

  • Go to the Profile Icon, then click on Account.

  • The page will navigate to the accounts page. Click Extras from the options:
  • From the Dropdown, Click on API keys will navigate to a page to generate your API keys.

  • Copy  the  API keys:


Create a new audience and generating audience id:

  •  Click on the audience icon

  •  From the audience, Section select All Contacts

  • Click on settings
  • From the dropdown, select the Audience name and defaults

  • Copy the Audience ID

  • At Last, you are able to collect Api keys, audience ID, Server details.

Installing Mailchimp library for Django:

  •  First, install the Mailchimp-marketing package:
    pip install Mailchimp-marketing

Adding required values in settings.py file for Mailchimp:

  • Go to your project’s settings, and add the following code:
    Project/settings.py:# Mailchimp credentials
    Mailchimp_API_KEY = "Your API keys here"
    Mailchimp_DATACENTER = 'us7'
    Mailchimp_LIST_ID = "Your list id 
  • The Datacenter is the last three-digit in your API key after the -. e.g:’us7’

Create a view to add email to Mailchimp:

from rest_framework import status
from Mailchimp_marketing import Client
from rest_framework.response import Response
from rest_framework.generics import GenericAPIView
from Mailchimp_marketing.api_client import ApiClientError
from Mailchimp_integration.settings import (

                                            Mailchimp_API_KEY,

                                            Mailchimp_DATA_CENTER,

                                            Mailchimp_LIST_ID,

                                            )

class MailSubscriptionAPIView(GenericAPIView):

    def subscribe_email(email):

        “””

        This function will communicate with Mailchimp api

        to create a member in an audience list

        “””

        Mailchimp = Client()

        Mailchimp.set_config({

            “api_key”: Mailchimp_API_KEY,

            “server”: Mailchimp_DATA_CENTER

        })

        member_info = {

            “email_address”: email,

            “status”“subscribed”,

        }

        try:

            Mailchimp.lists.add_list_member(Mailchimp_LIST_ID, member_info)

        except ApiClientError as error:

            print(error.text)

    def post(self, request, *args, **kwargs):

        email = request.data[’email’]

        MailSubscriptionAPIView.subscribe_email(email)

        return Response({

                “status_code”: status.HTTP_200_OK,

                “message”“Mail added to Mailchimp”

            })

 

Create an endpoint for the Class-based view :

from django.urls import path
from .views import MailSubscriptionAPIView
urlpatterns = [

    path(‘subscribe_email’, MailSubscriptionAPIView.as_view(), name‘subscribe-email’ ),

]

 

Conclusion:

In this way, you can add more emails to your subscriber list and can start an email campaign to boost your Business. Also, you can explore the Mailchimp dashboard to learn more about Mailchimp.

GitHub: https://github.com/nihar9938/Mailchimp-integration-with-drf

Nihar

Tech Expert

Nihar is a full stack developer with around 1 year of experience. He is an expert in building python Integrated web applications, creating REST API’s with well designed, testable and efficient code. He is always eager to learn new programming languages and technologies and looking for new ways to optimize the development process.

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
coma

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

author
ADDIE WOOTTEN
CEO, SMILINGMIND
coma

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

author
Shaz Khan
CEO, BuyNow WorldWide
coma

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

author
Kristen M.
Founder & CEO, TotTech
coma

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

author
Chacko Thomas
Co-Founder, TEAM8s
coma

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

author
Dave Dubier
Founder & CEO, MangoMirror
coma

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

author
Hieu Le
Executive Director of Product Development, Innovation Lab
coma

"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."

author
Alex Gobel
Co-Founder, Vesica
coma

Mindbowser is professional, efficient and thorough. 

author
MacKenzie R
Consultant at XPRIZE
coma

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

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

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.

author
Bennet Gillogly
Co-Founder, Flat Earth
coma

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

author
Uma Nidmarty
CEO, GS Advisorate, Inc.
coma

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

author
Katie Taylor
Founder, Child Life On Call
coma

“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."

author
Radhika Kotwal
Founder of Courtyardly
coma

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

author
Michael Wright
Chief Executive Officer, SDOH2Health LLC
coma

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.

author
David Cain
CEO, thirty2give