# Batching vendor webhook events

> Source: https://truto.one/docs/guides/webhooks/batching-vendor-webhooks/

By default, each batch of vendor webhook events Truto processes for an integrated account starts its own `unified_webhook:batch` workflow run. Set `webhook.batch` to group an account's events into fewer runs: one run per time window, or sooner once enough events wait.

Your `record:*` webhooks don't change. They still arrive as soon as each event is processed.

Use it when each run does real work, such as starting a sync job that writes a file. A burst of 50 Slack messages then starts one run instead of 50.

## The setting

`webhook.batch` takes two fields:

| Field | What it does |
|---|---|
| `window_ms` | Required. How long the oldest waiting event waits before its run starts, in milliseconds: from `1000` (1 second) to `600000` (10 minutes). |
| `max_events` | Optional. Start a run as soon as this many events wait, even before the window ends: from `1` to `1000`. The default is `1000`. |

A run starts when the window ends or when `max_events` events wait, whichever comes first. A run holds up to 512 KB of events. It can go over by one event, and the rest go in the next run.

Within a run, events keep the order Truto processed them in. A run never mixes integrated accounts.

## Where to set it

Set it at one of three levels. Each level overrides the one above it:

| Level | Field | Applies to |
|---|---|---|
| Integration (one you own) | `config.webhook.batch` | Every account of the integration, in every environment that installs it. |
| Installed integration | `override.webhook.batch` | Every account of the integration in that environment. |
| Integrated account | `integration_override.webhook.batch` | That account only. |

To turn off batching that a level above turned on, set `batch` to `null`.

Truto checks the value on the integration and on the installed integration, and answers `400` if it is out of range. It doesn't check an integrated account's value when you save it. There, a `window_ms` that is not a number turns batching off, and a value out of range is moved to the nearest end of the range.

A change reaches an account within about a minute.

## Turn it on for one environment

`PATCH /environment-integration/{id}` replaces the whole `override`. Get the current override first, add `webhook.batch` to it, and send the complete object back:

```bash
# The current override is the "override" field of the response
curl "https://api.truto.one/environment-integration/<id>" \
  -H "Authorization: Bearer <API token>"
```

Save that override to `override.json` and add `batch` under `webhook`, keeping every other key. For a 5-minute window with at most 250 events a run:

```json
{
  "webhook": {
    "batch": { "window_ms": 300000, "max_events": 250 }
  }
}
```

If the override already has a `webhook` object, add `batch` inside it. Then send the whole file back:

```bash
curl -X PATCH "https://api.truto.one/environment-integration/<id>" \
  -H "Authorization: Bearer <API token>" \
  -H "Content-Type: application/json" \
  -d "{\"override\": $(cat override.json)}"
```

To turn it off, send the override again with `"batch": null`. See [Customising integrations](/docs/guides/integrations/customising-integrations) for what else a `PATCH` of the override keeps and changes.

## What a run receives

A run's `payload` has the same fields with or without batching: `integrated_account_id`, `events` (the processed events, in order) and `batch_size` (the number of events). See [Workflows](/docs/guides/workflows/overview).

In JSONata, a path or `$filter` that matches one item returns that item, not a list of one. A run can hold a single event, so if a step needs a list, wrap the expression in `$append([], ...)`.

## Runs can arrive more than once

`unified_webhook:batch` runs are delivered at least once, with or without batching:

- If Truto can't start a run, for example during a cloud outage, it keeps the events and tries again later, waiting up to 5 minutes between tries.
- A run can be sent twice with the same events, for example when Truto restarts while it hands a run over.
- While a run waits to be tried again, newer events wait behind it. One run can then hold events from several deliveries.

Make your workflow safe to run twice for the same event. Skip events you have already handled, by an id each event carries: its `event_id` where present, or the vendor's own id in `raw_payload` (for Slack, `event_id`).

If Truto still can't start a run an hour after its events were due, it drops them. Their `record:*` webhooks were already sent.

## Next

See [Receiving vendor webhooks](/docs/guides/webhooks/inbound-vendor-webhooks) for the URLs vendors call, and [Workflows](/docs/guides/workflows/overview) for what a run can do.
