Mailchimp is a widely-used email marketing platform that offers a robust API for developers looking to integrate its features into their applications. The Mailchimp API allows you to automate email campaigns, manage subscribers, and analyze performance metrics seamlessly. In this article, we will explore three practical examples of using the Mailchimp API for email marketing.
Managing subscriber lists can be time-consuming, especially for businesses with a high volume of sign-ups. Automating the subscriber management process allows you to efficiently add or update subscribers based on their interactions with your website.
Here’s how to automate this process using the Mailchimp API:
import requests
# Mailchimp API credentials
api_key = 'YOUR_API_KEY'
list_id = 'YOUR_LIST_ID'
# Subscriber data
subscriber_email = 'newuser@example.com'
subscriber_data = {
'email_address': subscriber_email,
'status': 'subscribed', # or 'unsubscribed'
}
# API endpoint for adding a subscriber
url = f'https://<dc>.api.mailchimp.com/3.0/lists/{list_id}/members/'
# Make the request to add/update the subscriber
response = requests.post(
url,
auth=('anystring', api_key),
json=subscriber_data
)
# Check the response
if response.status_code == 200:
print('Subscriber added successfully.')
else:
print('Error adding subscriber:', response.json())
<dc>
with your Mailchimp data center prefix (e.g., us1).Targeted email campaigns increase engagement by delivering relevant content to specific segments of your audience. With the Mailchimp API, you can create and send campaigns based on user behavior or demographics.
Here’s how to create and send a targeted email campaign:
# Campaign details
campaign_data = {
'recipients': {'list_id': list_id},
'type': 'regular',
'settings': {
'subject_line': 'Special Promotion Just for You!',
'from_name': 'Your Company',
'reply_to': 'info@yourcompany.com',
}
}
# Create the campaign
campaign_url = 'https://<dc>.api.mailchimp.com/3.0/campaigns'
create_response = requests.post(
campaign_url,
auth=('anystring', api_key),
json=campaign_data
)
# Extract campaign ID
campaign_id = create_response.json()['id']
# Set campaign content
content_data = {
'html': '<h1>Exclusive Offer!</h1><p>Enjoy 20% off your next purchase.</p>'
}
# Add content to the campaign
content_url = f'https://<dc>.api.mailchimp.com/3.0/campaigns/{campaign_id}/content'
requests.put(content_url, auth=('anystring', api_key), json=content_data)
# Send the campaign
send_url = f'https://<dc>.api.mailchimp.com/3.0/campaigns/{campaign_id}/actions/send'
requests.post(send_url, auth=('anystring', api_key))
print('Campaign sent successfully.')
Understanding how your email campaigns perform is crucial for refining your marketing strategy. The Mailchimp API allows you to retrieve detailed analytics for your campaigns, including open rates, click rates, and more.
Here’s how to fetch campaign performance metrics:
# Retrieve campaign reports
report_url = f'https://<dc>.api.mailchimp.com/3.0/campaigns/{campaign_id}/report'
report_response = requests.get(report_url, auth=('anystring', api_key))
report_data = report_response.json()
# Display key performance metrics
open_rate = report_data['opens']['summary']['open_rate']
click_rate = report_data['clicks']['summary']['click_rate']
print(f'Open Rate: {open_rate * 100:.2f}%')
print(f'Click Rate: {click_rate * 100:.2f}%')