简体   繁体   中英

How can I add a contact to a list/audience in Mailchimp using its own library via Node JS

The code below is the API call to add a contact to a list/audience programmatically using Node JS and the Mailchimp library. Documentation is found on: https://mailchimp.com/developer/marketing/guides/create-your-first-audience/#add-a-contact-to-an-audience

const listId = "YOUR_LIST_ID";
const subscribingUser = {
 firstName: "Prudence",
 lastName: "McVankab",
 email: "prudence.mcvankab@example.com"
};

async function run() {
const response = await mailchimp.lists.addListMember(listId, {
  email_address: subscribingUser.email,
  status: "subscribed",
  merge_fields: {
    FNAME: subscribingUser.firstName,
    LNAME: subscribingUser.lastName
  }
});

console.log(
  `Successfully added contact as an audience member. The contact's id is ${
    response.id
  }.`
);
}
run();

Here's how I implemented the code in my app.js

app.post("/", function(req, res) {
 const firstName = req.body.firstName;
 const lastName = req.body.lastName;
 const email = req.body.email;

 const apiAudienceName = "Sample Tech Newsletter Subscription";

 const listId = apiAudienceName;
 const subscribingUser = {
  firstName: firstName,
  lastName: lastName,
  email: email
 };

 async function run() {
   const response = await mailchimp.lists.addListMember(listId, {
     email_address: subscribingUser.email,
     status: "subscribed",
     merge_fields: {
       FNAME: subscribingUser.firstName,
       LNAME: subscribingUser.lastName
     }
   });

   console.log(`Successfully added contact as an audience member. The contact's id is ${response.id}.`);

 }

 run();
});

To me, I did the exact requirement of the Mailchimp server to add to my created list but this code is throwing an error saying "Unhandled promise rejection" . I've tried to do my research of course but as a beginner in this language, I don't really quite grasp what is needed for me to make this happen. If someone could correct me or show me the error in my code, I would appreciate it. Thank you very much!

NOTE: I was able to make this work by using HTTP request module. But for this time, I wanted to learn how to follow a documentation and use their given code and library. To me, it seems that I did that but it doesn't seem to work.

Through research and help from the course community that I am in, I found the problem in my code. I hope this helps someone who's dealing/will potentially deal with the same problem.

const apiAudienceName = "Sample Tech Newsletter Subscription";

const listId = apiAudienceName;

As you can see in the above code, I am inputting the "list/audience name" that I have created in the Mailchimp website into the listID variable. The problem is that the Mailchimp API is expecting the listID and not the name of the list that you have in the Mailchimp API.

The answer that solved this problem was getting the list/audience ID from your account in the Mailchimp website and using it as an input. The list/audience ID is an alphanumeric characters provided by the Mailchimp server.

If you want a detailed guide on finding your list/audience ID , check this link .

Here's a sample code that solved this problem:

app.post("/", function(req, res) {
 const firstName = req.body.firstName;
 const lastName = req.body.lastName;
 const email = req.body.email;

 const listId = "1a2b3c4d5e777";
 const subscribingUser = {
  firstName: firstName,
  lastName: lastName,
  email: email
 };

 async function run() {
  const response = await mailchimp.lists.addListMember(listId, {
   email_address: subscribingUser.email,
   status: "subscribed",
   merge_fields: {
     FNAME: subscribingUser.firstName,
     LNAME: subscribingUser.lastName
   }
 });

 console.log(`Successfully added contact as an audience member. The contact's id is ${response.id}.`);

 }

 run();
});

Have you tried subsribi yet? it has a simple API you can use to add subscribers to your list using JavaScript,

  • Example:

    const data = { email:"sarah@mail.com", //* required username:"sarah", //optional listId:'[LIST_ID]',

     } const url = 'https://api.subscribi.io/v1/subscribers/add' fetch(url,{ method: 'POST', headers: { 'Content-Type': 'application/json', 'secret_key':'MY_SECRET_KEY' // Go the the settings and generate a key }, body: JSON.stringify(data) }).then(response =>{ console.log(response) /* { "data": { "name": "sarah", "email": "sarah@mail.com", "createdAt": "2020-09-25T19:41:07.350Z", "subscribed": true }, "ok": true, "status": "success" } */ }).catch(error=>{ console.error(error) })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM