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 is a highly skilled backend developer with extensive 3+ years experience in developing various blockchain platforms. He has a comprehensive understanding of the technologies and has hands-on expertise in Node.js, Ethereum, Layer 1 and Layer 2 solutions, smart contract development, and databases like MySQL and MongoDB. He has a proven track record of working on a range of blockchain-related projects, including token development, staking, governance, indexes, bridges, NFT, marketplace, ICO/IDO, and more. He is adept at managing trading bots, and developing centralized exchanges, and has a creative mind with excellent analytical skills.

Request for Proposal

Name is required

Comment is required

Sending message..