# Filter webhook deliveries

A filter decides which deliveries start a run. The others show in its events as **Ignored**, with no run.

## Before you start

You need a webhook, or the agent to make one for. See [Receive webhooks](/docs/orchestration/webhooks.md).

## Run only on matching deliveries

**Studio**

1. In your project, open **Orchestration** > **Triggers**.
2. Open **Webhooks**.
3. Click **Create webhook**.
4. Enter `Refund requests` as the **Name**.
5. Under **Triggers when**, pick **All conditions are met**.
6. Click **Add condition**.
7. Enter `action.type` as the **key**.
8. Keep the operator as **equals**.
9. Enter `refund.requested` as the **value**.
10. Enter the **Message**. Your agent is already the **Target**:

```text
A customer asks for a refund on order {{payload.order_id}}.
```

11. Click **Create webhook**. Studio shows the secret once, in a curl.
12. Send a matching delivery:

```bash
export WEBHOOK_URL=<the URL in the curl>
export WEBHOOK_SECRET=<the secret>
curl -X POST "$WEBHOOK_URL" \
  -H "X-Veryfront-Webhook-Secret: $WEBHOOK_SECRET" \
  -H "Idempotency-Key: refund-1042" \
  -H "Content-Type: application/json" \
  -d '{"action": {"type": "refund.requested"}, "order_id": "1042"}'
```

13. Send one that doesn't:

```bash
curl -X POST "$WEBHOOK_URL" \
  -H "X-Veryfront-Webhook-Secret: $WEBHOOK_SECRET" \
  -H "Idempotency-Key: shipped-1043" \
  -H "Content-Type: application/json" \
  -d '{"action": {"type": "order.shipped"}, "order_id": "1043"}'
```

14. Click **Done**.
15. Open the webhook's **Events** tab. You should now see the refund delivery **Completed** and the shipped one **Ignored**.

**Code**

1. Add the webhook, with your agent's ID as `target.id`:

```ts title="webhooks/refund-requests.ts"
import { webhook } from "veryfront/webhook";

export default webhook({
  id: "refund-requests",
  name: "Refund requests",
  target: { kind: "agent", id: "assistant" },
  agentMessage: {
    promptTemplate: "A customer asks for a refund on order {{payload.order_id}}.",
  },
  eventFilter: {
    mode: "all",
    conditions: [{ path: "action.type", operator: "equals", value: "refund.requested" }],
  },
});
```

2. Save a matching delivery:

```json title="refund.json"
{"action": {"type": "refund.requested"}, "order_id": "1042"}
```

3. Save one that doesn't:

```json title="shipped.json"
{"action": {"type": "order.shipped"}, "order_id": "1043"}
```

4. Run the matching one. You should now see `ran agent "assistant"`:

```bash
npx veryfront webhook run refund-requests --payload refund.json
```

5. Run the other. You should now see `was ignored: Webhook event did not match configured filter`:

```bash
npx veryfront webhook run refund-requests --payload shipped.json
```

### Try it in a sandbox

```playground
step: Create a project | npm create -y veryfront@latest support-desk -- --template ai-agent --yes
step: Add the webhook | cd support-desk && mkdir -p webhooks && printf '%s\n' 'import { webhook } from "veryfront/webhook";' '' 'export default webhook({' '  id: "refund-requests",' '  name: "Refund requests",' '  target: { kind: "agent", id: "assistant" },' '  agentMessage: {' '    promptTemplate: "A customer asks for a refund on order {{payload.order_id}}.",' '  },' '  eventFilter: {' '    mode: "all",' '    conditions: [{ path: "action.type", operator: "equals", value: "refund.requested" }],' '  },' '});' > webhooks/refund-requests.ts
step: Save a delivery that matches | printf '%s\n' '{"action": {"type": "refund.requested"}, "order_id": "1042"}' > refund.json
step: Save one that doesn't | printf '%s\n' '{"action": {"type": "order.shipped"}, "order_id": "1043"}' > shipped.json
step: Run it with the delivery that matches | npx veryfront webhook run refund-requests --payload refund.json
check: npx veryfront webhook run refund-requests --payload shipped.json
expect: was ignored: Webhook event did not match configured filter
```

**API**

1. Set `PROJECT` to your project's ID ([Find your IDs](/docs/authentication/api-keys.md#find-your-ids)): `export PROJECT=<id>`.
2. Set `AGENT_ID` to your agent's ID: `export AGENT_ID=<id>`.
3. Create the webhook and its filter with your [API key](/docs/authentication/api-keys.md). `mode` is `all` or `any`:

```bash
curl -X POST https://api.veryfront.com/projects/$PROJECT/webhooks \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Refund requests",
    "target": { "kind": "agent", "id": "'$AGENT_ID'", "conversation_mode": "none" },
    "prompt_template": "A customer asks for a refund on order {{payload.order_id}}.",
    "event_filter": {
      "mode": "all",
      "conditions": [{ "path": "action.type", "operator": "equals", "value": "refund.requested" }]
    }
  }'
```

