# Webhooks

> Hear on your server when templates change, renders finish, users arrive and sessions start, and check every delivery is from Muzekit.

Add an endpoint in the dashboard under **Webhooks**, per environment. Test endpoints may be `http://localhost`; Live ones are public https URLs. Each endpoint gets a signing secret (`whsec_…`), shown once and revealable by the owner and admins. An AI app your team connects can add an endpoint through [MCP](/docs/mcp), but never sees its secret: copy it from the dashboard.

<Screenshot src="/docs/1-webhooks.webp" original="/docs/raw/1-webhooks.png" alt="Webhooks in the dashboard: Test endpoints with their events, Send test and Secret, the Render on save formats and recent deliveries with their results and retries" />

## Events

| Event | When |
|---|---|
| `template.created` | A user or your server made a template (a user's first save of a design included); `data.name` is its name and `data.from_template` the template it was copied from (`{ id, external_id }`), or `null` |
| `template.updated` | A template was saved |
| `template.deleted` | A template was deleted |
| `user.created` | Someone opened your editor for the first time |
| `session.started` | Your page opened the editor |
| `render.succeeded` | A [render](/docs/api-reference/renders-create) finished; `data.outputs` holds the file URLs and `data.trigger` says what asked for it: `api`, `save` (render on save) or `download` (a video downloaded in the editor, or a file a user's AI app made) |
| `render.failed` | A render failed; `data.error` says why |

Pick events per endpoint, or none for every event (new ones included).

## Render on save

Turn on **Render on save** under **Webhooks** (per environment, Off by default) and pick a format (PNG, JPG, WEBP, PDF or MP4). Each time a template is saved, Muzekit renders it once your user stops editing for about ten seconds and sends `render.succeeded` with the files:

```json
{
  "type": "render.succeeded",
  "data": {
    "render": "rnd_Ud1esqJ1JNC",
    "template": "tpl_G2l2bmhk8Pq",
    "revision": 4,
    "trigger": "save",
    "format": "png",
    "outputs": ["https://storage.muzekit.com/r/d90/…/1.png"]
  }
}
```

Each render spends API credits (an image or PDF page 1, a video 1 a second). A render you ask for with [`POST /v1/renders`](/docs/api-reference/renders-create) sends the same event with `"trigger": "api"`.

## Payload

Every delivery is a `POST` with a JSON body naming what changed. Read the details from the API when you need them.

```json
{
  "id": "evt_8gK2…",
  "type": "template.updated",
  "created_at": "2026-10-17T03:14:15.061Z",
  "data": {
    "template": "tpl_Iwgf9RMo6Ml",
    "user": "usr_7PqX2mB0aZ1",
    "external_user_id": "user_42",
    "revision": 3
  }
}
```

`external_user_id` is the id your server gave the user in [the session](/docs/sessions), so you can act without a lookup.

## Check the signature

Every delivery carries `Muzekit-Signature: t=<unix seconds>,v1=<hex>`. `v1` is the HMAC-SHA256 of `` `${t}.${body}` `` with your endpoint's secret. Compare in constant time, and refuse anything older than five minutes.

```js
import { createHmac, timingSafeEqual } from "node:crypto";

export function verifyMuzekit(header, rawBody, secret) {
  const parts = Object.fromEntries(header.split(",").map((p) => p.split("=")));
  const expected = createHmac("sha256", secret).update(`${parts.t}.${rawBody}`).digest("hex");
  const ok = parts.v1?.length === expected.length && timingSafeEqual(Buffer.from(parts.v1), Buffer.from(expected));
  return ok && Math.abs(Date.now() / 1000 - Number(parts.t)) < 300;
}
```

Use the raw request body, before any JSON parsing. Answer with a 2xx within five seconds; the dashboard lists every delivery with its status, and **Send test event** checks your endpoint end to end.

## Retries

A delivery that doesn't get a 2xx is retried after 1 minute, 5 minutes, 30 minutes, 2, 6, 12 and 24 hours, then once more a day later: about three days in all. A retry carries the same event `id` with a fresh signature, so store the ids you've handled and skip repeats.

When a delivery's last retry fails, the owner and admins get an email naming the endpoint, the environment and its last answer, at most once a day per endpoint. The endpoint stays on: new events still go to it and retry the same way.
