> ## Documentation Index
> Fetch the complete documentation index at: https://docs.melius.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Generation webhooks

> Receive a signed notification when a generation finishes, fails, or is cancelled.

Receive generation status changes at your HTTPS endpoint, for node runs, [Cast](https://docs.melius.com/api/cast.md) videos and [Ditto](https://docs.melius.com/api/ditto.md) videos. Include a callback when starting a generation, or register one afterward, including when the generation has already finished.

| Generation | Start with `webhook` | Register afterward | Event type |
| - | - | - | - |
| Node run | `POST /nodes/{nodeId}/runs` | `POST /node-runs/{nodeRunId}/webhook` | `run.status_changed` |
| Cast video | `POST /cast/videos` | `POST /cast/videos/{generationId}/webhook` | `cast_video.status_changed` |
| Ditto video | `POST /ditto` | `POST /ditto/{generationId}/webhook` | `ditto.status_changed` |

Each path is under `/api/v1`. A `GET` on the same `/webhook` path reads its delivery status.

## Start a generation with a callback

Add `webhook` to `POST /api/v1/nodes/{nodeId}/runs`, alongside your generation options:

```json theme={null}
{
    "webhook": {
        "url": "https://your-app.example/webhooks/melius",
        "signingSecret": "<a random secret of 32–256 characters>"
    }
}
```

The callback is saved together with the run. Invalid destinations are rejected before generation starts. Requests made through an agent approval session must register the callback after the approved run starts.

`POST /cast/videos` and `POST /ditto` take the same `webhook` object. Resending a Cast video's `attemptId`, or a request with the same `Idempotency-Key`, keeps the webhook the first request registered.

## Register a callback afterward

Call the generation's `/webhook` path, such as `POST /api/v1/node-runs/{nodeRunId}/webhook`, with your usual bearer API key and team header:

```json theme={null}
{
    "url": "https://your-app.example/webhooks/melius",
    "signingSecret": "<a random secret of 32–256 characters>"
}
```

Generate the signing secret in your application with `crypto.randomBytes(32).toString("hex")`, and store it securely. Use the exact string as the HMAC key. Melius encrypts the destination and secret in storage.

An accepted registration returns HTTP `202`:

```json theme={null}
{
    "id": "<webhookId>",
    "status": "pending"
}
```

You need editor access to register a webhook and access to the generation: the run's canvas, the Cast video you started, or a Ditto video you can read. The destination must use HTTPS and resolve to public IP addresses. URLs containing credentials or fragments are rejected. Redirects are not followed.

One webhook can be registered per generation. Another registration returns `409` and keeps the original destination. If registration times out, read its status before trying again. Each new generation needs its own registration. Bulk-run IDs use their existing polling endpoint.

## Receive an event

Melius sends a JSON POST when a node run becomes `finished`, `failed`, or `cancelled`:

```json theme={null}
{
    "id": "<eventId>",
    "type": "run.status_changed",
    "sequence": 1,
    "createdAt": "2026-11-24T12:00:00.000Z",
    "data": {
        "nodeRunId": "<nodeRunId>",
        "teamId": "<teamId>",
        "nodeId": "<nodeId>",
        "nodeVersionId": "<nodeVersionId or null>",
        "status": "finished"
    }
}
```

`createdAt` records the run update represented by this event. Use `GET /api/v1/node-runs/{nodeRunId}` to retrieve output versions and error details. Preserve `nodeRunId` when reading outputs: a node may have started another generation since this event.

A Cast or Ditto video sends an event when it becomes `ready` or `failed`. Its `data` names the `generationId`:

```json theme={null}
{
    "id": "<eventId>",
    "type": "cast_video.status_changed",
    "sequence": 1,
    "createdAt": "2026-11-24T12:00:00.000Z",
    "data": {
        "generationId": "<generationId>",
        "teamId": "<teamId>",
        "status": "ready"
    }
}
```

A Ditto video's `type` is `ditto.status_changed`. Read `GET /api/v1/cast/videos/{generationId}` or `GET /api/v1/ditto/{generationId}` for the video's URL or failure message. A failed Ditto video can later finish once its finalization is retried, which sends a new `ready` event.

The event ID and body stay the same on retries. Deliveries may arrive more than once or out of order. Store processed event IDs and the highest `sequence` for each generation. Apply each event only once and ignore sequences lower than the highest you have processed. A failed run can later finish successfully; that recovery produces a new event with a higher sequence. Acknowledge with any `2xx` response after durably accepting the event; perform slow processing separately.

## Verify the signature

Each delivery includes:

* `x-melius-webhook-id`: the event ID.
* `x-melius-webhook-timestamp`: the delivery attempt's Unix timestamp in seconds.
* `x-melius-webhook-signature`: `v1=` followed by a hexadecimal HMAC-SHA256 signature.

The signed value is `eventId.timestamp.rawBody`, joined with literal periods. Verify the raw request bytes before parsing JSON. Check the timestamp to reject replayed requests, and compare signatures in constant time:

```javascript theme={null}
import { createHmac, timingSafeEqual } from "node:crypto";

function verifyWebhook(rawBody, headers, signingSecret) {
    const id = headers["x-melius-webhook-id"];
    const timestamp = headers["x-melius-webhook-timestamp"];
    const signature = headers["x-melius-webhook-signature"];
    if (
        typeof id !== "string" ||
        typeof timestamp !== "string" ||
        typeof signature !== "string" ||
        !/^v1=[0-9a-f]{64}$/.test(signature)
    ) {
        return false;
    }
    const seconds = Number(timestamp);
    if (
        !Number.isSafeInteger(seconds) ||
        Math.abs(Date.now() / 1000 - seconds) > 300
    )
        return false;

    const expected = createHmac("sha256", signingSecret)
        .update(`${id}.${timestamp}.`)
        .update(rawBody)
        .digest();
    const received = Buffer.from(signature.slice(3), "hex");
    return (
        received.length === expected.length &&
        timingSafeEqual(received, expected)
    );
}
```

Keep the receiver's clock synchronized. Every retry has a fresh timestamp and signature.

## Delivery timing and failures

Melius saves a notification in the same transaction as the status change it reports. Pending notifications are dispatched about once a second; worker load may delay delivery. A callback can wait for a long-running generation. Each notification has its own delivery deadline. Registration does not change generation charges.

Each delivery attempt has a ten-second request timeout. Network failures, HTTP `408`, `429`, and `5xx` responses retry with increasing delays, starting at ten seconds and capped at one hour. Delivery stops after 30 attempts or 24 hours, whichever comes first. Other non-`2xx` responses stop delivery immediately.

Read the generation's `/webhook` path with `GET`, such as `GET /api/v1/node-runs/{nodeRunId}/webhook`, to inspect the latest notification:

| Status | Meaning |
| - | - |
| `pending` | Waiting for the generation or retrying delivery. |
| `delivered` | Your endpoint acknowledged with `2xx`. |
| `failed` | The latest notification exhausted its delivery attempts or deadline. |

A `404` means the generation is inaccessible or the webhook was never registered. Registration and delivery status remain available while the generation exists, independently of worker history. Keep your own event IDs for deduplication. For a failed delivery, retrieve the generation through its existing GET endpoint.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.