---
title: "Webhook API：签名与重试 | Narwhal API"
description: "Webhook API 参考文档介绍签名传送、端点管理和重试机制。页面详细说明 Webhook 端点的细节，以及传送失败后重试行为的具体规则，并列出请求参数和响应字段。"
url: "https://narwhalapi.com/zh/docs/api/webhooks"
---

# Webhook API 参考

带签名、至少一次的事件交付，支持历史记录与重放

## 目标地址

十个需要身份验证的路由可用于创建、列出、查看、更新、轮换、测试和删除目标地址，还可读取配置和交付历史，并重放单次交付。目标地址的筛选条件始终限于账户当前有效的国家和数据类别使用权。 所有账户均可使用 Webhook。每次投递按其所含数据的价格计一次调用费用。 一个账户总共最多可保留 25 个目标地址，无论是否启用。创建第 26 个的 `POST /v1/webhooks/destinations` 会返回 `409` `limit_reached`，直到删除一个目标地址。

`GET /v1/webhooks/destinations` `POST /v1/webhooks/destinations` `GET /v1/webhooks/destinations/{destination_id}` `PATCH /v1/webhooks/destinations/{destination_id}` `DELETE /v1/webhooks/destinations/{destination_id}` `POST /v1/webhooks/destinations/{destination_id}/test` `POST /v1/webhooks/destinations/{destination_id}/rotate-secret` `GET /v1/webhooks/destinations/{destination_id}/history` `GET /v1/webhooks/destinations/{destination_id}/deliveries` `POST /v1/webhooks/destinations/{destination_id}/deliveries/{delivery_id}/replay`

## 请求字段

| 字段 | 类型 | 规则 |
| --- | --- | --- |
| `url` | 字符串 | 必填。最长2,048个字符的HTTPS URL。 |
| `countries` | ISO 3166 alpha-3 代码数组 | 必填。1–60个大写ISO alpha-3代码，或`WLD`。 |
| `families` | 发布系列标识数组 | 必填。从“事件类型”中列出的类别里选 1 到 8 个。未列出的类别会被拒绝，错误中会列出可用的类别。 |
| `companies` | 公司 ID 数组 | 使用 `filings` 系列时必填，不使用时会被拒绝。1–1,000 个公司 ID，例如 `usa:0000320193`；未知的 ID 会被拒绝，错误中会指明该 ID。 |
| `enabled` | 布尔值 | 可选。默认值为true。 |

`PATCH` 会替换整个配置：每次都需发送 `url`、`countries`、`families`、`enabled`，使用 `filings` 系列时还需发送 `companies`。省略 `enabled` 会重新启用已停用的目标。 如果启用目标地址会超出上限，`POST` 和 `PATCH` 返回 `409 limit_reached`。请先停用或删除另一个目标地址。

## 目标端字段

| 字段 | 类型 | 含义 |
| --- | --- | --- |
| `destination_id` | uuid | 标识目标端。 |
| `url` | 字符串 | HTTPS 接收端 URL。 |
| `countries` | ISO 3166 alpha-3 代码数组 | 在账户当前有效的权限范围内筛选国家。 |
| `families` | 发布系列标识数组 | 按发布系列筛选。 |
| `enabled` | 布尔值 | 表示目标端是否已启用。 |
| `companies` | 公司 ID 数组 | 被关注的公司。仅当目标具有 `filings` 系列时发送。 |
| `config_version` | 整数 | 每次配置变更时递增；投递记录会记录发送时使用的版本。 |
| `secret_version` | 整数 | 每次轮换密钥时递增。 |
| `created_at` | UTC时间戳 | 目标端的创建时间。 |
| `updated_at` | UTC时间戳 | 目标端最后一次更新的时间。 |
| `secret` | 字符串 | 仅在创建和轮换密钥时返回；请妥善保存，因为之后不会再次显示。 |
| `disabled_reason` | 字符串 | 目标停用原因：`customer`或`plan_limit`。`plan_limit` 表示因目标数量上限而被停用。目标启用时省略。 |

