# Webhooks

Get a signed POST when an entry is published, unpublished or deleted, and use it to rebuild your site or clear a cache.

A webhook is an HTTPS address that Mojimoto sends a `POST` to when a project's published content changes: an entry is published, unpublished or deleted. Use one to rebuild a static site, clear a cache or tell another system. Webhooks are on every plan, Free included.

## Create a webhook

1. Open the project's **API keys** page and find **Webhooks**. You need to be an admin or owner.
2. Give the webhook a name and the URL to call, then choose **Add webhook**.
3. Copy the signing secret. It is shown once.

The URL has to use `https` and has to resolve to a public address. An address on `localhost` or a private network is refused, so use a tunnel to test against your own machine.

A webhook cannot be edited or paused. To change the URL or get a new secret, remove it and add another. The list shows when each webhook last fired and the status code it got back.

## The events that fire a webhook

Every webhook receives all three events.

| Event | Sent when |
| --- | --- |
| `document.published` | A locale of an entry is published: from the editor, by a scheduled publish, or through the Management API or MCP. |
| `document.unpublished` | A locale of an entry is unpublished. |
| `document.deleted` | An entry that had published content is deleted. |

Saving a draft, deleting an entry that was never published, uploading media and changing the content model send nothing. Publishing an entry together with its linked entries sends one `document.published` for each entry that was published.

## What is sent

A `POST` with a JSON body and these headers:

| Header | Value |
| --- | --- |
| `Content-Type` | `application/json` |
| `X-Mojimoto-Event` | The event, for example `document.published`. |
| `X-Mojimoto-Signature` | `sha256=` followed by the HMAC-SHA256 of the raw body, in hex, keyed with the webhook's signing secret. |

For `document.published` the body carries the entry, shown here indented:

```json
{
  "event": "document.published",
  "project": "acme",
  "document": {
    "id": 42,
    "uid": "hello-world",
    "type": "post",
    "lang": "en-gb",
    "status": "published",
    "published_at": "2026-11-26T09:14:02+00:00",
    "updated_at": "2026-11-26T09:14:02+00:00",
    "data": { "title": "Hello, world" }
  }
}
```

`project` is the project's slug. `document` is the entry as the [Delivery API](/docs/api/delivery) returns it: the version that was published, in the locale that was published, with linked entries one level deep.

The other two events are sent after the content has gone, so they only say which entry it was. They do not name the locale.

```json
{
  "event": "document.unpublished",
  "project": "acme",
  "document": { "id": 42, "type": "post", "uid": "hello-world" }
}
```

`uid` is left out for an entry that has none. A deleted entry no longer has a type or a `uid` to report:

```json
{
  "event": "document.deleted",
  "project": "acme",
  "document": { "id": 42 }
}
```

## Verify the signature

Anyone who learns your URL can post to it, so check the signature before acting. Compute the HMAC-SHA256 of the raw request body with your signing secret, put `sha256=` in front, and compare it to the `X-Mojimoto-Signature` header. Use the body exactly as it arrived. Parsing it and encoding it again changes the bytes.

```ts lib/verify.ts
import { createHmac, timingSafeEqual } from 'node:crypto'

export function isFromMojimoto(rawBody: string, signature: string | null, secret: string): boolean {
  if (!signature) return false

  const expected = 'sha256=' + createHmac('sha256', secret).update(rawBody).digest('hex')

  const received = Buffer.from(signature)
  const wanted = Buffer.from(expected)

  return received.length === wanted.length && timingSafeEqual(received, wanted)
}
```

The same check in PHP:

```php
$expected = 'sha256='.hash_hmac('sha256', $rawBody, $secret);

$isFromMojimoto = hash_equals($expected, $signatureHeader);
```

The `@mojimoto/client` package exports `verifyWebhookSignature`, which does this for you. See the [JavaScript client](/docs/frameworks/javascript).

## Retries and timeouts

Webhooks are sent from a background queue shortly after the event, one delivery per webhook, so a slow endpoint does not hold up the others.

| Rule | Value |
| --- | --- |
| Time your endpoint has to answer | 10 seconds |
| Counts as a failure | A `4xx` or `5xx` status, or no answer in time |
| Attempts | 3 in all |
| Wait before the second attempt | 10 seconds |
| Wait before the third attempt | 60 seconds |

After the third failure the delivery is dropped. There is no delivery log and no way to send it again, so answer quickly and do slow work afterwards. A retry repeats the same body, so make your handler safe to run twice.

## Rebuild a site on publish

Most hosts give you a build hook: a URL that starts a deploy when it receives a `POST`. Use that URL as the webhook URL and you are done. Every publish, unpublish and delete starts a build, and no code is needed. A build hook does not check the signature, so anyone with the URL can start a build.

To check the signature first, point the webhook at a route of your own that verifies the request and then calls the build hook. This one is a Next.js route handler, using the function above:

```ts app/api/mojimoto/route.ts
import { isFromMojimoto } from '@/lib/verify'

export async function POST(request: Request) {
  const rawBody = await request.text()
  const signature = request.headers.get('x-mojimoto-signature')

  if (!isFromMojimoto(rawBody, signature, process.env.MOJIMOTO_WEBHOOK_SECRET!)) {
    return new Response('Bad signature', { status: 401 })
  }

  await fetch(process.env.DEPLOY_HOOK_URL!, { method: 'POST' })

  return new Response('ok')
}
```

`MOJIMOTO_WEBHOOK_SECRET` and `DEPLOY_HOOK_URL` are environment variables you set yourself: the signing secret and your host's build hook.

> **Note:** Published Delivery API responses can be served from a cache for 60 seconds, and once more while the cache refreshes, so a build that starts the moment the webhook arrives may still read the previous version of an entry. The `document.published` body carries the new version. See [HTTP details](/docs/api/http-details).
