0

I'm trying to set a suscriber's Language with MailChimp nodejs SDK while adding him/her to a list.

The nodejs SDK calls the API https://us2.api.mailchimp.com/3.0/lists/<list-ID>/members, and it successfully creates a new member. However, it doesn't set the user's language. I read online that I have to pass the Accept-Language header with my HTTP request, so it is what I did.

In order to be able to add a custom header using the SDK, I slightly edited the SDK to add a defaultHeaders option. With this modification, the header is correctly set, but unfortunately it doesn't change anything: the new member still doesn't have the language set.

Here is my code:

import mailchimp from "@mailchimp/mailchimp_marketing";

export const handler = async(event) => {
    
    const params = JSON.parse(event.body);

    mailchimp.setConfig({
        apiKey: process.env.apiKey,
        server: process.env.server,
        defaultHeaders: {
            'Accept-Language': event.headers['accept-language'],
        },
    });
    
    const email = params.email;
    const firstname = params.name.substring(0, params.name.indexOf(' ')) || "";
    const lastname = params.name.substring(params.name.indexOf(' ') + 1) || "";
    
    return await mailchimp.lists.addListMember(process.env.listId,
        {
            email_address: email,
            status: 'subscribed',
            email_type: 'html',
            merge_fields: {
                FNAME: firstname,
                LNAME: lastname,
            },
        }
    );
};

The generated request is the following:

Request {
  method: 'POST',
  url: 'https://us2.api.mailchimp.com/3.0/lists/<list-ID>/members',
  header: {
    'User-Agent': 'node-superagent/3.8.1',
    Authorization: 'Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX=',
    'Accept-Language': 'fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7',
    'Content-Type': 'application/json',
    Accept: 'application/json'
  },
  writable: true,
  cookies: '',
  qs: {},
  qsRaw: [],
  data: {
    email_address: '<the-tested-email-address>',
    status: 'subscribed',
    email_type: 'html',
    merge_fields: { FNAME: 'Firstname', LNAME: 'Lastname' }
  },
}

Still, the created member doesn't have the language set.

Please help me, it would really be appreciated How can I set a new member's language when creating it using MailChimp API?

0 Answers0