# Webhook 端点

> iMario 发送事件的目标端点、测试事件、签名密钥和投递记录。仅限所有者和管理员。

Source: https://imario.ai/zh/docs/api/reference/webhooks

基础地址为 `https://api.imario.ai/v1`。每次调用所用的密钥都要带有各接口标明的权限范围，密钥的角色也要达到标明的要求（参见[认证](https://imario.ai/zh/docs/api/authentication)）。错误的格式见 [API 错误](https://imario.ai/zh/docs/api/errors)。

## 列出 Webhook 端点
`GET /v1/webhook-endpoints`

**权限范围** `webhooks:read` · **角色** 所有者或管理员

与密钥模式（正式或测试）相同的端点，按时间倒序排列。`webhook_endpoint` 对象包含：`id`、`livemode`（测试密钥创建的端点是测试端点，只接收沙盒事件）、`created_at`、`url`、`description`、`events`（事件类型，`["*"]` 表示全部）、`enabled`、`disabled_reason`（`delivery_failures` 表示投递连续 3 天失败后被 iMario 自动关闭，`disabled_by_user` 表示被手动关闭，也可能为 null）。

**参数**

| 名称 | 位置 | 类型 | 必填 | 说明 |
|---|---|---|---|---|
| `limit` | 查询参数 | `integer` | 否 | 返回的对象个数（1 到 100）。默认值为 `20`。 |
| `cursor` | 查询参数 | `string` | 否 | 上一页返回的 `next_cursor`。最多 200 个字符。 |

**示例**

```bash
curl https://api.imario.ai/v1/webhook-endpoints \
  -H "Authorization: Bearer $IMARIO_API_KEY"
```

**响应**

返回一个列表：`{"object": "list", "data": [...], "has_more": false, "next_cursor": null}`。参见[分页](https://imario.ai/zh/docs/api/pagination)。

## 创建 Webhook 端点
`POST /v1/webhook-endpoints`

**权限范围** `webhooks:write` · **角色** 所有者或管理员

响应中带有 `secret`（`whsec_...`），只显示这一次：请妥善保存，用来验证 `webhook-signature`（遵循 Standard Webhooks 规范）。URL 必须是 https，并且只能解析到公网地址；每次投递时都会重新检查。`webhook_endpoint` 对象包含：`id`、`livemode`（测试密钥创建的端点是测试端点，只接收沙盒事件）、`created_at`、`url`、`description`、`events`（事件类型，`["*"]` 表示全部）、`enabled`、`disabled_reason`（`delivery_failures` 表示投递连续 3 天失败后被 iMario 自动关闭，`disabled_by_user` 表示被手动关闭，也可能为 null）。

**请求体**

| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| `url` | `string` | 是 | 一个公开的 https URL。不会跟随重定向。最多 2,048 个字符。 |
| `events` | `string[]` | 是 | 要接收的事件类型，["*"] 表示全部类型。最多 20 项。 |
| `description` | `string` | 否 | 最多 200 个字符。 |

**示例**

```bash
curl https://api.imario.ai/v1/webhook-endpoints \
  -H "Authorization: Bearer $IMARIO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com/imario/webhooks",
    "events": ["study.completed", "study.failed", "report.completed"],
    "description": "Production receiver"
  }'
```

## 获取 Webhook 端点
`GET /v1/webhook-endpoints/{endpoint_id}`

**权限范围** `webhooks:read` · **角色** 所有者或管理员

`webhook_endpoint` 对象包含：`id`、`livemode`（测试密钥创建的端点是测试端点，只接收沙盒事件）、`created_at`、`url`、`description`、`events`（事件类型，`["*"]` 表示全部）、`enabled`、`disabled_reason`（`delivery_failures` 表示投递连续 3 天失败后被 iMario 自动关闭，`disabled_by_user` 表示被手动关闭，也可能为 null）。

**示例**

```bash
curl https://api.imario.ai/v1/webhook-endpoints/ENDPOINT_ID \
  -H "Authorization: Bearer $IMARIO_API_KEY"
```

## 更新 Webhook 端点
`PATCH /v1/webhook-endpoints/{endpoint_id}`

**权限范围** `webhooks:write` · **角色** 所有者或管理员

只传要修改的字段。`webhook_endpoint` 对象包含：`id`、`livemode`（测试密钥创建的端点是测试端点，只接收沙盒事件）、`created_at`、`url`、`description`、`events`（事件类型，`["*"]` 表示全部）、`enabled`、`disabled_reason`（`delivery_failures` 表示投递连续 3 天失败后被 iMario 自动关闭，`disabled_by_user` 表示被手动关闭，也可能为 null）。

**请求体**

| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| `url` | `string` | 否 | 最多 2,048 个字符。 |
| `events` | `string[]` | 否 | 最多 20 项。 |
| `description` | `string` | 否 | 最多 200 个字符。 |
| `enabled` | `boolean` | 否 | 设为 false 时停止投递（并丢弃待投递的事件）；设为 true 时重新开启端点。 |

**示例**

```bash
curl -X PATCH https://api.imario.ai/v1/webhook-endpoints/ENDPOINT_ID \
  -H "Authorization: Bearer $IMARIO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "enabled": false
  }'
```

## 删除 Webhook 端点
`DELETE /v1/webhook-endpoints/{endpoint_id}`

**权限范围** `webhooks:write` · **角色** 所有者或管理员

等待投递到这个端点的事件会被丢弃。返回 `{object, id, deleted: true}`。

**示例**

```bash
curl -X DELETE https://api.imario.ai/v1/webhook-endpoints/ENDPOINT_ID \
  -H "Authorization: Bearer $IMARIO_API_KEY"
```

## 发送测试事件
`POST /v1/webhook-endpoints/{endpoint_id}/test`

**权限范围** `webhooks:write` · **角色** 所有者或管理员

把一个只发给这个端点的 `ping` 事件加入投递队列（事件的 `data.object` 就是这个端点），并返回 `webhook_delivery`：`id`、`endpoint_id`、`event_id`、`event_type`、`status`（pending、delivered、failed）、`attempts`、`last_status_code`、`last_error`、`created_at`、`delivered_at`、`next_attempt_at`。

**示例**

```bash
curl -X POST https://api.imario.ai/v1/webhook-endpoints/ENDPOINT_ID/test \
  -H "Authorization: Bearer $IMARIO_API_KEY"
```

## 轮换签名密钥
`POST /v1/webhook-endpoints/{endpoint_id}/rotate-secret`

**权限范围** `webhooks:write` · **角色** 所有者或管理员

返回新的 `secret`，只显示这一次。接下来 24 小时内，每次投递都带两个签名（分别用新、旧签名密钥生成），接收方可以从容切换，不会漏掉事件。`webhook_endpoint` 对象包含：`id`、`livemode`（测试密钥创建的端点是测试端点，只接收沙盒事件）、`created_at`、`url`、`description`、`events`（事件类型，`["*"]` 表示全部）、`enabled`、`disabled_reason`（`delivery_failures` 表示投递连续 3 天失败后被 iMario 自动关闭，`disabled_by_user` 表示被手动关闭，也可能为 null）。

**示例**

```bash
curl -X POST https://api.imario.ai/v1/webhook-endpoints/ENDPOINT_ID/rotate-secret \
  -H "Authorization: Bearer $IMARIO_API_KEY"
```

## 列出端点的投递记录
`GET /v1/webhook-endpoints/{endpoint_id}/deliveries`

**权限范围** `webhooks:read` · **角色** 所有者或管理员

按时间倒序排列，保留 30 天。每个 `webhook_delivery` 包含：`id`、`endpoint_id`、`event_id`（即 `webhook-id` 请求头）、`event_type`、`status`（pending、delivered、failed）、`attempts`、`last_status_code`、`last_error`、`created_at`、`delivered_at`、`next_attempt_at`。投递失败后，会在 1 分钟、5 分钟、30 分钟、2 小时、6 小时和 12 小时后重试。

**参数**

| 名称 | 位置 | 类型 | 必填 | 说明 |
|---|---|---|---|---|
| `limit` | 查询参数 | `integer` | 否 | 返回的对象个数（1 到 100）。默认值为 `20`。 |
| `cursor` | 查询参数 | `string` | 否 | 上一页返回的 `next_cursor`。最多 200 个字符。 |

**示例**

```bash
curl https://api.imario.ai/v1/webhook-endpoints/ENDPOINT_ID/deliveries \
  -H "Authorization: Bearer $IMARIO_API_KEY"
```

**响应**

返回一个列表：`{"object": "list", "data": [...], "has_more": false, "next_cursor": null}`。参见[分页](https://imario.ai/zh/docs/api/pagination)。
