How can I add contacts to a segment programmatically?

One of the most requested uses cases is "how can I create contacts in my MoonMail account programmatically?" This is very easy to do with MoonMail GraphQL API.

The process can be split into three steps as the following Node.JS shows:

1. Setting up the configuration to access the MoonMail GraphQL API

You'll need an API key for this. All MoonMail users, but those in the Lite plan, can create an API in their account settings.

const axios = require('axios').default;

(async () => {
  // Step 1: Setup the configuration
  const API_ENDPOINT = 'https://graphql.moonmail.io';

  const config = {
    headers: {
      'x-api-key': '<YOUR_API_KEY>'
    }
  };

2. Creating a dynamic segment where your contacts will be held

In this step, you need to create an empty dynamic segment for your contacts. You need to provide at least one or more custom attributes to use as criteria to define the segment.

// Step 2: Create a dynamic segment to hold your contacts
  const createSegmentMutation = {
    query: `mutation CreateSegment($input: CreateSegmentInput!) {
      createSegment(input: $input) {
          Id
          Name
      }
    }`,
    variables: {
      input: {
        Name: 'My Segment',
        Dimensions: {
          Attributes: [
            {
              Key: 'Dynamic Segment #1', 
              AttributeType: 'INCLUSIVE', 
              Values: 'Dynamic Segment #1'
	    }
          ]
        }
      }
    }
  };
  const createSegmentResponse = await axios
    .post(API_ENDPOINT, createSegmentMutation, config)
    .catch(error => console.log(error.toJSON()));

  const createSegment = createSegmentResponse.data.data.createSegment;
  console.log(createSegment);

3. Adding your contacts to the previously created segment

Once the segment has been created we can create contacts in our account and include them in the newly created segment. These contacts have to be created with the attributes we used to define the segment in the previous step. You can also add in this step the contact attributes that you find useful as user attributes.

  // Step 3: Add your contacts to the created segment
  // How do you funnel those contacts to the mutation is up to you, 
  // for instance, a CSV file or a DB
  const Contacts = [
    {
      Address: 'contact+1@contactdomain.com',
      // Use the same key and value you used to create the segment
      Attributes: [{Key: 'Dynamic Segment #1', Value: 'Dynamic Segment #1'}]
    },
    {
      Address: 'contact+2@contactdomain.com',
      Attributes: [{Key: 'Dynamic Segment #1', Value: 'Dynamic Segment #1'}],
      // You can also add the contacts useful attributes
      UserAttributes: [
        {Key: 'Firstname', Value: 'John'},
        {Key: 'Lastname', Value: 'Doe'}
      ]
    },
    {
      Address: 'contact+3@contactdomain.com',
      Attributes: [{Key: 'Dynamic Segment #1', Value: 'Dynamic Segment #1'}],
      UserAttributes: [{Key: 'Country', Value: 'US'}]
    }
  ];

  const putContactsMutation = {
    query: `mutation PutContacts($input: PutContactsInput!) {
      putContacts(input: $input) {
        Address
        Attributes {
          Key
          Value
        }
        CreationDate
        Id
        Metrics {
          Key
          Value
        }
        UserAttributes {
          Key
          Value
        }
      }
    }`,
    variables: {
      input: {
        Contacts
      }
    }
  };
  const putContactsResponse = await axios
    .post(API_ENDPOINT, putContactsMutation, config)
    .catch(error => console.log(error.toJSON()));

  const putContacts = putContactsResponse.data.data.putContacts;

  // Checking all the created contacts and its attributes
  putContacts.forEach(c => console.log(JSON.stringify(c)));
})(); // <-- END OF FILE

After running this script successfully you'll have a dynamic segment named My Segment with three contacts assigned to it. Now you can use your new segment to send a campaign to those contacts.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us