微信 API 接口文档:快速开始、接口参考、回调与错误码
这份微信 API 接口文档涵盖个人微信与企业微信接口,统一使用 REST + JSON。接口细节以开通账号后控制台中的最新文档为准。
本页是公开的接口摘要
这里列出了 18 个常用接口。完整版 API 文档包含全部接口和详细的参数、返回字段与回调说明,留下联系方式即可获取。
鉴权与约定
- 接口地址:
https://api.wechatapilabs.com - 鉴权:请求头
Authorization: Bearer YOUR_API_KEY - 请求与返回均为 UTF-8 JSON;返回结构统一为
{"code", "message", "request_id", "data"},code = 0表示成功 - 时间字段为 Unix 秒级时间戳
- 默认限流:每个账号每秒 5 次请求,超限返回
45009
快速开始
import requests
resp = requests.post(
"https://api.wechatapilabs.com/v1/message/send_text",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={"account_id": "acc_123", "to": "wxid_friend", "content": "你好,这是一条测试消息"},
timeout=10,
)
print(resp.json()) # {"code": 0, "data": {"msg_id": "..."}}HttpClient client = HttpClient.newHttpClient();
String body = """
{"account_id":"acc_123","to":"wxid_friend","content":"你好,这是一条测试消息"}
""";
HttpRequest req = HttpRequest.newBuilder(URI.create("https://api.wechatapilabs.com/v1/message/send_text"))
.header("Authorization", "Bearer YOUR_API_KEY")
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(body))
.build();
System.out.println(client.send(req, HttpResponse.BodyHandlers.ofString()).body());const res = await fetch("https://api.wechatapilabs.com/v1/message/send_text", {
method: "POST",
headers: { Authorization: "Bearer YOUR_API_KEY", "Content-Type": "application/json" },
body: JSON.stringify({ account_id: "acc_123", to: "wxid_friend", content: "你好,这是一条测试消息" }),
});
console.log(await res.json());body := strings.NewReader(`{"account_id":"acc_123","to":"wxid_friend","content":"你好,这是一条测试消息"}`)
req, _ := http.NewRequest("POST", "https://api.wechatapilabs.com/v1/message/send_text", body)
req.Header.Set("Authorization", "Bearer YOUR_API_KEY")
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil { log.Fatal(err) }
defer resp.Body.Close()
out, _ := io.ReadAll(resp.Body)
fmt.Println(string(out))curl -X POST https://api.wechatapilabs.com/v1/message/send_text \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"account_id":"acc_123","to":"wxid_friend","content":"你好,这是一条测试消息"}' 账号与登录接口
获取登录二维码
为一个新的托管账号生成登录二维码,用手机扫码确认后账号上线。上线结果通过 account.online 回调通知。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
type | string | 是 | personal(个人微信)或 wecom(企业微信) |
callback_url | string | 否 | 该账号的事件回调地址,不填则使用控制台默认地址 |
返回示例
{
"code": 0,
"message": "ok",
"request_id": "req_5c1e",
"data": {
"account_id": "acc_123",
"qrcode_url": "https://.../qr.png",
"expire_in": 120
}
}查询账号状态
查询托管账号是否在线、登录设备和最近心跳时间。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
account_id | string | 是 | 托管账号 ID,在控制台或登录接口返回中获取 |
返回示例
{
"code": 0,
"message": "ok",
"request_id": "req_5c1e",
"data": {
"account_id": "acc_123",
"online": true,
"nickname": "客服小王",
"last_heartbeat": 1764172800
}
}消息接口
发送文本消息
向好友或群发送文本消息。发送到群时可通过 at 参数 @ 指定成员。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
account_id | string | 是 | 托管账号 ID,在控制台或登录接口返回中获取 |
to | string | 是 | 好友 wxid 或群 ID(以 @chatroom 结尾) |
content | string | 是 | 消息内容,最长 2000 字 |
at | string[] | 否 | 群内要 @ 的成员 wxid 列表,传 ["all"] 表示 @所有人(需群主或管理员) |
返回示例
{
"code": 0,
"message": "ok",
"request_id": "req_5c1e",
"data": {
"msg_id": "m_9f2c1a"
}
}发送图片
发送图片消息,支持 URL 或 Base64。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
account_id | string | 是 | 托管账号 ID,在控制台或登录接口返回中获取 |
to | string | 是 | 好友 wxid 或群 ID |
image_url | string | 二选一 | 图片地址,需公网可访问 |
image_base64 | string | 二选一 | 图片 Base64 内容,不超过 10MB |
返回示例
{
"code": 0,
"message": "ok",
"request_id": "req_5c1e",
"data": {
"msg_id": "m_9f2c1b"
}
}发送文件
发送文件消息,文件名显示为 file_name。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
account_id | string | 是 | 托管账号 ID,在控制台或登录接口返回中获取 |
to | string | 是 | 好友 wxid 或群 ID |
file_url | string | 是 | 文件地址 |
file_name | string | 是 | 显示的文件名 |
返回示例
{
"code": 0,
"message": "ok",
"request_id": "req_5c1e",
"data": {
"msg_id": "m_9f2c1c"
}
}发送链接卡片
发送带标题、描述和缩略图的链接卡片。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
account_id | string | 是 | 托管账号 ID,在控制台或登录接口返回中获取 |
to | string | 是 | 好友 wxid 或群 ID |
title | string | 是 | 标题 |
desc | string | 否 | 描述 |
url | string | 是 | 跳转链接 |
thumb_url | string | 否 | 缩略图 |
返回示例
{
"code": 0,
"message": "ok",
"request_id": "req_5c1e",
"data": {
"msg_id": "m_9f2c1d"
}
}撤回消息
撤回 2 分钟内由该账号发出的消息。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
account_id | string | 是 | 托管账号 ID,在控制台或登录接口返回中获取 |
msg_id | string | 是 | 发送接口返回的 msg_id |
返回示例
{
"code": 0,
"message": "ok",
"request_id": "req_5c1e",
"data": {
"revoked": true
}
}联系人接口
获取好友列表
分页获取好友列表及备注、标签。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
account_id | string | 是 | 托管账号 ID,在控制台或登录接口返回中获取 |
cursor | string | 否 | 分页游标,首次不传 |
limit | int | 否 | 每页数量,默认 100,最大 500 |
返回示例
{
"code": 0,
"message": "ok",
"request_id": "req_5c1e",
"data": {
"items": [
{
"wxid": "wxid_abc",
"nickname": "张三",
"remark": "客户-张三",
"tags": [
"VIP"
]
}
],
"next_cursor": "c_2"
}
}通过好友申请
通过 friend.request 回调中收到的好友申请。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
account_id | string | 是 | 托管账号 ID,在控制台或登录接口返回中获取 |
ticket | string | 是 | 回调中的申请凭证 |
remark | string | 否 | 通过后设置的备注 |
返回示例
{
"code": 0,
"message": "ok",
"request_id": "req_5c1e",
"data": {
"wxid": "wxid_new"
}
}修改备注与标签
修改好友备注名和标签。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
account_id | string | 是 | 托管账号 ID,在控制台或登录接口返回中获取 |
wxid | string | 是 | 好友 wxid |
remark | string | 否 | 新备注 |
tags | string[] | 否 | 标签列表,覆盖原标签 |
返回示例
{
"code": 0,
"message": "ok",
"request_id": "req_5c1e",
"data": {
"ok": true
}
}群接口
创建群聊
用指定好友创建群聊,至少需要 2 位好友。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
account_id | string | 是 | 托管账号 ID,在控制台或登录接口返回中获取 |
members | string[] | 是 | 好友 wxid 列表 |
name | string | 否 | 群名称 |
返回示例
{
"code": 0,
"message": "ok",
"request_id": "req_5c1e",
"data": {
"group_id": "123456@chatroom"
}
}邀请成员进群
邀请好友进群;超过 40 人的群将发送邀请链接。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
account_id | string | 是 | 托管账号 ID,在控制台或登录接口返回中获取 |
group_id | string | 是 | 群 ID |
members | string[] | 是 | 好友 wxid 列表 |
返回示例
{
"code": 0,
"message": "ok",
"request_id": "req_5c1e",
"data": {
"invited": 3
}
}移出群成员
需要该账号为群主或管理员。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
account_id | string | 是 | 托管账号 ID,在控制台或登录接口返回中获取 |
group_id | string | 是 | 群 ID |
members | string[] | 是 | 要移出的成员 wxid |
返回示例
{
"code": 0,
"message": "ok",
"request_id": "req_5c1e",
"data": {
"removed": 1
}
}发布群公告
需要该账号为群主或管理员。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
account_id | string | 是 | 托管账号 ID,在控制台或登录接口返回中获取 |
group_id | string | 是 | 群 ID |
content | string | 是 | 公告内容 |
返回示例
{
"code": 0,
"message": "ok",
"request_id": "req_5c1e",
"data": {
"ok": true
}
}获取群成员
获取群成员列表与群昵称。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
account_id | string | 是 | 托管账号 ID,在控制台或登录接口返回中获取 |
group_id | string | 是 | 群 ID |
返回示例
{
"code": 0,
"message": "ok",
"request_id": "req_5c1e",
"data": {
"items": [
{
"wxid": "wxid_abc",
"display_name": "张三"
}
],
"total": 128
}
}朋友圈接口
发布朋友圈
发布文字或图文朋友圈,最多 9 张图片。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
account_id | string | 是 | 托管账号 ID,在控制台或登录接口返回中获取 |
content | string | 否 | 文字内容 |
images | string[] | 否 | 图片 URL 列表 |
visible_tags | string[] | 否 | 仅对这些标签的好友可见 |
返回示例
{
"code": 0,
"message": "ok",
"request_id": "req_5c1e",
"data": {
"moment_id": "mo_7d1"
}
}企业微信接口
外部群发送消息
向包含客户的外部群发送消息,支持 text、image、file、link、miniprogram 类型。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
account_id | string | 是 | 托管账号 ID,在控制台或登录接口返回中获取 |
group_id | string | 是 | 外部群 ID |
type | string | 是 | 消息类型 |
payload | object | 是 | 对应类型的消息内容,字段同个人微信发送接口 |
at | string[] | 否 | 要 @ 的成员 ID |
返回示例
{
"code": 0,
"message": "ok",
"request_id": "req_5c1e",
"data": {
"msg_id": "wm_31a"
}
}获取客户列表
获取该成员的客户列表、备注与企业标签。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
account_id | string | 是 | 托管账号 ID,在控制台或登录接口返回中获取 |
cursor | string | 否 | 分页游标 |
limit | int | 否 | 每页数量,默认 100 |
返回示例
{
"code": 0,
"message": "ok",
"request_id": "req_5c1e",
"data": {
"items": [
{
"external_id": "wo_abc",
"name": "李四",
"corp_name": "某公司",
"tags": [
"意向客户"
]
}
],
"next_cursor": null
}
}回调事件
在控制台或登录接口中设置回调地址后,事件会以 POST JSON 推送。请求头带 X-Signature,值为 HMAC-SHA256(回调密钥, 请求体) 的十六进制字符串,请先校验再处理。你的服务需在 5 秒内返回 HTTP 200,否则会按 10 秒、60 秒、5 分钟重试 3 次。
message.received
收到新消息(单聊或群聊)
{
"event": "message.received",
"account_id": "acc_123",
"data": {
"msg_id": "m_1",
"from": "wxid_abc",
"room_id": "123456@chatroom",
"type": "text",
"content": "@客服小王 你好",
"at_me": true,
"time": 1764172800
}
}friend.request
收到好友申请
{
"event": "friend.request",
"account_id": "acc_123",
"data": {
"from": "wxid_new",
"nickname": "王五",
"hello": "我是王五",
"ticket": "t_8a2"
}
}group.member_join
有新成员进群
{
"event": "group.member_join",
"account_id": "acc_123",
"data": {
"room_id": "123456@chatroom",
"members": [
"wxid_x"
],
"inviter": "wxid_abc"
}
}account.online / account.offline
账号上线或掉线
{
"event": "account.offline",
"account_id": "acc_123",
"data": {
"reason": "logout_on_phone",
"time": 1764172800
}
}错误码
| code | 含义 | 处理建议 |
|---|---|---|
0 | 成功 | |
40001 | API Key 无效或已过期 | 在控制台重新生成 API Key |
40003 | account_id 不存在或不属于当前 Key | 检查账号 ID 与 Key 是否对应 |
40010 | 参数缺失或格式错误 | 按文档检查必填字段与类型 |
42001 | 账号离线 | 调用登录接口重新扫码,或订阅 account.offline 回调 |
42002 | 目标好友或群不存在 | 先通过好友列表 / 群列表接口确认 ID |
43001 | 无群管理权限 | 该操作需要群主或管理员身份 |
45009 | 调用频率超限 | 降低频率,按返回的 retry_after 秒后重试 |
46001 | 触发平台风控,操作被限制 | 暂停该账号的主动操作 24 小时,并联系技术支持 |
50000 | 服务内部错误 | 稍后重试,持续出现请联系技术支持并提供 request_id |
想看全部接口和完整参数说明?留下联系方式,我们把完整版 API 文档发给你。