DELETE 和重放返回 `204 No Content`。测试返回 `delivery_id`（没有内容进入队列时省略）和 `status`（`queued`、`rate_limited` 或 `disabled`）；受到速率限制时还会返回 `retry_after_seconds`。配置历史路由返回 `history`，其中每个版本对应一条记录，包含 `config_version`、`url`、`countries`、`families`、`enabled` 和 `recorded_at`。列表路由返回 `destinations` 和 `destination_limit`，后者是一个账户最多可保留的目标数量（25）。没有值的字段会被省略，例如已启用目标的 `disabled_reason`。 未知或已删除的目标在所有目标路由上均返回`404 webhook_request_failed`。重放不可用的投递也返回同一错误。投递列表的`cursor`无效时返回`400`。`503 webhook_unavailable`表示目前无法提供Webhook。 测试投递有账户级的10秒冷却时间。期间测试路由返回 `status: "rate_limited"` 和 `retry_after_seconds`。

## 投递记录

投递记录路由使用 `limit`（1～100，默认 50）和 `cursor` 分页，并返回 `next_cursor`（最后一页为 null）。

| 字段 | 类型 | 含义 |
| --- | --- | --- |
| `delivery_id` | 字符串 | 标识投递记录。 |
| `event_cursor` | 整数 | 标识事件；测试投递时省略。 |
| `is_test` | 布尔值 | 仅在测试投递时发送，值为 true。 |
| `config_version` | 整数 | 该次投递使用的目标端配置版本。 |
| `state` | 枚举 | 投递状态：`pending`、`leased`、`succeeded`、`failed` 或 `cancelled`。 |
| `attempts` | 整数 | 投递尝试次数。 |
| `replays` | 整数 | 重放次数；仅在大于 0 时发送。 |
| `next_attempt_at` | 时间戳 | 下次尝试的时间；仅在状态为 `pending` 或 `leased` 时发送。 |
| `retry_until` | 时间戳 | 允许重试投递的截止时间；仅在状态为 `pending` 或 `leased` 时发送。 |
| `last_status` | 整数 | 接收端最近一次返回的 HTTP 状态；首次尝试前省略。 |
| `last_error` | 字符串 | 最近一次错误；没有错误时省略。 |
| `created_at` | 时间戳 | 投递记录的创建时间。 |
| `updated_at` | 时间戳 | 投递记录最后一次更新的时间。 |

## 事件类型

目标仅在已订阅类别匹配，且符合账户当前有效的国家和类别权限时，收到以下事件类型。

目标可订阅以下一个或多个类别：`gdp`、`prices`、`labour`、`trade`、`policy_rate`、`yield_curve`、`predictions` 和 `filings`。

| 事件类型 | 含义 |
| --- | --- |
| `release.updated` | 日历中的发布带有此前没有的数字 |
| `dataset.published` | 已接受的官方出版物新增参考期时 |
| `yield_curve.published` | 新的收益率曲线日期被接受时 |
| `rate_decision.published` | 央行公布利率决定时，每次会议一次 |
| `filing.published` | 当你关注的公司发布申报文件时，每份申报文件发送一次 |

目标只在新数字到达时收到投递。日期变更、已取消的发布和之后被更改的数字都不会发送。日期变更请从日历中查看。

## 预测事件

订阅 `predictions` 类别的目标在事件停止交易时收到 `prediction.event.closed`，在事件结算时收到 `prediction.event.settled`，在事件作废时收到 `prediction.event.voided`。每种类型对每个事件最多发送一次。正文只包含事件状态，不包含概率或获胜结果。市场ID为 `prediction_event_id`；`event_id` 标识事件。

## 载荷正文

Webhook正文包含`schema_version`，REST事件则不包含。无值的字段会被省略。`cursor`用于排列日志事件的顺序。

经济数据和发布事件

```
{
  "schema_version": "1",
  "event_id": "2cdf117d-4a23-4a1f-b68e-93a7e9dc765e",
  "cursor": "1",
  "type": "dataset.published",
  "country": "USA",
  "family": "prices",
  "resource_id": "775d11a3-a6de-52b1-b41b-a236ad595e7a",
  "resource_version": "822e107c-09f6-4736-82e9-082d725398a2",
  "operation": "economics_get_cpi",
  "data": {
    "country": "USA",
    "period": "2026-10",
    "value": "337.511",
    "index_base": "1982-84=100",
    "released_on": "2026-11-10"
  },
  "recorded_at": "2026-11-10T13:31:12Z"
}
```

