Search code examples
javascriptnode.jsmailchimp-api-v3.0

How to submit a form (add a subscriber) by mailchimp-api-v3 in Node.js?


I am using mailchimp-api-v3 to submit a form.

This list only has three fields, which are FNAME, EMAIL, and COMPANY.

const Mailchimp = require('mailchimp-api-v3');

const mailchimp = new Mailchimp(myMailchimpAPI);

mailchimp.post(`/lists/${myListId}/members`, {
  FNAME: 'Jack',
  EMAIL: '[email protected]',
  COMPANY: 'Apple'
})
.then(res => console.log(res))
.catch(err => console.log(err));

This gives me error:

[ { field: 'email_address', message: 'This value should not be blank.' } ] }


Solution

  • I figured out after reading the Mailchimp API document. I have to add another two fields email_address and status which are required. Then move the rest of form fields under merge_fields.

    The correct code is

    const Mailchimp = require('mailchimp-api-v3');
    
    const mailchimp = new Mailchimp(myMailchimpAPI);
    
    mailchimp.post(`/lists/${myListId}/members`, {
      email_address: '[email protected]',
      status: 'subscribed',
      merge_fields: {
        FNAME: 'Jack',
        EMAIL: '[email protected]',
        COMPANY: 'Apple'
      }
    })
    .then(res => console.log(res))
    .catch(err => console.log(err));
    

    It was hard for me to find a simple demo online. Hope this helps for future people.