Mailchimp Email Service Integration With NodeJS

Posted By : Vishal Yadav | 28-Apr-2020

Mailchimp is an email marketing service and automation platform that helps you manage and talk to your clients and other interested parties. Mailchimp allows you to create a list of an audience where you can send Emails to all audiences or a particular user with the help of Campaigns. Campaigns allow you to use custom or prebuild Email templates from Mailchimp Dashboard so you don't have to manually create an Email format in your backend just call a campaign from your Node application.

 

Creating a Mailchimp Account and Using It In NodeJS Application

Step 1: Create a trial Mailchimp account. Now from dashboard go to API keys option under the Account tab. Get the API key for your account or create a new one.

Step 2: Now go the audience tab and create a new audience and from settings copy its unique ID later to be used in the configuration.

Step 3: Install Mailchimp dependency. Open your project directory and run the command mentioned below.

npm install mailchimp

 

Step 4: Add Mailchimp credential in .env or configuration and create an object for Mailchimp.

// Add Mailchimp Credentials in .env file.

APIKEY = "Mailchimp_Key_obtained_from_step 1"
UNIQUEID = "Audience_ID_obtained_from_step 2"


// Create Mailchimp object in you JS file
var MailChimp = require('mailchimp').MailChimpAPI;
var apiKey = process.env.APIKEY;
global.MailChimpAPI = new MailChimp(apiKey, {
    version: '2.0'
});

 

Step 5: Create a function to subscribe to the Audience or send mail.

// Function to subscribe to Audience
var subscribeUser = function(userInfo) {
        var userData = {
            "id": process.env.UNIQUEID,    //Audience Unique ID
            "email": {
                "email": userInfo.email,   //Client Email
            },
            "merge_vars": {
                FNAME: userInfo.firstName, //Client Name
                LNAME: userInfo.lastName   //Last Name
            }

        }
        MailChimpAPI.call('lists', 'subscribe', userData, function(error, data) {
            if (error)
                CustomLogger.error("Error in subscribing user", error.message);
            else
                CustomLogger.debug(JSON.stringify(data)); // Do something with your data!
        });
    }

//Function to call Campaign. Complete APIs documentation for all functions can be found under the Mailchimp developer guide.

var subscribeUser = function(userInfo) {
       MailChimpAPI.call('campaigns', 'list', { start: 0, limit: 30 }, function (error, data) {
             if (error)
               CustomLogger.error(error.message);
             else
               CustomLogger.debug(JSON.stringify(data)); // Do something with your data!
        });
}

 

Conclusion

In the above code we have integrated Mailchimp Email services in our Node JS Application. We can subscribe to Mailchimp Email service from the Audience and fetch campaign details form these functions. All other functions are similar to these functions only required to change the parameters.

About Author

Author Image
Vishal Yadav

Vishal has good knowledge of MEAN stack, JavaScript,HTML,CSS. He has worked on both front-end and back-end. He likes to learn new technologies.

Request for Proposal

Name is required

Comment is required

Sending message..