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

We Keep Sharing Tips And Learnings On Growth And Technology. Join Our Popular Newsletter That Goes Out Once In 2 Weeks

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.

Get in touch for a detailed discussion.

What’s on your mind? Tell us a little bit about yourself and your question, and we will be in touch with you within 12 hours

Free eBook on Telemedicine Platform Development: All About Telemedicine

Download Free eBook Now!