用即时沙盒密钥免账户试用 app2email WhatsApp API,然后连接您自己的号码,发送第一个模板,跟踪其状态并读取回复。
本指南分两部分,带您从零开始,发出一条 WhatsApp 消息并收到回复。
第 1 部分,即时沙盒(约 2 分钟,无需账户): 您会得到一个演示号码的密钥,向一位虚拟客户发送模板,看着它从已发送变为已读,并读取客户的回复。消息不会真正发到 WhatsApp,但每个请求和响应的格式都与真实 API 一致,所以同样的代码以后也能使用。
第 2 部分,您自己的号码: 您注册账户,连接您的 WhatsApp 号码,创建 API 密钥,然后向真实手机发送同样的消息。
客户看到的内容(第 2 部分):一条来自您企业名称的 WhatsApp 消息,例如“Ava 您好,您的订单 #1234 已发货。”客户回复后,您可以在接下来的 24 小时内用普通文本回复。
您看到的内容: 带有 message_id 的发送响应,该消息的状态(sent、delivered、read),作为 message.received 事件的客户回复,以及 app2email 活动日志和收件箱中的每一条消息。
curl 的终端,或 Node.js 18+,或装有 requests 的 Python 3.9+。不需要其他东西。| 权限范围 | 在本指南中的用途 |
|---|---|
messages:send | 发送模板和回复(POST /messages) |
messages:read | 列出您的模板(GET /templates),读取消息状态(GET /messages/{message_id})并获取事件(GET /events) |
webhooks:manage | 仅当您希望把事件推送到您的服务器时需要(POST /webhooks) |
无论权限范围如何,GET /me 对任何有效密钥都可用。
即时沙盒是一个演示用的 WhatsApp 号码 12025550100,带有五位虚拟客户,他们的反应与真实客户一样。开始时您不需要账户,也不需要密钥。
它提供:
POST https://api.app2email.com/v1/account/sandbox,会返回一个即时沙盒密钥(a2es_...)。它只显示一次,有效期为 7 天,只能用于演示号码。sandbox_recipient。hello_world(en_US,UTILITY)、order_update(en,UTILITY)和 weekend_offer(en,MARKETING)。它们由与真实模板相同的代码检查。GET /me、GET /templates、POST /messages、GET /messages、GET /conversations、GET /conversations/{contact}、PUT /conversations/{contact}/owner、GET /events 和 POST /sandbox/inbound(让一位虚拟客户给您发消息)。任何其他调用都会返回 403 not_in_instant_sandbox。GET /events 获取,而不是通过 webhook。curl -X POST https://api.app2email.com/v1/account/sandboxconst res = await fetch("https://api.app2email.com/v1/account/sandbox", { method: "POST" });
const { instant_sandbox } = await res.json();
console.log(instant_sandbox.api_key, instant_sandbox.expires);import requests
res = requests.post("https://api.app2email.com/v1/account/sandbox", timeout=10)
sandbox = res.json()["instant_sandbox"]
print(sandbox["api_key"], sandbox["expires"])您会得到的响应(已缩短:响应还会在 calls 和 try_first 中列出可用的调用以及最先该做的调用):
{
"success": true,
"request_id": "s4ndb",
"instant_sandbox": {
"api_key": "a2es_000qlA9iTHS1NETB9JExample...",
"expires": "2026-11-22T03:00:00.000Z",
"number": "12025550100",
"base_url": "https://api.app2email.com/v1/12025550100",
"mcp_url": "https://api.app2email.com/mcp",
"authorization": "Authorization: Bearer <api_key>",
"test_contacts": [
{ "number": "12025550101", "name": "Ava", "behaviour": "answers every message a few seconds later" },
{ "number": "12025550102", "name": "Ben", "behaviour": "reads, never answers: a free-form message to him is 422 outside_24h_window" },
{ "number": "12025550103", "name": "Cara", "behaviour": "delivery fails: message.status failed and message.failed" },
{ "number": "12025550104", "name": "Dan", "behaviour": "answers STOP: contact.opted_out, then a marketing template to him is 403 recipient_opted_out" },
{ "number": "12025550105", "name": "Eve", "behaviour": "asks for a person: set the conversation's owner to human" }
],
"templates": ["hello_world (en_US, UTILITY)", "order_update (en, UTILITY)", "weekend_offer (en, MARKETING)"],
"not_here": "Webhooks (use GET /events), media uploads, contacts, broadcasts in the background, scheduled sends and every other call need a connected number: they answer 403 not_in_instant_sandbox here."
},
"next": "For a real WhatsApp number call create_account with the person's e-mail address: they confirm, connect their number and choose a plan.",
"documentation_url": "https://www.app2email.com/docs/api/sandbox"
}请保存好 api_key:它不会再次显示。如果丢失,请重新获取一个。下文中的 SANDBOX_KEY 指代它。快速检查它是否可用:使用 Authorization: Bearer SANDBOX_KEY 调用 GET https://api.app2email.com/v1/12025550100/me。
GET /templates 会列出每个模板所需的值,以及一个完整的 example_request,您改好数值后即可发送。
curl https://api.app2email.com/v1/12025550100/templates \
-H "Authorization: Bearer SANDBOX_KEY"const KEY = process.env.SANDBOX_KEY;
const API = "https://api.app2email.com/v1/12025550100";
const res = await fetch(`${API}/templates`, { headers: { Authorization: `Bearer ${KEY}` } });
const { templates } = await res.json();
for (const t of templates) console.log(t.name, t.language, t.status, t.needs);import os, requests
KEY = os.environ["SANDBOX_KEY"]
API = "https://api.app2email.com/v1/12025550100"
res = requests.get(f"{API}/templates", headers={"Authorization": f"Bearer {KEY}"}, timeout=10)
for t in res.json()["templates"]:
print(t["name"], t["language"], t["status"], t.get("needs"))每个条目都有 name、language、category、status、can_send、needs,已批准的模板还有 example_request。order_update 的正文需要两个文本值:客户姓名和订单号。
Ava(12025550101)会在几秒后回复每一条消息。模板是您随时都可以发送的消息,即使对方还没有给您写过信。
curl -X POST https://api.app2email.com/v1/12025550100/messages \
-H "Authorization: Bearer SANDBOX_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": "12025550101",
"type": "template",
"template": {
"name": "order_update",
"language": { "code": "en" },
"components": [
{ "type": "body", "parameters": [
{ "type": "text", "text": "Ava" },
{ "type": "text", "text": "#1234" }
] }
]
}
}'const res = await fetch(`${API}/messages`, {
method: "POST",
headers: { Authorization: `Bearer ${KEY}`, "Content-Type": "application/json" },
body: JSON.stringify({
to: "12025550101",
type: "template",
template: {
name: "order_update",
language: { code: "en" },
components: [
{ type: "body", parameters: [{ type: "text", text: "Ava" }, { type: "text", text: "#1234" }] },
],
},
}),
});
const sent = await res.json();
console.log(res.status, sent.message_id, sent.status);res = requests.post(
f"{API}/messages",
headers={"Authorization": f"Bearer {KEY}"},
json={
"to": "12025550101",
"type": "template",
"template": {
"name": "order_update",
"language": {"code": "en"},
"components": [
{"type": "body", "parameters": [{"type": "text", "text": "Ava"}, {"type": "text", "text": "#1234"}]}
],
},
},
timeout=10,
)
sent = res.json()
print(res.status_code, sent.get("message_id"), sent.get("status"))您会得到的响应(HTTP 200,已缩短):消息已被接受。text 是客户看到的消息内容。
{
"success": true,
"request_id": "kxrkf",
"message_id": "wamid.HBgLNjE0MTIzNDU2NzgVAgARGBJTQU1QTEVfVEVTVF9FVkVOVAA=",
"status": "accepted",
"from": "12025550100",
"to": "12025550101",
"template": { "name": "order_update", "language": "en", "category": "UTILITY" },
"text": "Hi Ava, your order #1234 has shipped."
}错误的值会返回带有稳定 error.code 的错误、一个 how_to_fix,对于模板错误,还会附上一个完整且正确的 example_request。试一试:漏掉一个参数,您会得到 422 template_parameters_invalid。
GET /events?wait=25 最多等待 25 秒以获取下一个事件,所以您不需要公网地址。您会先收到带有 sent、delivered 和 read 的 message.status,然后收到带有 Ava 回复的 message.received。
curl "https://api.app2email.com/v1/12025550100/events?wait=25" \
-H "Authorization: Bearer SANDBOX_KEY"let after = "";
for (let i = 0; i < 5; i++) {
const q = after ? `&after=${encodeURIComponent(after)}` : "";
const res = await fetch(`${API}/events?wait=25${q}`, { headers: { Authorization: `Bearer ${KEY}` } });
const page = await res.json();
for (const e of page.events ?? []) console.log(e.type, e.data.status ?? e.data.text);
after = page.cursor ?? after;
}after = ""
for _ in range(5):
params = {"wait": 25, **({"after": after} if after else {})}
page = requests.get(f"{API}/events", params=params,
headers={"Authorization": f"Bearer {KEY}"}, timeout=40).json()
for e in page.get("events", []):
print(e["type"], e["data"].get("status") or e["data"].get("text"))
after = page.get("cursor", after)您会得到的响应(已缩短;Ava 的措辞会有变化):
{
"success": true,
"request_id": "e7fd2",
"events": [
{
"id": "evt_Xq3k9m2Lw8Rt5Yp1Zs01",
"type": "message.status",
"created": "2026-11-15T03:10:02.000Z",
"number": "12025550100",
"data": {
"message_id": "wamid.HBgLNjE0MTIzNDU2NzgVAgARGBJTQU1QTEVfVEVTVF9FVkVOVAA=",
"to": "12025550101",
"status": "delivered",
"timestamp": "2026-11-15T03:10:01.000Z",
"sent_by": "api"
}
},
{
"id": "evt_Ab7c2d9Ef4Gh6Jk8Lm03",
"type": "message.received",
"created": "2026-11-15T03:10:09.000Z",
"number": "12025550100",
"data": {
"message_id": "wamid.HBgLNjE0MTIzNDU2NzgVAgASGBQzQUZCMTY0MDc2MUYwNzBDNTY5MAA=",
"from": "12025550101",
"name": "Ava",
"type": "text",
"text": "Thanks! When will it arrive?",
"timestamp": "2026-11-15T03:10:08.000Z"
}
}
],
"count": 2,
"cursor": "ec_184467",
"has_more": false
}下次调用时把 cursor 作为 after 传入,只获取更新的事件。每个事件都与之后 webhook 收到的 JSON 相同,id 也相同。types=message.received 只返回收到的消息。GET /messages 也会列出消息,每条已发送的消息都带有其 status 和 times。
Ava 刚刚给您写了消息,所以她的 24 小时窗口已经开启,您可以发送自由文本:
curl -X POST https://api.app2email.com/v1/12025550100/messages \
-H "Authorization: Bearer SANDBOX_KEY" \
-H "Content-Type: application/json" \
-d '{"to": "12025550101", "type": "text", "text": {"body": "It arrives on Friday."}}'向从未给您写过信的 Ben(12025550102)发送同样的文本,会返回 422 outside_24h_window:在窗口之外只能发送模板。
| 客户 | 发送给他们 | 您会了解到什么 |
|---|---|---|
Ben,12025550102 | 一条文本 | 他会读消息但从不回复:自由文本返回 422 outside_24h_window |
Cara,12025550103 | hello_world | 送达失败:带有 failed 的 message.status,以及 message.failed |
Dan,12025550104 | order_update,然后是 weekend_offer | 他回复 STOP:contact.opted_out,之后营销模板会返回 403 recipient_opted_out |
Eve,12025550105 | 任意模板 | 她要求找真人:用 PUT /conversations/{contact}/owner 和 "owner": "human" 转交对话 |
用 AI 助手代替终端: Management MCP 服务器 https://api.app2email.com/mcp/manage 提供工具 create_sandbox,而且即时密钥也可用于 MCP 服务器 https://api.app2email.com/mcp(check_key、list_templates、send_template、list_events、wait_for_message 等)。请参阅用 MCP 让 AI 代理运行您的 WhatsApp 和沙盒。
消息能否真正发出由 Meta 一方决定:请为您的 WhatsApp Business Account 添加付款方式,验证您的企业,并让您的显示名称获得批准。入门指南会带您完成这些步骤。
在 API 密钥页面创建一个具有 messages:send 和 messages:read 的密钥(如果需要 webhook,再加上 webhooks:manage)。密钥(a2en_...)只显示一次:请把它保存在您的服务器上,不要放进网页或日志里。
创建时勾选 沙盒密钥 可先在您自己的号码上测试:所有检查都针对您的账户运行,不会向 WhatsApp 发送任何内容,响应带有 "sandbox": true,消息 ID 以 wamid.SANDBOX. 开头,message.status 事件仍会送达。准备好真正发送时,再创建一个不带该标记的密钥。
在每个请求中,YOUR_NUMBER 是您发送消息所用的 WhatsApp 号码:国家代码在前,只写数字。
curl https://api.app2email.com/v1/YOUR_NUMBER/me \
-H "Authorization: Bearer YOUR_API_KEY"const res = await fetch("https://api.app2email.com/v1/YOUR_NUMBER/me", {
headers: { Authorization: `Bearer ${process.env.API_KEY}` },
});
console.log(await res.json());import os, requests
res = requests.get("https://api.app2email.com/v1/YOUR_NUMBER/me",
headers={"Authorization": f"Bearer {os.environ['API_KEY']}"}, timeout=10)
print(res.json())您会得到的响应:
{
"success": true,
"request_id": "a7k3p",
"key_valid": true,
"api_version": "v1",
"number": "4976659348270",
"plan": "standard_5000",
"service_status": "LIVE",
"key": { "name": "First message", "scopes": ["messages:read", "messages:send"], "ip_allow": "" },
"limits": { "max_numbers_per_request": 1000, "requests_per_second": 20, "burst": 50, "rate_limit_mode": "enforce", "webhooks_max": 5 }
}service_status 必须是 LIVE,key.scopes 必须列出您需要的权限范围(或用 * 表示完全访问)。沙盒密钥还会显示 "sandbox": true。
GET /v1/YOUR_NUMBER/templates 会列出您的模板。选择 "status": "APPROVED" 且 "can_send": true 的模板,并复制它的 example_request。还没有已批准的模板?请在控制台的“模板”页面创建一个,或参阅发送订单和预约通知,了解如何通过 API 创建并获得批准。
把模板发送到您自己的手机号码(不是企业号码本身:向企业号码发送会返回 422 recipient_is_sender)。请加上 Idempotency-Key:如果超时后重试,消息不会被发送两次。
curl -X POST https://api.app2email.com/v1/YOUR_NUMBER/messages \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: first-message-1" \
-d '{
"to": "61412345678",
"type": "template",
"template": {
"name": "order_update",
"language": { "code": "en" },
"components": [
{ "type": "body", "parameters": [
{ "type": "text", "text": "John" },
{ "type": "text", "text": "#1234" }
] }
]
}
}'const res = await fetch("https://api.app2email.com/v1/YOUR_NUMBER/messages", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.API_KEY}`,
"Content-Type": "application/json",
"Idempotency-Key": "first-message-1",
},
body: JSON.stringify({
to: "61412345678",
type: "template",
template: {
name: "order_update",
language: { code: "en" },
components: [
{ type: "body", parameters: [{ type: "text", text: "John" }, { type: "text", text: "#1234" }] },
],
},
}),
});
const sent = await res.json();
console.log(res.status, sent.message_id ?? sent.error);res = requests.post(
"https://api.app2email.com/v1/YOUR_NUMBER/messages",
headers={"Authorization": f"Bearer {os.environ['API_KEY']}", "Idempotency-Key": "first-message-1"},
json={
"to": "61412345678",
"type": "template",
"template": {
"name": "order_update",
"language": {"code": "en"},
"components": [
{"type": "body", "parameters": [{"type": "text", "text": "John"}, {"type": "text", "text": "#1234"}]}
],
},
},
timeout=10,
)
sent = res.json()
print(res.status_code, sent.get("message_id") or sent.get("error"))请使用您自己模板的名称、语言和值(它的 example_request 里有这些内容)。您会得到的响应(HTTP 200):
{
"success": true,
"request_id": "kxrkf",
"message_id": "wamid.HBgLNjE0MTIzNDU2NzgVAgARGBJTQU1QTEVfVEVTVF9FVkVOVAA=",
"status": "accepted",
"from": "4976659348270",
"to": "61412345678",
"template": { "name": "order_update", "language": "en", "category": "UTILITY" },
"text": "Hi John, your order #1234 has shipped.",
"conversation_url": "https://www.app2email.com/conversation/61412345678_4976659348270_...",
"note": "accepted by WhatsApp. delivery and read status appear in your app2email Activity Log (WhatsApp sent).",
"contacts": [{ "input": "61412345678", "wa_id": "61412345678" }]
}在请求体中加入 "dry_run": true,可以检查所有内容(模板值、号码、Do Not Call 名单、月度限额)而不实际发送。响应会显示确切的 WhatsApp 消息。不会发送、记录或计数。
GET /messages/{message_id}(权限范围 messages:read)会显示消息进行到哪一步。请对 ID 进行 URL 编码。
curl "https://api.app2email.com/v1/YOUR_NUMBER/messages/wamid.HBgLNjE0MTIzNDU2NzgVAgARGBJTQU1QTEVfVEVTVF9FVkVOVAA%3D" \
-H "Authorization: Bearer YOUR_API_KEY"您会得到的响应: status 是 accepted、sent、delivered、read、failed、deleted、queued 或 unknown 之一。尚未到达的步骤不会出现在 times 中,客户回复这条消息后会出现 reply。
{
"success": true,
"request_id": "p4n8w",
"message_id": "wamid.HBgLNjE0MTIzNDU2NzgVAgARGBJTQU1QTEVfVEVTVF9FVkVOVAA=",
"to": "61412345678",
"status": "read",
"sent_by": "api",
"text": "Hi John, your order #1234 has shipped.",
"times": {
"submitted": "2026-11-06T01:00:00Z",
"accepted": "2026-11-06T01:00:01Z",
"sent": "2026-11-06T01:00:02Z",
"delivered": "2026-11-06T01:00:04Z",
"read": "2026-11-06T01:12:40Z"
},
"conversation_url": "https://www.app2email.com/conversation/61412345678_4976659348270_..."
}在手机上回复这条消息。有两种方式可以在代码中获得回复:
GET /v1/YOUR_NUMBER/events?wait=25&types=message.received,与第 1 部分相同。在您自己的号码上,第一次调用会开启事件流,此后事件会保留 72 小时。所以请在发送之前先用 after=now 调用一次,并保存 cursor。POST /v1/YOUR_NUMBER/webhooks 和 {"url": "https://your-server.example/webhook"} 注册一次 webhook(权限范围 webhooks:manage),或者在 API 密钥页面的 Webhooks 下添加。之后 app2email 会把每个事件作为带签名的 POST 发送到您的 URL。构建 WhatsApp 聊天机器人中有包含签名校验的完整接收程序。回复会以 message.received 的形式到达:
{
"id": "evt_Xq3k9m2Lw8Rt5Yp1Zs01",
"type": "message.received",
"created": "2026-11-06T01:00:00.000Z",
"number": "4976659348270",
"data": {
"message_id": "wamid.HBgLNjE0MTIzNDU2NzgVAgARGBJTQU1QTEVfVEVTVF9FVkVOVAA=",
"from": "61412345678",
"name": "John",
"type": "text",
"text": "Thanks! When will it arrive?",
"conversation_url": "https://www.app2email.com/conversation/61412345678_4976659348270_SAMPLEk3XbQ9rTzP2mW",
"timestamp": "2026-11-06T01:00:00.000Z"
}
}现在您可以像第 1 部分一样用 "type": "text" 回复,直到客户最后一条消息之后的 24 小时结束。
message.status 事件:sent,然后是 delivered,您打开它时是 read。失败的消息会以带有 status: "failed" 和 error 对象的 message.status 到达,同时也会以 message.failed 到达。message.received 到达,同时用于自由格式回复的 24 小时窗口开启。每条消息只发送一次。 给每个 POST /messages 一个只属于该消息的 Idempotency-Key,例如您的订单号加上事件名。24 小时内用同一个密钥发出相同请求,会再次得到第一次的响应,并带有头 Idempotent-Replayed: true。同一个密钥配不同的请求体:422 idempotency_key_reused。
按事件的 id 去重。 同一个事件可能再次到来:丢失响应后来自 GET /events,重试后来自 webhook。请把您已处理的 ID 至少保留两天。
校验 webhook 签名。 每个 webhook 事件都带有 X-App2email-Signature: t=<unix seconds>,v1=<hex>:以 webhook 的密钥(whsec_...)为密钥,对 t、一个点和原始请求体计算 HMAC-SHA256。拒绝不匹配或 t 过旧的事件。
遵守速率限制。 每个响应都带有 X-RateLimit-Limit、X-RateLimit-Remaining 和 X-RateLimit-Reset。超出限制时会返回 429 rate_limited 和 Retry-After:等待相应的时间后再重试。若要把一个模板发给多个号码,请把它们放在一次调用中,而不是每个号码调用一次。
24 小时窗口之外使用模板。 自由文本只在客户最后一条消息之后的 24 小时内有效(app2email 会提前 15 分钟停止)。其他情况都需要已批准的模板。
只给同意的人发消息。 营销模板不会发给 Do Not Call 名单上的号码,写下 STOP 的客户会被加入该名单(contact.opted_out)。发送尊重 opt-in 的营销群发介绍了用户同意的相关内容。
保护好密钥,权限要精简。 密钥放在服务器上,绝不放进网页。只授予它所需的权限范围,测试时使用沙盒密钥。
| 您看到的现象 | 原因 | 解决方法 |
|---|---|---|
POST /v1/account/sandbox 返回 429 | 同一地址每小时获取了超过 10 个即时沙盒密钥 | 等待 Retry-After 秒,或继续使用您已有的密钥(有效期 7 天) |
401 missing_api_key、invalid_api_key、api_key_expired、api_key_revoked | 没有密钥、密钥错误、已过期(即时沙盒密钥有效期为 7 天)或已被撤销 | 用有效的密钥发送 Authorization: Bearer YOUR_API_KEY;重新获取即时密钥,或使用 API 密钥页面上的密钥 |
403 api_key_number_mismatch | 密钥属于 URL 中号码以外的另一个号码 | 在 URL 中使用该密钥自己的号码。即时沙盒密钥只能用于 12025550100 |
400 invalid_number_in_url | URL 中的号码不是带国家代码的数字 | 使用完整号码,只写数字,不带 + |
403 not_in_instant_sandbox | 演示号码没有这个调用(例如 webhook) | 在演示号码上改用 GET /events,或使用您自己号码上的沙盒密钥 |
422 sandbox_recipient | 即时沙盒只会向它的虚拟客户发送 | 发送到 12025550101 至 12025550105 |
403 plan_not_allowed、service_not_live | 没有付费套餐,或服务已暂停 | 选择套餐;已暂停的服务可在“设置”页面通过“Go Live”恢复上线 |
403 insufficient_scope | 密钥缺少某项权限(details.required_scope) | 创建具有 messages:send 和 messages:read 的密钥 |
403 sandbox_key | 沙盒密钥不能执行这个操作(模板、设置、审批) | 这个调用请使用不带沙盒标记的密钥 |
404 template_not_found、template_language_not_found | 模板名称或语言有误 | 从 GET /templates 复制两者 |
422 template_not_approved | Meta 尚未批准该模板 | 等待批准,或选择 can_send: true 的模板 |
422 template_parameters_invalid、template_parameter_count_mismatch | 值的数量或类型有误 | 根据错误中的 details 和 example_request 修正 |
422 outside_24h_window | 向最近 24 小时内没有给您写过信的人发送了自由文本 | 发送已批准的模板 |
422 recipient_is_sender | 您发送给了企业号码本身 | 发送到另一部手机 |
403 recipient_opted_out | 客户在您的 Do Not Call 名单上 | 不要向他们发送营销消息 |
403 whatsapp_payment_issue | 您的 WhatsApp Business Account 没有有效的付款方式(Meta 错误 131042) | 在 WhatsApp Manager 的“账单和付款”下添加付款方式 |
429 rate_limited | 该密钥的调用过多 | 等待 Retry-After 秒,然后重试 |
在您自己的号码上 GET /events 一直为空 | 事件流从第一次调用开始 | 发送前先调用一次(after=now),然后再发送 |
每个响应都带有 request_id。GET /requests/{id} 会显示在您自己的号码上为它记录的内容,support@app2email.com 也能查到它。所有错误代码请见错误。