Create Newsletter app using MailChimp and NodeJS
Nowadays, every business uses email marketing to promote their business and send regular updates to their users via email. Maybe, you are also subscribers of some websites such as GeeksforGeeks and many more. It’s simple if you subscribe, you will get regular email from their side, and if you unsubscribe, you will stop receiving emails.
Have you thought about how they can send emails to subscribed users with a single click? MailChimp is the best tool for email marketing. Also, it allows you to create different lists to manage your audiences and send bulk emails. However, only sending emails is not adequate. It should have some attractive templates and text that MailChimp provides, and you pick default templates for your email directly.
In this tutorial, we will create a simple newsletter app using MailChimp and NodeJS. We will learn to build an audience list and store the users to MailChimp via NodeJS When users subscribe to our app.
Pre-requisite:
Create MailChimp Free Account:
Now, users need to follow the below steps to create MailChimp free account and get an API key.
Step 1: Go to MailChimp.com and signup for free. Also, set up the profile of your account.
Step 2: Now, to get the API key, From the bottom left of your screen, Go to the Profile >> Extras >> API Key >> Create API Key. Furthermore, it would help if you store your API key. We will use it later in our NodeJS app.

Step 3: Next, we need to create an audience list. Go to https://us14.admin.mailchimp.com/lists/, and click on the Create Audience. Fill in the required details and see your audience on the audience dashboard.

Step 4: Now, we need to get the audience list id. Go to the audience settings -> list id. Store the list id to use it in the NodeJs app.

Now, we are ready to create the NodeJS app.
Create NodeJS APP:
To create the new NodeJS App, go to your project directory from the terminal and enter the below command to it.
npm init -y
Next, enter the below command in the same directory to install the required npm packages.
npm i body-parser express https nodemon
Project Structure: Your project directory should look like the below image.
Example: This example will demonstrate the creation Newsletter app using MailChimp and NodeJS
In this file, we will add the basic code of the express app to send the data to your MailChimp via API key.
// import required packages
const express= require("express");
const https= require("https");
const bodyparser= require("body-parser");
const app= express();
app.use(express.static("public"));
app.use(bodyparser.urlencoded({extended:true}));
// On the home route, send signup html template
app.get("/",function(req,res){
res.sendFile(__dirname+"/signup.html");
});
// Manage post request on home route and
// Send data to the MailChimp account via API
app.post("/",function(req,res){
var firstName=req.body.Fname;
var email=req.body.Email;
var password=req.body.password;
var data={
members:[{
email_address: email,
status: "subscribed",
merge_fields:{
FNAME: firstName,
LNAME: password
}
}]
}
// Converting string data to JSON data
const jsonData= JSON.stringify(data);
const url="https://us14.api.mailchimp.com/3.0/lists/f4f5ad20f7";
const options={
method:"POST",
auth:"201951173:acfa4fffd113041454c6d953a71fa3e5-us14"
}
// On success send users to success, otherwise on failure template
const request=https.request(url,options,function(response){
if(response.statusCode===200){
res.sendFile(__dirname+"/success.html");
}else{
res.sendFile(__dirname+"/failure.html");
}
response.on("data",function(data){
console.log(JSON.parse(data));
});
});
request.write(jsonData);
request.end();
});
// Failure route
app.post("/failure",function(req,res){
res.redirect("/");
})
app.listen(8000,function(){
console.log("server is running on port 8000.");
})