# Receive webhooks

A webhook is a URL. When another service calls it, Veryfront starts a [run](/docs/concepts/runs.md) of the agent, task or workflow you pick.

## Before you start

You need an agent. See [Create an agent](/docs/agents/create-agent.md).

## Create a webhook

**Studio**

1. In your project, open **Orchestration** > **Triggers**.

2. Open **Webhooks**.

3. Click **Create webhook**.

4. Enter `Refund questions` as the **Name**.

5. Keep **Type** as **Agent**.

6. Under **Target**, pick your agent.

7. Keep **Mode** as **No conversation**.

8. Enter the **Message**. Use `{{payload.field}}` for a value from the delivery:

   ```text
   A customer asks: {{payload.question}}
   ```

9. Click **Create webhook**. Studio shows the secret once, with a curl that uses it.

10. Copy the secret.

11. Copy the curl.

12. Send a delivery with your own payload, with `WEBHOOK_URL` set to the URL in the curl you copied and `WEBHOOK_SECRET` to the secret:

```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-question-1" \
  -H "Content-Type: application/json" \
  -d '{"question": "How long does a refund take?"}'
```

13. Open the webhook's **Events** tab. You should now see the delivery **Completed**, with a run.

**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 with your [API key](/docs/authentication/api-keys.md). `{{payload.question}}` takes a value from the delivery:

```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 questions",
    "target": { "kind": "agent", "id": "'$AGENT_ID'", "conversation_mode": "none" },
    "prompt_template": "A customer asks: {{payload.question}}"
  }'
```

4. Set three values from the response: `WEBHOOK_ID` to `webhook.id`, `INGRESS_ID` to `webhook.ingress_id`, and `WEBHOOK_SECRET` to `webhook_secret`, which is shown only once:

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

5. Send a delivery as the other service would, with the secret and no API key:

```bash
curl -X POST https://api.veryfront.com/webhooks/$INGRESS_ID \
  -H "X-Veryfront-Webhook-Secret: $WEBHOOK_SECRET" \
  -H "Idempotency-Key: refund-question-1" \
  -H "Content-Type: application/json" \
  -d '{"question": "How long does a refund take?"}'
```

6. List the webhook's events. You should now see the delivery's `status` `completed`, with a `run_id`:

```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 | curl -X POST https://api.veryfront.com/projects/$PROJECT/webhooks -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" -d '{ "name": "Refund questions", "target": { "kind": "agent", "id": "'$AGENT_ID'", "conversation_mode": "none" }, "prompt_template": "A customer asks: {{payload.question}}" }' | 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 | curl -X POST https://api.veryfront.com/webhooks/$INGRESS_ID -H "X-Veryfront-Webhook-Secret: $WEBHOOK_SECRET" -H "Idempotency-Key: refund-question-1" -H "Content-Type: application/json" -d '{"question": "How long does a refund take?"}'
step: Wait until it is completed | until [ "$(curl -s https://api.veryfront.com/projects/$PROJECT/webhooks/$WEBHOOK_ID/events -H "Authorization: Bearer $VERYFRONT_API_KEY" | jq -r '.data[0].status')" = completed ]; do sleep 3; done
check: curl https://api.veryfront.com/projects/$PROJECT/webhooks/$WEBHOOK_ID/events -H "Authorization: Bearer $VERYFRONT_API_KEY"
expect: "status":"completed"
```

## Troubleshooting

**If a delivery fails with "Service-account owner must have project write access to own a new conversation"**, the webhook starts a new conversation, which fails on every delivery. Set **Mode** to **No conversation** (`"conversation_mode": "none"`).

## With MCP

1. Call `veryfront__create_webhook`. The message goes in `prompt_template`:

```json
{
  "project_reference": "my-project",
  "name": "Refund questions",
  "target": { "kind": "agent", "id": "assistant", "conversation_mode": "none" },
  "prompt_template": "A customer asks: {{payload.question}}"
}
```

2. Call `veryfront__run_webhook` with the returned webhook `id` as `webhook_id`, `"project_reference": "my-project"` and `"payload": { "question": "How long does a refund take?" }`.
3. Call `veryfront__list_webhook_events` with the same `webhook_id` and `project_reference`. The delivery starts `queued` or `running`: call it again until its `status` is `completed`, a few seconds. It worked when it is `completed`, with a `run_id`.

## Next steps

* [Schedule a run](/docs/orchestration/schedules.md)
* [Debug a run](/docs/observability/debug.md)
