---
title: Webhook
description: >-
  Endpoint HTTPS per link.visit, link.download e link.play, consegnati tramite
  Svix.
sidebar:
  label: Webhook
  order: 8
---
Endpoint HTTPS. Solo HTTPS. Niente localhost o IP privati. Fino a 10 per account.

Eventi: `link.visit`, `link.download`, `link.play`. Il segreto è `whsec_…`, mostrato una sola volta alla creazione o alla rotazione. Conservalo. Non inserirlo mai nel corpo 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` e `get` restituiscono solo `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`.

## Payload

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

`data.variant` è la variante ricevuta dal visitatore (`main` per i link con una sola variante).

Header in ogni consegna (i tentativi di Svix riutilizzano lo stesso id):

- `svix-id`
- `svix-timestamp` (secondi unix)
- `svix-signature` (`v1,<base64 hmac>`)

Contenuto firmato: `{id}.{timestamp}.{raw body}` HMAC-SHA256. Formato del segreto `whsec_` + base64.

## Verifica

Le consegne sono inviate da [Svix](https://www.svix.com) e seguono la specifica [Standard Webhooks](https://www.standardwebhooks.com). Verifica con il pacchetto [`svix`](https://www.npmjs.com/package/svix) (come fanno Clerk e Resend).

:::note
Passa il corpo **grezzo** della richiesta a `verify`. Fare il parsing del JSON e poi di nuovo lo stringify farà fallire la firma.
:::

```bash
npm i svix
```

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

const wh = new Webhook(process.env.LINKDASH_WEBHOOK_SECRET); // whsec_… mostrato una sola volta

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