Mailchimp
Mailchimp
通过托管的OAuth认证访问Mailchimp营销API。管理电子邮件营销的受众、营销活动、模板、自动化流程、报告和订阅者。
快速开始
# 列出所有受众列表
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://gateway.maton.ai/mailchimp/3.0/lists')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
基础URL
https://gateway.maton.ai/mailchimp/{原生API路径}
替换{native-api-path}为实际的Mailchimp API端点路径(例如,3.0/lists)。网关会将请求代理到您的Mailchimp数据中心,并自动注入您的OAuth令牌。
认证
所有请求都要求在Authorization头中包含Maton API密钥:
Authorization: Bearer $MATON_API_KEY
环境变量:将您的API密钥设置为MATON_API_KEY:
export MATON_API_KEY="YOUR_API_KEY"
获取您的API密钥
- 登录或创建一个账户,访问maton.ai
- 前往maton.ai/settings
- 复制您的API密钥
连接管理
管理您的Mailchimp OAuth连接,请访问https://ctrl.maton.ai。
列出连接
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://ctrl.maton.ai/connections?app=mailchimp&status=ACTIVE')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
创建连接
python <<'EOF'
import urllib.request, os, json
data = json.dumps({'app': 'mailchimp'}).encode()
req = urllib.request.Request('https://ctrl.maton.ai/connections', data=data, method='POST')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
req.add_header('Content-Type', 'application/json')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
获取连接
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://ctrl.maton.ai/connections/{connection_id}')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
响应示例:
{
"connection": {
"connection_id": "21fd90f9-5935-43cd-b6c8-bde9d915ca80",
"status": "ACTIVE",
"creation_time": "2025-12-08T07:20:53.488460Z",
"last_updated_time": "2026-01-31T20:03:32.593153Z",
"url": "https://connect.maton.ai/?session_token=...",
"app": "mailchimp",
"metadata": {}
}
}
打开返回的网址在浏览器中完成OAuth授权。
删除连接
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://ctrl.maton.ai/connections/{connection_id}', method='DELETE')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
指定连接
如果您有多个Mailchimp连接,请使用Maton-Connection请求头指定要使用哪一个:
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://gateway.maton.ai/mailchimp/3.0/lists')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
req.add_header('Maton-Connection', '21fd90f9-5935-43cd-b6c8-bde9d915ca80')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
如果省略,网关将使用默认(最早创建的)活动连接。
API参考
列表(受众)
在Mailchimp应用中,“受众”是常用术语,但API在端点中使用“列表”。
获取所有列表
GET /mailchimp/3.0/lists
查询参数:
count- 返回的记录数量(默认10条,最多1000条)offset- 跳过的记录数量(用于分页)fields- 要包含的字段列表,用逗号分隔exclude_fields- 要排除的字段列表,用逗号分隔
示例:
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://gateway.maton.ai/mailchimp/3.0/lists?count=10')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
响应:
{
"lists": [
{
"id": "abc123def4",
"name": "Newsletter Subscribers",
"contact": {
"company": "Acme Corp",
"address1": "123 Main St"
},
"stats": {
"member_count": 5000,
"unsubscribe_count": 100,
"open_rate": 0.25
}
}
],
"total_items": 1
}
获取一个列表
GET /mailchimp/3.0/lists/{list_id}
示例:
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://gateway.maton.ai/mailchimp/3.0/lists/abc123def4')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
创建一个列表
POST /mailchimp/3.0/lists
Content-Type: application/json
{
"name": "Newsletter",
"contact": {
"company": "Acme Corp",
"address1": "123 Main St",
"city": "New York",
"state": "NY",
"zip": "10001",
"country": "US"
},
"permission_reminder": "You signed up for our newsletter",
"campaign_defaults": {
"from_name": "Acme Corp",
"from_email": "newsletter@acme.com",
"subject": "",
"language": "en"
},
"email_type_option": true
}
更新一个列表
PATCH /mailchimp/3.0/lists/{list_id}
删除列表
DELETE /mailchimp/3.0/lists/{list_id}
列表成员(订阅者)
成员是指受众中的联系人。API 使用小写电子邮件地址的 MD5 哈希值作为订阅者标识符。
获取列表成员
GET /mailchimp/3.0/lists/{list_id}/members
查询参数:
status- 按订阅状态筛选(已订阅、已取消订阅、已清理、待处理、交易型)count- 要返回的记录数offset- 要跳过的记录数
示例:
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://gateway.maton.ai/mailchimp/3.0/lists/abc123def4/members?status=subscribed&count=50')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
响应:
{
"members": [
{
"id": "f4b7c8d9e0",
"email_address": "john@example.com",
"status": "subscribed",
"merge_fields": {
"FNAME": "John",
"LNAME": "Doe"
},
"tags": [
{"id": 1, "name": "VIP"}
]
}
],
"total_items": 500
}
获取成员
GET /mailchimp/3.0/lists/{list_id}/members/{subscriber_hash}
该subscriber_hash是小写电子邮件地址的 MD5 哈希值。
示例:
# 对于电子邮件 "john@example.com",subscriber_hash = md5("john@example.com")
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://gateway.maton.ai/mailchimp/3.0/lists/abc123def4/members/b4c9a0d1e2f3g4h5')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
添加成员
POST /mailchimp/3.0/lists/{list_id}/members
Content-Type: application/json
{
"email_address": "newuser@example.com",
"status": "subscribed",
"merge_fields": {
"FNAME": "Jane",
"LNAME": "Smith"
},
"tags": ["Newsletter", "Premium"]
}
示例:
python <<'EOF'
import urllib.request, os, json
data = json.dumps({'email_address': 'newuser@example.com', 'status': 'subscribed', 'merge_fields': {'FNAME': 'Jane', 'LNAME': 'Smith'}}).encode()
req = urllib.request.Request('https://gateway.maton.ai/mailchimp/3.0/lists/abc123def4/members', data=data, method='POST')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
req.add_header('Content-Type', 'application/json')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
更新成员
PATCH /mailchimp/3.0/lists/{list_id}/members/{subscriber_hash}
示例:
python <<'EOF'
import urllib.request, os, json
data = json.dumps({'merge_fields': {'FNAME': 'Jane', 'LNAME': 'Doe'}}).encode()
req = urllib.request.Request('https://gateway.maton.ai/mailchimp/3.0/lists/abc123def4/members/b4c9a0d1e2f3g4h5', data=data, method='PATCH')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
req.add_header('Content-Type', 'application/json')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
添加或更新成员(Upsert)
PUT /mailchimp/3.0/lists/{list_id}/members/{subscriber_hash}
Content-Type: application/json
{
"email_address": "user@example.com",
"status_if_new": "subscribed",
"merge_fields": {
"FNAME": "Jane",
"LNAME": "Smith"
}
}
根据电子邮件哈希创建新成员或更新现有成员。使用status_if_new来设置创建新成员时的状态。
删除成员
归档成员(稍后可重新添加):
DELETE /mailchimp/3.0/lists/{list_id}/members/{subscriber_hash}
返回204 No Content成功时返回。
永久删除(符合 GDPR 要求):
POST /mailchimp/3.0/lists/{list_id}/members/{subscriber_hash}/actions/delete-permanent
成员标签
获取成员标签
GET /mailchimp/3.0/lists/{list_id}/members/{subscriber_hash}/tags
添加或移除标签
POST /mailchimp/3.0/lists/{list_id}/members/{subscriber_hash}/tags
Content-Type: application/json
{
"tags": [
{"name": "VIP", "status": "active"},
{"name": "Old Tag", "status": "inactive"}
]
}
返回204 No Content成功时返回。
细分
获取细分
GET /mailchimp/3.0/lists/{list_id}/segments
示例:
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://gateway.maton.ai/mailchimp/3.0/lists/abc123def4/segments')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
创建细分
POST /mailchimp/3.0/lists/{list_id}/segments
Content-Type: application/json
{
"name": "活跃订阅者",
"options": {
"match": "all",
"conditions": [
{
"condition_type": "EmailActivity",
"field": "opened",
"op": "date_within",
"value": "30"
}
]
}
}
更新细分
PATCH /mailchimp/3.0/lists/{list_id}/segments/{segment_id}
获取细分成员
GET /mailchimp/3.0/lists/{list_id}/segments/{segment_id}/members
删除细分
DELETE /mailchimp/3.0/lists/{list_id}/segments/{segment_id}
返回204 No Content成功时返回。
营销活动
获取所有营销活动
GET /mailchimp/3.0/campaigns
查询参数:
type- 活动类型(常规、纯文本、A/B拆分、RSS、变量)状态- 活动状态(保存、暂停、定时、发送中、已发送)列表ID- 按列表ID筛选数量- 返回的记录数量偏移量- 跳过的记录数量
示例:
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://gateway.maton.ai/mailchimp/3.0/campaigns?status=sent&count=20')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
响应:
{
"campaigns": [
{
"id": "campaign123",
"type": "regular",
"status": "sent",
"settings": {
"subject_line": "月度通讯",
"from_name": "Acme公司"
},
"send_time": "2025-02-01T10:00:00Z",
"report_summary": {
"opens": 1500,
"clicks": 300,
"open_rate": 0.30,
"click_rate": 0.06
}
}
],
"total_items": 50
}
获取活动
GET /mailchimp/3.0/campaigns/{campaign_id}
创建活动
POST /mailchimp/3.0/campaigns
Content-Type: application/json
{
"type": "regular",
"recipients": {
"list_id": "abc123def4"
},
"settings": {
"subject_line": "您的月度更新",
"from_name": "Acme公司",
"reply_to": "hello@acme.com"
}
}
示例:
python <<'EOF'
import urllib.request, os, json
data = json.dumps({'type': 'regular', 'recipients': {'list_id': 'abc123def4'}, 'settings': {'subject_line': '二月通讯', 'from_name': 'Acme公司', 'reply_to': 'newsletter@acme.com'}}).encode()
req = urllib.request.Request('https://gateway.maton.ai/mailchimp/3.0/campaigns', data=data, method='POST')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
req.add_header('Content-Type', 'application/json')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
更新活动
PATCH /mailchimp/3.0/campaigns/{campaign_id}
删除一个营销活动
DELETE /mailchimp/3.0/campaigns/{campaign_id}
返回204 无内容表示成功。
获取营销活动内容
GET /mailchimp/3.0/campaigns/{campaign_id}/content
设置营销活动内容
PUT /mailchimp/3.0/campaigns/{campaign_id}/content
Content-Type: application/json
{
"html": "<html><body><h1>你好!</h1><p>新闻通讯内容在此。</p></body></html>",
"plain_text": "你好!新闻通讯内容在此。"
}
或使用模板:
PUT /mailchimp/3.0/campaigns/{campaign_id}/content
Content-Type: application/json
{
"template": {
"id": 12345,
"sections": {
"body": "<p>模板部分的定制内容</p>"
}
}
}
获取营销活动发送检查清单
检查营销活动是否准备就绪可以发送:
GET /mailchimp/3.0/campaigns/{campaign_id}/send-checklist
发送营销活动
POST /mailchimp/3.0/campaigns/{campaign_id}/actions/send
安排营销活动发送时间
POST /mailchimp/3.0/campaigns/{campaign_id}/actions/schedule
Content-Type: application/json
{
"schedule_time": "2025-03-01T10:00:00+00:00"
}
取消已安排的营销活动
POST /mailchimp/3.0/campaigns/{campaign_id}/actions/cancel-send
模板
获取所有模板
GET /mailchimp/3.0/templates
查询参数:
type- 模板类型(用户、基础、图库)count- 要返回的记录数量offset- 要跳过的记录数量
示例:
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://gateway.maton.ai/mailchimp/3.0/templates?type=user')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
获取单个模板
GET /mailchimp/3.0/templates/{template_id}
获取模板默认内容
GET /mailchimp/3.0/templates/{template_id}/default-content
创建模板
POST /mailchimp/3.0/templates
Content-Type: application/json
{
"name": "Newsletter Template",
"html": "<html><body mc:edit=\"body\"><h1>Title</h1><p>Content here</p></body></html>"
}
更新模板
PATCH /mailchimp/3.0/templates/{template_id}
删除模板
DELETE /mailchimp/3.0/templates/{template_id}
返回204 No Content表示成功。
自动化
Mailchimp的经典自动化功能让您可以构建由日期、活动或事件触发的电子邮件系列。
获取所有自动化
GET /mailchimp/3.0/automations
示例:
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://gateway.maton.ai/mailchimp/3.0/automations')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
获取单个自动化
GET /mailchimp/3.0/automations/{workflow_id}
启动自动化
POST /mailchimp/3.0/automations/{workflow_id}/actions/start-all-emails
暂停自动化
POST /mailchimp/3.0/automations/{workflow_id}/actions/pause-all-emails
获取自动化邮件
GET /mailchimp/3.0/automations/{workflow_id}/emails
将订阅者添加到自动化队列
手动将订阅者添加到自动化工作流:
POST /mailchimp/3.0/automations/{workflow_id}/emails/{workflow_email_id}/queue
Content-Type: application/json
{
"email_address": "subscriber@example.com"
}
报告
获取活动报告
GET /mailchimp/3.0/reports
查询参数:
count- 返回的记录数量offset- 跳过的记录数量type- 活动类型
示例:
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://gateway.maton.ai/mailchimp/3.0/reports?count=20')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
响应:
{
"reports": [
{
"id": "campaign123",
"campaign_title": "月度通讯",
"emails_sent": 5000,
"opens": {
"opens_total": 1500,
"unique_opens": 1200,
"open_rate": 0.24
},
"clicks": {
"clicks_total": 450,
"unique_clicks": 300,
"click_rate": 0.06
},
"unsubscribed": 10,
"bounce_rate": 0.02
}
]
}
获取活动报告
GET /mailchimp/3.0/reports/{campaign_id}
获取活动打开详情
GET /mailchimp/3.0/reports/{campaign_id}/open-details
获取活动点击详情
GET /mailchimp/3.0/reports/{campaign_id}/click-details
获取列表活动
GET /mailchimp/3.0/lists/{list_id}/activity
返回最近最多180天的每日聚合活动统计数据(退订、注册、打开、点击)。
批量操作
在一次调用中处理多个操作。
创建批量操作
POST /mailchimp/3.0/batches
Content-Type: application/json
{
"operations": [
{
"method": "POST",
"path": "/lists/abc123def4/members",
"body": "{\"email_address\":\"user1@example.com\",\"status\":\"subscribed\"}"
},
{
"method": "POST",
"path": "/lists/abc123def4/members",
"body": "{\"email_address\":\"user2@example.com\",\"status\":\"subscribed\"}"
}
]
}
获取批量状态
GET /mailchimp/3.0/batches/{batch_id}
列出所有批量操作
GET /mailchimp/3.0/batches
删除批量操作
DELETE /mailchimp/3.0/batches/{batch_id}
返回204 无内容操作成功时。
分页
Mailchimp 使用基于偏移量的分页:
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://gateway.maton.ai/mailchimp/3.0/lists?count=50&offset=100')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
响应包含总条目数用于计算总页数:
{
"lists": [...],
"total_items": 250
}
代码示例
JavaScript
const response = await fetch(
'https://gateway.maton.ai/mailchimp/3.0/lists',
{
headers: {
'Authorization': `Bearer ${process.env.MATON_API_KEY}`
}
}
);
const data = await response.json();
Python
import os
import requests
import hashlib
# 获取列表
response = requests.get(
'https://gateway.maton.ai/mailchimp/3.0/lists',
headers={'Authorization': f'Bearer {os.environ["MATON_API_KEY"]}'}
)
data = response.json()
# 添加订阅者
list_id = 'abc123def4'
email = 'newuser@example.com'
response = requests.post(
f'https://gateway.maton.ai/mailchimp/3.0/lists/{list_id}/members',
headers={
'Authorization': f'Bearer {os.environ["MATON_API_KEY"]}',
'Content-Type': 'application/json'
},
json={
'email_address': email,
'status': 'subscribed'
}
)
# 获取用于更新的订阅者哈希值
subscriber_hash = hashlib.md5(email.lower().encode()).hexdigest()
注意事项
- 列表ID是10个字符的字母数字字符串
- 订阅者哈希值是电子邮件地址小写形式的MD5哈希值
- 时间戳采用ISO 8601格式
- API调用的超时时间为120秒
- 列表端点的每次请求最多返回1000条记录
- "Audience"和"list"可以互换使用(分别是应用程序和API的术语)
- "Contact"和"member"可以互换使用(分别是应用程序和API的术语)
- 重要提示:使用curl命令时,如果URL包含括号(
请使用 curl -g例如:fields[]这类参数时排序[],记录[]) 以禁用通配符解析 - 重要提示:当将 curl 输出通过管道传递给
jq或其他命令时,在某些 shell 环境中,像$MATON_API_KEY这样的环境变量可能无法正确展开。通过管道传递时,您可能会遇到“无效 API 密钥”的错误。
响应代码
| 状态 | 含义 |
|---|---|
| 200 | 成功,并返回响应体 |
| 204 | 成功,无内容(DELETE、某些 POST 操作) |
| 400 | 错误请求或缺少 Mailchimp 连接 |
| 401 | 无效或缺少 Maton API 密钥 |
| 403 | 禁止访问 - 权限不足 |
| 404 | 资源未找到 |
| 405 | 方法不允许 |
| 429 | 请求频率受限 |
| 4xx/5xx | 来自 Mailchimp API 的透传错误 |
Mailchimp 错误响应包含详细信息:
{
"type": "https://mailchimp.com/developer/marketing/docs/errors/",
"title": "无效资源",
"status": 400,
"detail": "提交的资源无法通过验证。",
"instance": "abc123-def456",
"errors": [
{
"field": "email_address",
"message": "此值应为有效的电子邮件地址。"
}
]
}
故障排除:API 密钥问题
- 检查
MATON_API_KEY环境变量是否已设置:
echo $MATON_API_KEY
- 通过列出连接来验证 API 密钥是否有效:
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://ctrl.maton.ai/connections')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
故障排除:无效的应用名称
- 确保您的 URL 路径以
mailchimp开头。例如:
- 正确:
https://gateway.maton.ai/mailchimp/3.0/lists - 错误:
https://gateway.maton.ai/3.0/lists


微信扫一扫,打赏作者吧~