4. Set these from the response. `webhook_secret` is shown only once:

```bash
export WEBHOOK_ID=<webhook.id>
export INGRESS_ID=<webhook.ingress_id>
export WEBHOOK_SECRET=<webhook_secret>
```

5. Send a matching delivery:

```bash
curl -X POST https://api.veryfront.com/webhooks/$INGRESS_ID \
  -H "X-Veryfront-Webhook-Secret: $WEBHOOK_SECRET" \
  -H "Idempotency-Key: refund-1042" \
  -H "Content-Type: application/json" \
  -d '{"action": {"type": "refund.requested"}, "order_id": "1042"}'
```

6. Send one that doesn't:

```bash
curl -X POST https://api.veryfront.com/webhooks/$INGRESS_ID \
  -H "X-Veryfront-Webhook-Secret: $WEBHOOK_SECRET" \
  -H "Idempotency-Key: shipped-1043" \
  -H "Content-Type: application/json" \
  -d '{"action": {"type": "order.shipped"}, "order_id": "1043"}'
```

7. List the webhook's events. You should now see one `completed`, with a `run_id`, and one `ignored`, with `error_code` `webhook_event_filtered`:

```bash
curl https://api.veryfront.com/projects/$PROJECT/webhooks/$WEBHOOK_ID/events \
  -H "Authorization: Bearer $VERYFRONT_API_KEY"
```

### Try it in a sandbox

```playground
step: Add the support agent | curl -fsS -X POST https://api.veryfront.com/projects/$VERYFRONT_PROJECT_ID/agents -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" -d '{"id": "support", "name": "Support assistant", "system_prompt": "Answer customer questions about orders and refunds. Refunds take 5 business days. Keep replies to two sentences."}'
step: Set PROJECT | export PROJECT=$VERYFRONT_PROJECT_ID
step: Set AGENT_ID | export AGENT_ID=support
step: Create the webhook with its filter | curl -X POST https://api.veryfront.com/projects/$PROJECT/webhooks -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" -d '{ "name": "Refund requests", "target": { "kind": "agent", "id": "'$AGENT_ID'", "conversation_mode": "none" }, "prompt_template": "A customer asks for a refund on order {{payload.order_id}}.", "event_filter": { "mode": "all", "conditions": [{ "path": "action.type", "operator": "equals", "value": "refund.requested" }] } }' | tee webhook.json
step: Set WEBHOOK_ID, INGRESS_ID and WEBHOOK_SECRET | export WEBHOOK_ID=$(jq -r .webhook.id webhook.json) INGRESS_ID=$(jq -r .webhook.ingress_id webhook.json) WEBHOOK_SECRET=$(jq -r .webhook_secret webhook.json)
step: Send a delivery that matches | curl -X POST https://api.veryfront.com/webhooks/$INGRESS_ID -H "X-Veryfront-Webhook-Secret: $WEBHOOK_SECRET" -H "Idempotency-Key: refund-1042" -H "Content-Type: application/json" -d '{"action": {"type": "refund.requested"}, "order_id": "1042"}'
step: Send one that doesn't | curl -X POST https://api.veryfront.com/webhooks/$INGRESS_ID -H "X-Veryfront-Webhook-Secret: $WEBHOOK_SECRET" -H "Idempotency-Key: shipped-1043" -H "Content-Type: application/json" -d '{"action": {"type": "order.shipped"}, "order_id": "1043"}'
step: Wait until one is completed and one ignored | until [ "$(curl -s https://api.veryfront.com/projects/$PROJECT/webhooks/$WEBHOOK_ID/events -H "Authorization: Bearer $VERYFRONT_API_KEY" | jq -r '[.data[].status] | sort | join(",")')" = "completed,ignored" ]; do sleep 3; done
check: curl https://api.veryfront.com/projects/$PROJECT/webhooks/$WEBHOOK_ID/events -H "Authorization: Bearer $VERYFRONT_API_KEY"
expect: "error_code":"webhook_event_filtered"
```

## With MCP

`my-project` stands for your project's slug or ID. Take the agent's `id` from `veryfront__list_agents`.

1. Call `veryfront__create_webhook`. The filter goes in `event_filter`:

```json
{
  "project_reference": "my-project",
  "name": "Refund requests",
  "target": { "kind": "agent", "id": "support", "conversation_mode": "none" },
  "prompt_template": "A customer asks for a refund on order {{payload.order_id}}.",
  "event_filter": {
    "mode": "all",
    "conditions": [{ "path": "action.type", "operator": "equals", "value": "refund.requested" }]
  }
}
```

2. It worked when the answer's webhook holds that `event_filter`. The answer's `webhook_secret` is shown only once: keep it to send deliveries, as the API tab does.

## Next steps

* [Check webhook deliveries](/docs/orchestration/webhook-events.md)
* [Find a run that failed](/docs/observability/runs.md)
