# Webhooks

> Standard Webhooks events from hoshime, how to verify them, the retry schedule, recovery, and Slack and Telegram messages.

hoshime sends an event when work finishes (a job, a crawl, a rank check or a screenshot) to your URL as a signed webhook, to Slack or to Telegram. Subscribe a URL with `POST /v1/webhooks/endpoints` to exact event types, a domain such as `crawl.*`, or `*`.

## Events

| Event | When |
|---|---|
| `job.completed`, `job.failed` | A queued job finished: `result` holds the answer of an HTTP job, `scriptResult` the exit code and output of a script job, `browserResult` the final address of a browser job |
| `crawl.completed`, `crawl.failed` | A crawl run finished |
| `crawl.page` | A crawl stored one page (one event per page, with `changed`) |
| `serp.completed`, `serp.failed` | A rank check finished |
| `screenshot.completed`, `screenshot.failed` | A screenshot finished |

Every `webhook` event has the envelope `{id, type, createdAt, clientRef, data}`. `clientRef` is the `X-Client-Ref` of the request that created the work. Slack and Telegram endpoints get a message instead, see [Slack and Telegram](#slack-and-telegram).

## Verify the signature

Requests to `webhook` endpoints follow [Standard Webhooks](https://www.standardwebhooks.com/): headers `webhook-id`, `webhook-timestamp` and `webhook-signature: v1,<base64 HMAC-SHA256>`, signed with the endpoint's `whsec_` secret. Use an official Standard Webhooks library with a timestamp tolerance:

```js
import { Webhook } from "standardwebhooks";

const wh = new Webhook(process.env.HOSHIME_WEBHOOK_SECRET); // whsec_…
const event = wh.verify(rawBody, {
  "webhook-id": req.headers["webhook-id"],
  "webhook-timestamp": req.headers["webhook-timestamp"],
  "webhook-signature": req.headers["webhook-signature"],
});
```

Try it without code with the [webhook signature verifier](/tools/webhook-signature-verifier).

## Delivery contract

- Delivery is **at least once**, with no ordering between events. Dedupe on `webhook-id`, which a retry or replay reuses.
- Answer 2xx only after you have stored the event.
- Any other answer is retried on the schedule 0, 10 s, 1 min, 5 min, 30 min, 2 h, 6 h, 12 h. A `410 Gone` stops the retries.
- An event can arrive before the response of the call that created its work. If you do not know the id yet, answer non-2xx and the retry delivers it again.

## Recover

- `POST /v1/webhooks/endpoints/{id}/recover` with `since` re-sends the endpoint's failed deliveries since that time.
- `POST /v1/webhooks/deliveries/{id}/replay` re-sends one delivery.
- `POST /v1/webhooks/endpoints/{id}/test` sends a test event and returns your endpoint's answer.

Jobs sign their outbound requests the same way once you create a signing secret with `POST /v1/jobs/signing-secret`, so one verifier handles both.

## Slack and Telegram

A `slack` or `telegram` endpoint gets a short message for each event instead of the JSON envelope. `type` defaults to `webhook` and cannot change after creation. For these endpoints, `POST /v1/webhooks/endpoints/{id}/test` posts a test message to the channel.

### Slack

`url` is the Slack incoming webhook URL, `https://hooks.slack.com/services/…`. Create it in Slack with an incoming webhook for the channel. Slack-compatible incoming webhooks work too, such as Mattermost's, or a Discord webhook URL ending in `/slack`.

Create the endpoint with `POST /v1/webhooks/endpoints`:

```json
{"name": "Ops alerts", "type": "slack", "url": "https://hooks.slack.com/services/T000/B000/XXXX", "eventTypes": ["job.failed", "crawl.failed"]}
```

### Telegram

`botToken` comes from @BotFather, and `chatId` is required. `threadId` is optional: the topic of a group with topics. `chatId` is the chat's numeric id (groups and channels look like `-1001234567890`) or the `@name` of a public channel. Add the bot to the group, or to the channel as an admin that can post.

Create the endpoint with `POST /v1/webhooks/endpoints`:

```json
{"name": "Ops group", "type": "telegram", "botToken": "123456:ABC-DEF…", "chatId": "-1001234567890", "eventTypes": ["job.failed", "crawl.failed", "serp.failed", "screenshot.failed"]}
```

### Credentials

The Slack URL and the bot token are credentials. hoshime stores them encrypted and returns only their last four characters as `secretHint`; `url` is empty for these types. To change one, `PATCH /v1/webhooks/endpoints/{id}` with the new `url` (Slack) or `botToken` (Telegram). A `chatId` in a PATCH also replaces `threadId`.

These endpoints are not signed. There is no signing secret: the create response has an empty `secret`, and `rotate-secret` answers 409.

### Events and message

Subscribe a chat endpoint to failures only, with `eventTypes` such as `["job.failed", "crawl.failed", "serp.failed", "screenshot.failed"]`, since a chat channel is for people. `crawl.page` sends one message per page and is rarely wanted in a chat.

The message has a title such as "Job failed", the main fields of the event (ids, status, error, HTTP status, rank, URL), the `clientRef` as "Reference", and for jobs, crawls and rank checks a link to the run in the dashboard. It does not include scraped content.

### Delivery

The [delivery contract](#delivery-contract) applies to chat endpoints with these differences:

- A 4xx answer from Slack or Telegram is final, except 408 and 429. It means the URL was revoked, the bot was removed or the chat does not exist. Fix the endpoint with a PATCH, then call `recover`.
- A 429 waits for Telegram's `retry_after` or Slack's `Retry-After`.
- Telegram allows a bot about one message a second per chat and 20 a minute per group. Slack allows about one a second per webhook. Many events at once are retried after the 429s, so keep chat endpoints to failures.