# Connect webhooks

> 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.2.1/guides/webhooks.md.

Register an HTTPS endpoint for sandbox lifecycle events and save its signing secret to a private file.

1. Prepare your HTTPS receiver and choose 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;
   install `hermitbox` in the Node program that registers the receiver.
3. Run this once. Keep the saved file private: the signing secret is returned only at registration.

```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 file is created without overwriting an existing path; the block never prints the secret.
Verify timestamp and HMAC on the exact received bytes, deduplicate delivery IDs and acknowledge
promptly. [Signing, retries and dead letters](https://docs.hermitbox.dev/v0.2.1/reference/api.md#lifecycle-webhooks) describe the receiver contract.

Private destinations need the host's allowlist; a caller cannot relax it. Events still reflect
the selected [tier's lifecycle limits](https://docs.hermitbox.dev/v0.2.1/reference/tiers.md).
