Add subscriber to Mailerlite

Adding the customer's email address to MailerLite can be done via the MailerLite API.

The hook just needs to post a CreateOrUpsert object to the subscriber resource:

const apiKey = 'Your Mailerlite API key';
const subscriberGroups = ['Subscriber group id1', 'Subscriber group id2'];

module.exports = async function(payload, actions) {
  const httpConfig = {
    headers: {
      'Authorization': `Bearer ${apiKey}`
    }
  };
  
  const subscriberParams = {
    email: payload.customer.email,
    fields: {
        name: `${payload.customer.first_name} ${payload.customer.last_name}`
    },
    groups: subscriberGroups,
    status: 'active'
  };

  try {
    const {data: result} = await actions.http.post(
      'https://connect.mailerlite.com/api/subscribers',
      subscriberParams,
      httpConfig
    );
    
    console.log('Subscription result: ', result);
  } catch (err) {
    console.log('Subscription error: ' + err);
  }
};

For more examples, you can visit our Gist page on Github.