# Receive sandbox events by webhook

> Let your agent do it: Register my HTTPS receiver for Hermitbox sandbox lifecycle events. Ask me for the receiver's URL and a new file path, in an existing private directory, for its signing secret. Set `HERMITBOX_API_URL`, `HERMITBOX_API_KEY`, `HERMITBOX_WEBHOOK_URL` and `HERMITBOX_WEBHOOK_FILE` privately, install `hermitbox` in a Node program, and run the page's block once. Never print the signing secret or copy the key into this chat; tell me only that it registered. Follow the exact steps in https://docs.hermitbox.dev/v0.3.2/guides/webhooks.md.

Hermitbox can send a signed HTTPS request to your endpoint when a sandbox is created, wakes, goes
to sleep, fails or is deleted. Register your endpoint once, and save its signing secret to a
private file: Hermitbox shows the secret only at registration.

1. Have your HTTPS receiver ready, and pick a new file in an existing private directory for its
   signing secret.
2. Set `HERMITBOX_API_URL`, `HERMITBOX_API_KEY`, `HERMITBOX_WEBHOOK_URL` and `HERMITBOX_WEBHOOK_FILE`
   privately, and install `hermitbox` in the Node program that registers the receiver.
3. Run this once.

```ts
import { writeFileSync } from 'node:fs'
import { Hermitbox } from 'hermitbox'

// The receiver and the secret's file come from the environment.
const receiver = process.env.HERMITBOX_WEBHOOK_URL
const file = process.env.HERMITBOX_WEBHOOK_FILE
if (!receiver || !file) {
  throw new Error('Set HERMITBOX_WEBHOOK_URL and HERMITBOX_WEBHOOK_FILE')
}

// Connect with HERMITBOX_API_URL and HERMITBOX_API_KEY from the environment.
const hb = new Hermitbox()

// Register the receiver for sandbox creation and deletion.
const hook = await hb.webhook({ url: receiver, kinds: ['created', 'deleted'] })

try {
  // Save the signing secret once, to a new private file.
  const saved = JSON.stringify({ id: hook.id, secret: hook.secret }) + '\n'
  writeFileSync(file, saved, { mode: 0o600, flag: 'wx' })
} catch (error) {
  // Without the saved secret the registration is useless, so remove it.
  await hook.delete()
  throw error
}

console.log('Webhook registered')
```

The block never prints the secret, and it won't overwrite an existing file. If it can't save the
secret, it removes the registration.

From a terminal, `hermitbox webhook create <url> --kinds created,deleted` registers the same way
and prints the secret once; `webhook ls`, `webhook rm` and `webhook deadletters` manage
registrations ([CLI](https://docs.hermitbox.dev/v0.3.2/reference/cli.md)).

## What your receiver gets

- **Kinds:** `created`, `running`, `sleeping`, `woken`, `resting`, `deleted`, `failed`,
  `ttl_expired` and `idle_slept`. Register only the ones you need.
- **Payload:** `{id, event, sandboxId, at}`. It never carries the sandbox's environment, metadata
  or secret values.
- **Signature:** verify `hermitbox-signature` (`v1=` and a lowercase hex HMAC-SHA256 with the saved
  secret) over `hermitbox-timestamp`, a dot and the exact received bytes. Compare in constant time,
  reject stale timestamps and deduplicate `hermitbox-id` across retries.
- **Deadline and retries:** each attempt has ten seconds. Return 2xx once you have durably accepted
  the event; otherwise it is retried after one, two, four and eight minutes, five attempts in all,
  then kept as a dead letter (`hermitbox webhook deadletters <webhook-id>`).

[Lifecycle webhooks](https://docs.hermitbox.dev/v0.3.2/reference/api.md#lifecycle-webhooks) has the full receiver contract.
Events reflect the [lifecycle limits of the sandbox's tier](https://docs.hermitbox.dev/v0.3.2/reference/tiers.md).

## Private receivers

Hermitbox delivers only to public addresses unless the host's operator allows a private receiver.
A caller can't relax that allowlist.