`operation` 指明其最新值保存在 `data` 中的操作，例如 `economics_get_cpi`，`data` 是该国的最新数字，格式与该操作的最新值端点返回的一致。两者都在 `dataset.published` 和 `yield_curve.published` 上发送；当投递时关联的数据事件存在时，`release.updated` 也会发送。`data` 只读取一次，因此每次重试和重放都相同；如果无法读取则省略，届时请从端点获取该数字。

发布事件使用`release_id`而非`resource_id`。 `release.updated` 事件还带有 `release`，即含其 `indicators` 的日历修订。其中的每个时间戳都是 UTC。

发布更新

```
{
  "schema_version": "1",
  "event_id": "c3a8e4d0-5b1f-4f7e-9d2a-6e0b8f3a1c57",
  "cursor": "2",
  "type": "release.updated",
  "country": "USA",
  "family": "prices",
  "release_id": "f01e2289-2e17-49b3-9d64-d29118742dee",
  "resource_version": "4d6b0c1e-8a27-4f93-b5c0-71e2a9d3f648",
  "operation": "economics_get_cpi",
  "data": {
    "country": "USA",
    "period": "2026-10",
    "value": "337.511",
    "index_base": "1982-84=100",
    "released_on": "2026-11-10"
  },
  "release": {
    "title": "Consumer Price Index",
    "reference_period": "2026-10",
    "status": "published",
    "schedule": {
      "precision": "exact",
      "at": "2026-11-10T13:30:00Z",
      "timezone": "America/New_York",
      "local_date": "2026-11-10"
    },
    "officially_published_at": "2026-11-10T13:30:00Z",
    "indicators": [
      {
        "measure": "cpi_mom",
        "concept": "cpi",
        "title": "CPI, month over month",
        "period": "2026-10",
        "unit": "percent",
        "transformation": "month_over_month",
        "annualization": "not_annualized",
        "actual": "0.4",
        "previous": "0.6",
        "previous_period": "2026-09",
        "consensus": "0.3",
        "value_status": "available"
      }
    ]
  },
  "recorded_at": "2026-11-10T13:31:12Z"
}
```

`rate_decision.published` 事件带有 `decision`，其中每个字段都与利率决议端点返回的一致，每次会议只发送一次。`country` 是作出决议的国家，或作出决议的联盟的 ID（`EMU`、`WAEMU`、`CEMAC`、`ECCU`、`CBCS` 或 `IEOM`）；关注该联盟成员国的目标地址也会收到。之后发生变化的决议不会再发送。

利率决议

```
{
  "schema_version": "1",
  "event_id": "8b7f2d19-0c4e-4a63-a1d5-3f9e6c2b7a04",
  "cursor": "3",
  "type": "rate_decision.published",
  "country": "USA",
  "family": "policy_rate",
  "resource_id": "3e0f6a52-9d14-5b87-8c21-d4a7b9e1f063",
  "resource_version": "b1c94e7a-2f56-4d08-93ae-5c7d0e8a1b32",
  "recorded_at": "2026-10-28T18:05:41Z",
  "decision": {
    "decision_id": "e5149a43bf24f96d202a6a8a6700d12b",
    "authority_id": "federal_reserve",
    "authority": "Federal Reserve System",
    "country": "USA",
    "decided_on": "2026-10-28",
    "date_basis": "decision",
    "decision_basis": "committee",
    "evidence_kind": "statement",
    "source_tier": "own",
    "source_publisher": null,
    "source_published_on": null,
    "unchanged_since": null,
    "action": "hold",
    "any_rate_moved": false,
    "currency": null,
    "announced_on": null,
    "effective_on": null,
    "rates": [
      {
        "role": "main",
        "instrument_label": "target range for the federal funds rate",
        "rate": null,
        "lower_bound": "3.75",
        "upper_bound": "4.00",
        "rate_source": "stated"
      }
    ],
    "quotes": [
      {
        "text": "The Committee decided to maintain the target range for the federal funds rate at 3-3/4 to 4 percent",
        "url": "https://www.federalreserve.gov/newsevents/pressreleases/monetary20261028a.htm"
      }
    ],
    "votes_status": "unknown",
    "served_state": "confirmed",
    "from_union": null
  }
}
```

