---
title: Webhooks
description: 用于 link.visit、link.download 和 link.play 的 HTTPS 端点，由 Svix 负责投递。
sidebar:
  label: Webhooks
  order: 8
---
仅支持 HTTPS 端点，不允许使用 localhost 或私有 IP。每个账户最多 10 个。

事件：`link.visit`、`link.download`、`link.play`。密钥格式为 `whsec_…`，仅在创建或轮换时显示一次，请妥善保存。切勿将其放入 JSON 请求体中。

## CLI

```bash
linkdash webhooks create --url https://example.com/hooks
linkdash webhooks list
linkdash webhooks get <id>
linkdash webhooks rotate <id>
linkdash webhooks delete <id>
```

`list` 和 `get` 只返回 `secretHint`（`whsec_…xxxx`）。

## HTTP

```bash
curl https://api.linkdash.dev/v1/webhooks \
  -X POST \
  -H "Authorization: Bearer ldash_..." \
  -H "Content-Type: application/json" \
  -d '{"url":"https://example.com/hooks"}'
```

`GET /webhooks`、`POST /webhooks`、`GET /webhooks/:id`、`DELETE /webhooks/:id`、`POST /webhooks/:id/rotate`。

## 负载

```json
{
  "type": "link.visit",
  "timestamp": "2026-08-17T16:00:00.000Z",
  "data": { "id": "deck", "kind": "file", "variant": "main", "country": "DE", "referrer": "…" }
}
```

`data.variant` 表示访问者实际获得的变体（单变体链接为 `main`）。

每次投递都会携带以下请求头（Svix 重试时沿用同一个 id）：

- `svix-id`
- `svix-timestamp`（Unix 时间戳，单位为秒）
- `svix-signature`（`v1,<base64 hmac>`）

签名内容：对 `{id}.{timestamp}.{raw body}` 进行 HMAC-SHA256 计算。密钥格式为 `whsec_` + base64。

## 验证

投递由 [Svix](https://www.svix.com) 发送，遵循 [Standard Webhooks](https://www.standardwebhooks.com) 规范。请使用 [`svix`](https://www.npmjs.com/package/svix) 包进行验证（与 Clerk 和 Resend 相同）。

:::note
请将**原始**请求体传给 `verify`。如果先解析 JSON 再重新序列化，签名验证将会失败。
:::

```bash
npm i svix
```

```ts
import { Webhook, WebhookVerificationError } from "svix";

const wh = new Webhook(process.env.LINKDASH_WEBHOOK_SECRET); // whsec_… shown once

try {
  const event = wh.verify(rawBody, {
    "svix-id": headers["svix-id"],
    "svix-timestamp": headers["svix-timestamp"],
    "svix-signature": headers["svix-signature"],
  });
  // event.type is "link.visit" | "link.download" | "link.play"
} catch (err) {
  if (err instanceof WebhookVerificationError) {
    // 400 — do not process
  }
  throw err;
}
```
