创建 Webhook 端点
验证端点可接收投递后,保存事件订阅并返回新的签名密钥。
POST
/api/settings/webhooks请求参数
| 参数 | 类型 | 位置 | 必填 | 说明 |
|---|---|---|---|---|
Origin | URI | header | 是 | 当前站点的完整来源。 |
url | URI | body | 是 | 公开 HTTPS 地址,最多 2,048 字符,须使用默认端口且不含凭据或片段。 |
events | string[] | body | 是 | 从列表接口返回的 eventTypes 中选择至少一项。重复项会自动合并。 |
请求示例
JavaScript 示例可在本站已登录的浏览器中运行。cURL 和 Python 示例读取你本机的 cookies.txt Cookie 文件。请将会话 Cookie 视为私密凭据。
cURL
curl -i -X POST 'https://social2api.com/api/settings/webhooks' \
--cookie ./cookies.txt \
-H 'Content-Type: application/json' \
-H 'Origin: https://social2api.com' \
--data-binary '{
"url": "https://example.com/webhook",
"events": [
"tool_run.finished"
]
}'Python
from urllib.error import HTTPError
from urllib.request import Request, HTTPCookieProcessor, build_opener
from http.cookiejar import MozillaCookieJar
request = Request(
"https://social2api.com/api/settings/webhooks",
method="POST",
headers={
"Content-Type": "application/json",
"Origin": "https://social2api.com",
},
data="{\"url\":\"https://example.com/webhook\",\"events\":[\"tool_run.finished\"]}".encode("utf-8"),
)
cookies = MozillaCookieJar("./cookies.txt")
cookies.load(ignore_discard=True)
send = build_opener(HTTPCookieProcessor(cookies)).open
try:
with send(request) as response:
print(response.status, response.read().decode("utf-8"))
except HTTPError as error:
print(error.code, error.read().decode("utf-8"))JavaScript
const response = await fetch("/api/settings/webhooks", {
method: "POST",
credentials: "same-origin",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
"url": "https://example.com/webhook",
"events": [
"tool_run.finished"
]
}),
});
console.log(response.status, await response.text());响应
201 · 示例响应
{
"endpoint": {
"id": "wh_4a5076aa-f9ab-46e6-b383-7b1374392dc4",
"url": "https://example.com/webhook",
"events": [
"tool_run.finished"
],
"enabled": true,
"createdAt": "2026-01-15T12:00:00.000Z",
"updatedAt": "2026-01-15T12:00:00.000Z"
},
"secret": "<new signing secret>",
"delivery": {
"id": "whd_8fd20315-91cc-4654-9d70-ae084e1cfa80",
"endpointId": "wh_4a5076aa-f9ab-46e6-b383-7b1374392dc4",
"url": "https://example.com/webhook",
"eventId": "evt_b30f6dbc-79c1-4d78-bad1-05ec854a50b2",
"type": "webhook.test",
"status": "delivered",
"statusCode": 204,
"error": null,
"createdAt": "2026-01-15T12:00:00.000Z"
}
}错误示例
{
"error": "请先登录。"
}| 字段 | 类型 | 说明 |
|---|---|---|
endpoint.id | string | 端点 ID。 |
endpoint.url | URI | 端点的接收地址。 |
endpoint.events | string[] | 订阅的事件类型。 |
endpoint.enabled | boolean | 是否开启自动事件投递。 |
endpoint.createdAt | ISO 8601 date-time | 端点创建时间。 |
endpoint.updatedAt | ISO 8601 date-time | 端点上次更新时间。 |
secret | string | 完整签名密钥。仅创建和轮换响应包含该字段。 |
delivery.id | string | 投递记录 ID。 |
delivery.endpointId | string | 接收端点 ID。 |
delivery.url | URI | 投递时使用的端点地址。 |
delivery.eventId | string | 事件 ID。同一事件重投时保持一致。 |
delivery.type | string | 事件类型。测试投递使用 webhook.test。 |
delivery.status | delivered | failed | 本次投递结果。 |
delivery.statusCode | integer | null | 端点返回的 HTTP 状态。连接失败时为 null。 |
delivery.error | string | null | 失败信息。投递成功时为 null。 |
delivery.createdAt | ISO 8601 date-time | 投递时间。 |
错误状态
| HTTP | 原因与处理 |
|---|---|
403 | 浏览器来源校验未通过。 |
401 | 控制台会话缺失、过期或无效。 |
405 | 请求方法不受支持。 |
503 | 此部署未配置该控制台服务。 |
400 | 请求格式或字段无效。 |
413 | 请求体超出 8,192 字节。 |
415 | 需要 application/json 请求体。 |
409 | 达到 10 个端点上限、端点 URL 已存在,或该端点同时被修改。 |
422 | 验证投递失败。确认公开 HTTPS 接收端及时返回 2xx。 |
使用细节
- 写请求须包含与站点完全相同的 Origin。
- JSON 请求体最多 8,192 字节。
- 服务先向候选地址发送一次 webhook.test,只有接收端成功响应 2xx 才保存。示例 URL 需替换为自己的接收地址。
- 接收端会收到 JSON 事件和 Shishu-Signature 请求头。签名是 HMAC-SHA256,输入为 Unix 秒级时间戳、点和原始请求体。使用端点密钥校验,事件 ID 可用于去重。