Add subscriber to Mailerlite

This hook automatically adds customer email addresses to MailerLite using the MailerLite API. It creates or updates subscriber records when triggered by a customer-related event in your Shopify store.

Prerequisites

  • MailerLite API key
  • MailerLite subscriber group IDs (optional)
  • A Shopify store with Cloudhooks installed

Implementation

The hook posts a CreateOrUpsert object to the MailerLite 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);
  }
};

Configuration

  1. Set up trigger: Select an order-related trigger (like "An order is created" or "An order is paid")
  2. Add your API key: Replace 'Your Mailerlite API key' with your actual key.
  3. Configure groups (optional): Add your Mailerlite group IDs to the subscriberGroups array.

Testing

Test your hook using Cloudhooks' test payload feature before activating it in production. Verify that subscriber data appears in your MailerLite dashboard.

Additional Resources

For more Cloudhooks examples, visit our Gist page on Github.