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.

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 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 file for Mailchimp:

  • Go to your project’s settings, and add the following code:
    Project/ 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 (





class MailSubscriptionAPIView(GenericAPIView):

    def subscribe_email(email):


        This function will communicate with Mailchimp api

        to create a member in an audience list


        Mailchimp = Client()


            “api_key”: Mailchimp_API_KEY,

            “server”: Mailchimp_DATA_CENTER


        member_info = {

            “email_address”: email,




            Mailchimp.lists.add_list_member(Mailchimp_LIST_ID, member_info)

        except ApiClientError as error:


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

        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’ ),




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.


Keep Reading

Keep Reading

Mindbowser is excited to meet healthcare industry leaders and experts from across the globe. Join us from Feb 25th to 28th, 2024, at ViVE 2024 Los Angeles.

Learn More

Let's create something together!