`filing.published` 事件带有 `filing` 而不是 `data`：`filing_id` 是该申报文件的受理编号，当申报文件没有 `fy`、`fp`、`period_end`、`title`、`url` 和 `company_name` 时会省略这些字段。每份申报文件只发送一次，条件是该文件所属国家在 `countries` 中，且其公司在 `companies` 中。如果你关注 `subject_id` 所指的持股对象公司，13D 或 13G 申报文件也会发送。

申报文件事件

```
{
  "schema_version": "1",
  "event_id": "6a1d9c37-4e80-4b25-8f3a-0d7c5e2b9a16",
  "cursor": "4",
  "type": "filing.published",
  "country": "USA",
  "recorded_at": "2026-11-03T13:06:02Z",
  "filing": {
    "filing_id": "0000320193-26-000026",
    "company_id": "usa:0000320193",
    "company_name": "Apple Inc.",
    "form": "10-K",
    "type": "annual",
    "fy": 2026,
    "fp": "FY",
    "period_end": "2026-09-26",
    "url": "https://www.sec.gov/Archives/edgar/data/320193/000032019326000026/0000320193-26-000026-index.htm",
    "filed_at": "2026-11-03T13:05:15Z"
  }
}
```

预测事件

```
{
  "schema_version": "1",
  "event_id": "5d0b6f0e-7a1c-4c53-9b5e-2f9c4b1d8a63",
  "cursor": "470",
  "type": "prediction.event.settled",
  "country": "USA",
  "recorded_at": "2026-10-28T20:15:02Z",
  "prediction": {
    "prediction_event_id": "usa-fed-2026-10",
    "topic_id": "usa-fed",
    "shape": "scalar",
    "status": "settled",
    "closes_at": "2026-10-28T17:59:00Z",
    "resolves_at": "2026-10-28T18:00:00Z",
    "settlements": [
      {
        "venue": "kalshi",
        "status": "settled"
      },
      {
        "venue": "polymarket",
        "status": "settled"
      }
    ],
    "release_id": "ae2a77cc-3b5b-4541-a060-3bc2ea1145d9"
  }
}
```

测试投递

```
{
  "schema_version": "1",
  "event_id": "9f1c2a54-3b7d-4e0a-8c46-1d5e7b9a0f32",
  "type": "webhook.test",
  "recorded_at": "2026-10-22T09:00:00Z"
}
```

测试投递没有`cursor`、`release`和`data`。

## 签名与重试

1. 解析任何JSON之前，先读取`x-narwhal-timestamp`（Unix秒）和原始请求体。
2. 将目标的`secret`作为UTF-8字节密钥，计算`timestamp + "." + raw_body`的HMAC-SHA256，结果使用小写十六进制。
3. 以恒定时间将结果与`x-narwhal-signature`中的每个`sha256=<hex>`值比较。任一值匹配即可接受投递。密钥轮换后的24小时内，请求头会包含第二个值，由旧密钥签名。
4. 拒绝与本地时钟相差过大的时间戳，例如早于五分钟前的时间戳，以防重放。

至少投递一次。请在持久化处理结果后才返回2xx；响应正文会被忽略。`event_id` 标识事件，是重试和重放时的去重键。`delivery_id` 标识投递尝试；重试保留此值，测试投递也将其用作 `event_id`。

429或5xx响应、超时或连接错误会以指数退避重试，尝试间隔最长一小时；429或503附带`Retry-After`时可能更长。重定向或任何其他状态会导致投递失败。 重试窗口为投递创建或最近一次重放后的24小时。

## 历史与重放

投递历史记录尝试状态和响应状态。重放会为同一不可变事件创建新的投递。密钥轮换有24小时的重叠窗口，接收方可验证任一签名。

此页面对你有帮助吗?
