# Webhooks

Call sendWebhook() from any callback. ViteHub stores the delivery, signs it, waits for the debounce, and retries.

Hosts: Node, Docker, Cloudflare, Vercel.

```ts [server/webhooks/t3.ts]
import { defineWebhook } from 'vite-hub/webhooks'

// One file is one webhook. The values are names of server variables.
export default defineWebhook({
  url: 'T3_WEBHOOK_URL',
  secret: 'T3_WEBHOOK_SECRET',
  debounce: '30 minutes',
})
```

```ts [server/comments.ts]
import { defineComments } from 'vite-hub/comments'
import { sendWebhook } from 'vite-hub/webhooks'

// 10 comments in 30 minutes give one delivery with the last body
export const comments = defineComments({
  onComment: async ({ thread, message }) => {
    const [error] = await sendWebhook('t3', { thread: thread.id, message: message.body }, { event: 'comments.comment' })
    if (error) console.error(error)
  },
})
```

```bash [.env]
T3_WEBHOOK_URL=https://example.com/hooks/vitehub
T3_WEBHOOK_SECRET=...
```

## What you get

| Part | What it does |
| --- | --- |
| Definition | `server/webhooks/<name>.ts` names the receiver URL and the secret by their server variables. The file name is the webhook name, and `sendWebhook()` types it. |
| Signature | Each delivery has an HMAC-SHA256 signature in the GitHub form (`x-hub-signature-256`) or the Stripe form (`stripe-signature`). |
| Storage | Each delivery is a row of the Default Database, so it survives a restart or a deploy. |
| Debounce | With `debounce`, sends in the window collapse into one delivery with the last body. |
| Retries | The Queue tries a failed delivery again with the same delivery id, 4 attempts in all. |
| Inspection | `vite-hub/webhooks/runtime` lists, flushes, cancels, and redelivers deliveries. |

## Webhooks or Queue

Does the job go to a URL outside your app? Then use Webhooks. Does your own code do the job? Then use [Queue](https://vitehub.dev/docs/queue). Webhooks uses Queue to send each delivery. To receive a webhook from Stripe or GitHub, use [`verifyWebhook()`](https://vitehub.dev/docs/env/webhooks).

## Next

- [Get started](https://vitehub.dev/docs/webhooks/get-started): send a signed webhook from a ViteHub app.
- [Comments to a T3 Code thread](https://vitehub.dev/docs/webhooks/t3-code-thread): start a T3 Code thread 30 minutes after the last comment.
- [Send events](https://vitehub.dev/docs/webhooks/send-events): Forms, Blob, Content, and Auth events.
- [Server API](https://vitehub.dev/docs/webhooks/server-api): `defineWebhook()`, `sendWebhook()`, the headers, and the delivery functions.

<!-- vitehub-docs:section-links -->
## In this section

- [Overview](https://vitehub.dev/raw/docs/webhooks.md) (this page)
- [Get started](https://vitehub.dev/raw/docs/webhooks/get-started.md)
- [Configure](https://vitehub.dev/raw/docs/webhooks/configure.md)
- [Server API](https://vitehub.dev/raw/docs/webhooks/server-api.md)
- [Hosts](https://vitehub.dev/raw/docs/webhooks/hosts.md)
- [Limits and errors](https://vitehub.dev/raw/docs/webhooks/limits-and-errors.md)
- [Comments to T3 Code](https://vitehub.dev/raw/docs/webhooks/t3-code-thread.md)
- [Send events](https://vitehub.dev/raw/docs/webhooks/send-events.md)

Next: [WEBHOOKS_BODY_INVALID](https://vitehub.dev/raw/docs/webhooks/diagnostics/webhooks_body_invalid.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
