# Add a webhook

URL: https://lyeve.com/docs/how-to/add-a-webhook/

> **Included free** on every install, up to 25 webhooks per tenant. Step 6 sets retry settings and resends a delivery, which need a license with the `webhook-pro` feature, and step 7 needs `events`. See [pricing](https://lyeve.com/pricing/).

This walkthrough connects LyEve to an endpoint of your own. You run a small
receiver that refuses any request LyEve did not sign, register it as a
webhook, watch a test delivery and a real content event arrive, then read the
history and recover what failed. Every field, header and route is described on
[Webhooks](https://lyeve.com/docs/features/webhooks/). This page is the path through them.

## Before you start

- An account with the `super_admin` role, such as the first administrator.
  For the console steps, sign in to LyEve Console with it. For the API
  steps, keep its token in `TOKEN`.
  [Sign in](https://lyeve.com/docs/getting-started/quickstart/#3-sign-in) shows how to get one.
- A content type to watch. The steps use `post` from
  [Create your first content type](https://lyeve.com/docs/getting-started/first-content-type/).
- Node.js 18 or later, or Python 3.8 or later. Neither receiver needs a
  package.
- A place the instance can reach your receiver. The URL's host must resolve to
  a public address, or to a private range the operator lists in
  `WEBHOOK_ALLOWED_PRIVATE_NETWORKS`.

## 1. Run a receiver

Pick a long random secret and keep it in `WEBHOOK_SECRET`. LyEve signs every
delivery with it, and the receiver recomputes the signature over the raw body,
the timestamp and the nonce. It also refuses a timestamp older than five
minutes and a nonce it has already seen, so a captured request cannot be
replayed.

Node.js, saved as `receiver.mjs`:

```javascript
import http from "node:http";
import crypto from "node:crypto";

const SECRET = process.env.WEBHOOK_SECRET;
const PORT = Number(process.env.PORT ?? 8787);
const MAX_AGE_SECONDS = 300;
const seenNonces = new Map(); // nonce -> time it may be forgotten

function verify(headers, rawBody) {
  const timestamp = headers["x-webhook-timestamp"];
  const nonce = headers["x-webhook-nonce"];
  const signature = headers["x-webhook-signature"];
  if (!timestamp || !nonce || !signature) return "unsigned request";

  const now = Date.now() / 1000;
  if (Math.abs(now - Number(timestamp)) > MAX_AGE_SECONDS) return "stale timestamp";

  for (const [seen, forgetAt] of seenNonces) if (forgetAt < now) seenNonces.delete(seen);
  if (seenNonces.has(nonce)) return "nonce already used";

  const expected = "sha256=" + crypto
    .createHmac("sha256", SECRET)
    .update(`${timestamp}.${nonce}.`)
    .update(rawBody)
    .digest("hex");
  const a = Buffer.from(signature);
  const b = Buffer.from(expected);
  if (a.length !== b.length || !crypto.timingSafeEqual(a, b)) return "bad signature";

  seenNonces.set(nonce, now + 2 * MAX_AGE_SECONDS);
  return null;
}

http.createServer((req, res) => {
  const chunks = [];
  req.on("data", (chunk) => chunks.push(chunk));
  req.on("end", () => {
    const rawBody = Buffer.concat(chunks);
    const problem = verify(req.headers, rawBody);
    if (problem) {
      console.log("refused:", problem);
      res.writeHead(401).end(problem);
      return;
    }
    const delivery = JSON.parse(rawBody);
    console.log("accepted:", delivery.event, delivery.schema, JSON.stringify(delivery.data));
    res.writeHead(204).end();
  });
}).listen(PORT, () => console.log(`listening on http://localhost:${PORT}`));
```

Python, saved as `receiver.py`:

```python
import hashlib
import hmac
import json
import os
import time
from http.server import BaseHTTPRequestHandler, HTTPServer

SECRET = os.environ["WEBHOOK_SECRET"].encode()
PORT = int(os.environ.get("PORT", "8787"))
MAX_AGE_SECONDS = 300
seen_nonces = {}  # nonce -> time it may be forgotten


def verify(headers, raw_body):
    timestamp = headers.get("X-Webhook-Timestamp")
    nonce = headers.get("X-Webhook-Nonce")
    signature = headers.get("X-Webhook-Signature")
    if not (timestamp and nonce and signature):
        return "unsigned request"

    now = time.time()
    if abs(now - int(timestamp)) > MAX_AGE_SECONDS:
        return "stale timestamp"

    for seen in [n for n, forget_at in seen_nonces.items() if forget_at < now]:
        del seen_nonces[seen]
    if nonce in seen_nonces:
        return "nonce already used"

    signed = f"{timestamp}.{nonce}.".encode() + raw_body
    expected = "sha256=" + hmac.new(SECRET, signed, hashlib.sha256).hexdigest()
    if not hmac.compare_digest(expected, signature):
        return "bad signature"

    seen_nonces[nonce] = now + 2 * MAX_AGE_SECONDS
    return None


class Receiver(BaseHTTPRequestHandler):
    def do_POST(self):
        raw_body = self.rfile.read(int(self.headers.get("Content-Length", 0)))
        problem = verify(self.headers, raw_body)
        if problem:
            print("refused:", problem, flush=True)
            self.send_response(401)
            self.end_headers()
            self.wfile.write(problem.encode())
            return
        delivery = json.loads(raw_body)
        data = json.dumps(delivery["data"], separators=(",", ":"), ensure_ascii=False)
        print("accepted:", delivery["event"], delivery["schema"], data, flush=True)
        self.send_response(204)
        self.end_headers()

    def log_message(self, *args):
        pass


print(f"listening on http://localhost:{PORT}", flush=True)
HTTPServer(("", PORT), Receiver).serve_forever()
```

Start one of them:

```bash
export WEBHOOK_SECRET='replace-with-a-long-random-secret'
node receiver.mjs        # or: python3 receiver.py
```

```text
listening on http://localhost:8787
```

The receiver keeps this terminal. Run the API steps below in a second one, and
export `WEBHOOK_SECRET` with the same value there, and `TOKEN` too. In a
terminal where `WEBHOOK_SECRET` is unset, step 2 registers the webhook with no
secret, and the receiver prints `refused: unsigned request` for every delivery.

Both print the fields of the standard body. If you give the webhook a
`payload_template`, the body is your own JSON, so change the line that prints
it. Both check the raw bytes. If you move the check into a framework, read the body
before any JSON parser touches it (in Express, `express.raw({ type: "application/json" })`),
because a re-serialized body no longer matches the signature. Keep the nonce
list in a shared store such as Redis when you run more than one receiver.

## 2. Register the receiver

Use the address the instance reaches your receiver at, and the same secret.

<div class="paths">
<div data-path="console">

1. Open **Delivery > Webhooks** and choose **New webhook**.
2. Enter `My receiver` in **Name** and the receiver's address in **URL**.
   Enter the value of `WEBHOOK_SECRET` in **Signing secret**.
3. Under **Events**, tick `after_create`, `after_update` and `after_delete`.
4. In **Content types**, which reads **Every content type**, pick `post`.
5. Choose **Create**.

</div>
<div data-path="api">

```bash
curl -X POST http://localhost:3001/api/admin/webhooks \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
        "name": "My receiver",
        "url": "https://hooks.example.com/lyeve",
        "events": ["after_create", "after_update", "after_delete"],
        "schemas": ["post"],
        "secret": "'"$WEBHOOK_SECRET"'"
      }'
```

The answer is `201` with the webhook. Copy its `id` into `WEBHOOK_ID`. A URL
whose host does not resolve, or resolves to a private address, answers
`400 invalid input`.

</div>
</div>

## 3. Send a test

<div class="paths">
<div data-path="console">

Choose **Test**, the flask icon, on the webhook's row. The outcome shows under
the row: `HTTP 204: No Content`.

</div>
<div data-path="api">

```bash
curl -X POST http://localhost:3001/api/admin/webhooks/$WEBHOOK_ID/test \
  -H "Authorization: Bearer $TOKEN"
```

The API answers `{"success": true, "status_code": 204, "message": "No Content"}`.

</div>
</div>

The receiver prints:

```text
accepted: test * {"message":"webhook test from LyEve"}
```

If the receiver prints `refused: bad signature`, the two secrets differ.

## 4. Fire a real event

<div class="paths">
<div data-path="console">

Open **Content**, pick **Post** and choose **New entry**. Enter
`Webhook check` in **Title** and `Hello` in **Body**, then choose **Create**.

Within a moment the receiver prints an `after_create` line. An entry saved in
the console arrives in the same shape as one saved with the API: its `id`,
each field under its own name and its status under `_status`. A field you left
empty arrives as `null`:

```text
accepted: after_create post {"_status":"draft","author_id":null,"body":"<p>Hello</p>","id":"8371ea04-24e5-49a8-93b0-0bb8b7d4baff","slug":null,"title":"Webhook check"}
```

</div>
<div data-path="api">

Create a `post` on the Content API:

```bash
curl -X POST http://localhost:3002/api/v1/content/post \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"data": {"title": "Webhook check", "slug": "webhook-check", "body": "Hello"}}'
```

Within a moment the receiver prints an `after_create` line with the entry's
fields in alphabetical order, its `id` and status among them:

```text
accepted: after_create post {"_status":"published","body":"Hello","id":"4af50561-8093-4925-983f-ed8813ab615b","slug":"webhook-check","title":"Webhook check"}
```

</div>
</div>

## 5. Read the history

<div class="paths">
<div data-path="console">

Choose **Delivery history**, the clock icon, on the webhook's row. The dialog
lists the test and the `after_create` delivery, newest first, each with its
time, event, content type, status, duration and result.

</div>
<div data-path="api">

```bash
curl "http://localhost:3001/api/admin/webhooks/$WEBHOOK_ID/deliveries?limit=5" \
  -H "Authorization: Bearer $TOKEN"
```

The answer lists the latest deliveries, newest first:

```json
[
  {
    "id": "c1b593c6-8e40-4a97-9851-d545eae5c27e",
    "webhook_id": "df373000-a2df-4fcb-b7c1-949494a57337",
    "tenant_id": "default",
    "event_type": "after_create",
    "schema_name": "post",
    "status_code": 204,
    "success": true,
    "duration_ms": 32,
    "request_body": "{\"event\":\"after_create\",\"schema\":\"post\",\"data\":{\"_status\":\"published\",\"body\":\"Hello\",\"id\":\"4af50561-8093-4925-983f-ed8813ab615b\",\"slug\":\"webhook-check\",\"title\":\"Webhook check\"},\"timestamp\":\"2026-10-03T14:29:09Z\"}",
    "original_payload": "{\"data\": {\"id\": \"4af50561-8093-4925-983f-ed8813ab615b\", \"body\": \"Hello\", \"slug\": \"webhook-check\", \"title\": \"Webhook check\", \"_status\": \"published\"}, \"event\": \"after_create\", \"schema\": \"post\", \"timestamp\": \"2026-10-03T14:29:09Z\"}",
    "attempted_at": "2026-10-03T14:29:09.529085Z",
    "retry_count": 0
  }
]
```

A failed delivery carries `success: false`, the `status_code` or an `error`,
and `next_retry_at`.

</div>
</div>

## 6. Recover a failed delivery

Without retry settings of its own, a webhook retries a failed delivery after
30, 60, 120 and 240 seconds, as [Retry policy](https://lyeve.com/docs/features/webhooks/#retry-policy)
explains. For shorter waits while you test, give the webhook its own. Those
settings, the manual retry and the replay below need `webhook-pro`.

<div class="paths">
<div data-path="console">

1. Choose **Edit** on the webhook's row. Under **Delivery options**, enter `2`
   in **Retries** and `5` in **Retry delay (seconds)**, then choose
   **Save**. A failed delivery now gets two retries after the first attempt.
2. Stop the receiver and create another `post`. The delivery fails, and its
   two retries follow at the worker's 30-second checks. About a minute and a
   half after the failure, the delivery moves to the dead-letter queue.
3. Start the receiver again. Choose **Delivery history** on the webhook's row
   and choose **Retry this delivery**, the arrow on the failed delivery. Its
   result reads `OK`, and the receiver prints the delivery.
4. Choose **Dead letters**. The **Dead letter queue** lists the delivery with
   its attempts and the status `pending`. Choose **Replay** on it. The row
   reads **Delivered (HTTP 200)**.

</div>
<div data-path="api">

Set a retry policy. A policy wins over the console's **Retries** and
**Retry delay** once a webhook has one:

```bash
curl -X PUT http://localhost:3001/api/admin/webhooks/$WEBHOOK_ID/retry-config \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"max_attempts": 5, "base_delay_ms": 5000, "max_delay_ms": 20000, "strategy": "exponential"}'
```

Stop the receiver and create another `post`. The delivery fails, and with this
policy four retries follow. Each one goes at the first 30-second check after
its wait ends, and every wait here is shorter than that, so the first retry
goes about a minute after the failure and the rest about half a minute apart.
`GET /api/admin/webhooks/$WEBHOOK_ID/attempts` lists them.

Keep the receiver stopped until the delivery runs out of attempts, about three
minutes after the failure. It then waits in the dead-letter queue.
`GET /api/admin/webhook-dead-letters?status=pending` lists it with
`total_attempts` 5. Copy its `id` into `DEAD_LETTER_ID`. Then copy the failed
delivery's `id` from `GET /api/admin/webhooks/$WEBHOOK_ID/deliveries?limit=5`,
the one with `success: false`, into `DELIVERY_ID`.

Start the receiver again and send the delivery yourself:

```bash
curl -X POST http://localhost:3001/api/admin/webhooks/$WEBHOOK_ID/deliveries/$DELIVERY_ID/retry \
  -H "Authorization: Bearer $TOKEN"
```

```json
{"success": true, "status_code": 200, "message": "OK", "delivery_id": "..."}
```

Send the dead letter again too:

```bash
curl -X POST http://localhost:3001/api/admin/webhook-dead-letters/$DEAD_LETTER_ID/replay \
  -H "Authorization: Bearer $TOKEN"
```

```json
{"id": "...", "status": "replayed", "http_status": 200}
```

</div>
</div>

[Replay or dismiss a dead letter](https://lyeve.com/docs/features/webhooks/#replay-or-dismiss-a-dead-letter)
covers the queue in full.

## 7. Recover a missed window

When the receiver was down for longer than its retries lasted, send every
content event of the window again with [event replay](https://lyeve.com/docs/features/event-replay/).
This step needs a license with the `events` feature. Start with a dry run,
which counts what would be sent and sends nothing.

<div class="paths">
<div data-path="console">

1. Open **Insight > Events**. Under **Replay**, pick the day and enter the
   hour and minute the receiver went down in **Replay from**, and enter
   `my-receiver-catchup` in **Run name**.
2. Keep **Rehearse without publishing** ticked and choose **Rehearse**.
   The page says **Rehearsal started**, and the run's row
   shows how many events it counted, marked **rehearsal** and
   **(nothing was posted)**.
3. Clear **Rehearse without publishing**. The button now reads
   **Replay**. Choose it. The page says **Replay started.**, and the row
   reads, for one event, **1 replayed, 0 skipped, 0 failed**. The receiver
   prints the event again.

</div>
<div data-path="api">

```bash
curl -i -X POST http://localhost:3001/api/admin/events/replay \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"since": "2026-10-01T09:00:00Z", "handler": "my-receiver-catchup", "dry_run": true}'
```

The answer is `202` with a `Location` header naming the run. Read the run until
`status` is `completed`, and check `total_events`:

```bash
curl http://localhost:3001/api/admin/events/replay/$RUN_ID \
  -H "Authorization: Bearer $TOKEN"
```

Send the same request without `dry_run` to replay for real. The run then
counts each event it sent in `replayed`.

</div>
</div>

Each event is published again, the webhook matches it as it matched the
original, and the receiver gets it with a fresh timestamp and nonce. A
rehearsal marks nothing as replayed. Running the same name for real again
skips the events it already replayed and counts them in `skipped`.

:::caution
A replay publishes the events to everything that listens, not only this
webhook: other webhooks, flows, a message broker and realtime clients see them
too. The receiver may get an event it already processed, so deduplicate on the
entry's `id` and the event.
:::

## Next

- [Webhooks](https://lyeve.com/docs/features/webhooks/): filters, payload templates, health and
  incoming webhooks.
- [Event replay](https://lyeve.com/docs/features/event-replay/): the event log and how replays
  run.
- [Flows](https://lyeve.com/docs/features/flows/): send your own events with `webhook.deliver`.
- [Rebuild a static site when content changes](https://lyeve.com/blog/rebuild-static-site-with-webhooks/):
  a guide on the blog that points a webhook at a receiver which starts one build on
  Cloudflare, Netlify or Vercel after each burst of edits.
