Oct 21, 2024 09:18 AM
Here're a few 'Run a Script' Action automations for integrating with MailChimp (create contact, tag contact, archive contact). In all examples, be sure to add your own values for mailchimpAPIKey and audianceId constants and add your own input variables e.g. email, firstName, lastName, organisation, tag etc.
Note: these automations send the actual email address across to MailChimp instead of using an MD5 hash of it, as is recommended by MailChimp. Whilst these scripts are not necessarily insecure, the transport is still TLS encrypted (HTTPS), sending an MD5 hash is still a more secure method.
CREATE A MAILCHIMP CONTACT
// API Key and Audience ID
const mailchimpAPIKey = "ENTER YOUR MAILCHIP API KEY";
const audienceId = "ENTER YOUR AUDIANCE ID";
// Extract email and other fields from the record that triggered the automation
let config = input.config(); // Capture all input variables
let email = config.email; // Email address from Airtable
let firstName = config.firstName; // First Name from Airtable
let lastName = config.lastName; // Last Name from Airtable
let organisation = config.organisation; // Organisation from Airtable
// MailChimp API endpoint to add a contact
let mailchimpServer = mailchimpAPIKey.split('-')[1]; // Get the server prefix from the API key
let url = `https://${mailchimpServer}.api.mailchimp.com/3.0/lists/${audienceId}/members`;
// Prepare the headers for the API call
let headers = {
"Authorization": `apikey ${mailchimpAPIKey}`,
"Content-Type": "application/json"
};
// Define the contact data to send to MailChimp
let contactData = {
email_address: email,
status: "subscribed", // Options: "subscribed", "unsubscribed", "cleaned", "pending"
merge_fields: {
FNAME: firstName,
LNAME: lastName,
ORG: organisation
}
};
// Make the API request
let response = await fetch(url, {
method: "POST",
headers: headers,
body: JSON.stringify(contactData)
});
// Handle the response
if (response.ok) {
output.set('result', 'Contact created successfully!');
} else {
let error = await response.json();
output.set('result', `Error: ${error.detail}`);
}
APPLY A TAG TO MAILCHIMP CONTACT
// API Key and Audience ID
const mailchimpAPIKey = "ENTER YOUR MAILCHIP API KEY";
const audienceId = "ENTER YOUR AUDIANCE ID";
// Extract email and other fields from the record that triggered the automation
let config = input.config(); // Capture all input variables
let email = config.email; // Email address from Airtable
let tag = config.tag; // Tag from Airtable
// MailChimp API endpoint for tagging a contact using email instead of MD5-hashed-email
let mailchimpServer = mailchimpAPIKey.split('-')[1]; // Get the server prefix from the API key
let url = `https://${mailchimpServer}.api.mailchimp.com/3.0/lists/${audienceId}/members/${email}/tags`;
// Prepare headers for the API call
let headers = {
"Authorization": `apikey ${mailchimpAPIKey}`,
"Content-Type": "application/json"
};
// Define the tag data
let tagData = {
"tags": [
{
"name": tag,
"status": "active" // Use "inactive" to remove the tag
}
]
};
// Make the API request
let response = await fetch(url, {
method: "POST",
headers: headers,
body: JSON.stringify(tagData)
});
// Handle the response
if (response.ok) {
output.set('result', 'Contact tagged successfully!');
} else {
let error = await response.json();
output.set('result', `Error: ${error.detail}`);
}
APPLY MULTIPLE TAGS TO A MAILCHIMP CONTACT
// API Key and Audience ID
const mailchimpAPIKey = "ENTER YOUR MAILCHIP API KEY";
const audienceId = "ENTER YOUR AUDIANCE ID";
// Extract email and other fields from the record that triggered the automation
let config = input.config(); // Capture all input variables
let email = config.email; // Email address from Airtable
let tags = config.tags; // Tags from Airtable
// MailChimp API endpoint for applying multiple tags to a contact using email instead of MD5-hashed-email
let mailchimpServer = mailchimpAPIKey.split('-')[1]; // Get the server prefix from the API key
let url = `https://${mailchimpServer}.api.mailchimp.com/3.0/lists/${audienceId}/members/${email}/tags`;
// Prepare headers for the API call
let headers = {
"Authorization": `apikey ${mailchimpAPIKey}`,
"Content-Type": "application/json"
};
// Define the tag data for multiple tags
let tagData = {
"tags": tags.map(tag => ({
"name": tag,
"status": "active" // Use "active" to add the tag or "inactive" to remove it
}))
};
// Make the API request to assign the tags
let response = await fetch(url, {
method: "POST",
headers: headers,
body: JSON.stringify(tagData)
});
// Handle the response
if (response.ok) {
output.set('result', 'Contact tagged successfully!');
} else {
let error = await response.json();
output.set('result', `Error: ${error.detail}`);
}
ARCHIVE A MAILCHIMP CONTACT
// API Key and Audience ID
const mailchimpAPIKey = "ENTER YOUR MAILCHIP API KEY";
const audienceId = "ENTER YOUR AUDIANCE ID";
// Extract the email from the record that triggered the automation
let config = input.config(); // Capture all input variables
let email = config.email; // Email address from Airtable
// MailChimp API endpoint for archiving a contact using email instead of MD5-hashed-email
let mailchimpServer = mailchimpAPIKey.split('-')[1]; // Get the server prefix from API key
let url = `https://${mailchimpServer}.api.mailchimp.com/3.0/lists/${audienceId}/members/${email}`;
// Prepare headers for the API request
let headers = {
"Authorization": `apikey ${mailchimpAPIKey}`,
"Content-Type": "application/json"
};
// Make the API request
let response = await fetch(url, {
method: "DELETE",
headers: headers,
});
// Handle the response
if (response.ok) {
output.set('result', 'Contact archived successfully!');
} else {
let error = await response.json();
output.set('result', `Error: ${error.detail}`);
}
I hope this helps.
David.