Mail-chimp API Using Node.js SDK - Node.js API Tutorial
Mailchimp is the All-In-One integrated marketing platform for small businesses, to grow their business on their terms. It's easy to use - start for free today! If you want to learn more you can check out my youtube channel.
package.json
{ "name": "mc-api", "version": "1.0.0", "description": "", "main": "server.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "@mailchimp/mailchimp_marketing": "^3.0.27", "express": "^4.17.1" } }
server.js
const mailchimp = require("@mailchimp/mailchimp_marketing"); const express = require('express'); const app = express(); app.use(express.json()); app.use(express.urlencoded({extended: false})); mailchimp.setConfig({ apiKey: "YOUR_API_KEY", server: "YOUR_DATA_CENTER", }); async function getInformations() { // MAKE FIRST API CALL // https://mailchimp.com/developer/marketing/guides/quick-start/#make-your-first-api-call // const response = await mailchimp.ping.get(); // console.log(response); // GET INFORMATIONS ABOUT ALL AUDIANCE // https://mailchimp.com/developer/marketing/api/lists/get-lists-info/ // const response = await mailchimp.lists.getAllLists(); // console.log(response); // GET INFORMATIONS ABOUT SPECEFIC AUDIANCE // https://mailchimp.com/developer/marketing/api/lists/get-list-info/ // const response = await mailchimp.lists.getList("ID_OF_YOUR_AUDIANCE");
// console.log(response); // GET INFORMATIONS ABOUT MEMBERS // https://mailchimp.com/developer/marketing/api/list-members/list-members-info/ const response = await mailchimp.lists.getListMembersInfo("ID_OF_YOUR_AUDIANCE");
console.log(response); } // getInformations(); // ADD MEMBER TO THE AUDIANCE OR LIST // https://mailchimp.com/developer/marketing/api/list-members/ const addMembers = async () => { const response = await mailchimp.lists.addListMember("ID_OF_YOUR_AUDIANCE", {
email_address: "messi@yahoo.com", status: "subscribed", }); console.log(response); }; // addMembers(); app.get('/audiance', async(req, res, next)=>{ // GET INFORMATIONS ABOUT MEMBERS // https://mailchimp.com/developer/marketing/api/list-members/list-members-info/ const response = await mailchimp.lists.getListMembersInfo("ID_OF_YOUR_AUDIANCE");
console.log(response); res.status(200).json(response); }); app.post('/audiance', async(req, res, next)=>{ const {email, status} = req.body; // const response = await mailchimp.lists.addListMember("ID_OF_YOUR_AUDIANCE", {
// email_address: email, // status: status, // }); // res.status(201).json(response); const response = await mailchimp.lists.addListMember("ID_OF_YOUR_AUDIANCE", {
email_address: email, status: status, }); res.status(200).json(response); }); app.listen(4000, ()=> console.log("server is running on 4000"));
No comments: