# Events

`POST /api/server/v1/:ws/events` wakes the agent for one person: your backend
says what happened, or what to tell them, and when. "Task 12 was assigned to
you by Pedro", "the plans for project 430 are in Dropbox", "task 12 is due in
two days", "order #1042 shipped". When it is due, the agent reads it, looks up
the current state with its [tools](https://vatio.ai/docs/tools), and writes to the person on
WhatsApp, in its own words. Or it writes nothing, because the task was closed
or the date moved in the meantime; with `required: true` it always writes.

This is the one way your backend has the agent write to someone. To send your
own words or a template as written, with no agent, use
[Messages](https://vatio.ai/docs/api/messages).

It takes the same [secret key](https://vatio.ai/docs/api/messages#secret-keys) as
[Messages](https://vatio.ai/docs/api/messages), with the `messages` scope.

```bash
curl -X POST https://vatio.ai/api/server/v1/acme/events \
  -H "Authorization: Bearer $VATIO_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "to": "+56912345678",
    "event": "Task 12 (Plans for project 430) is due on 2026-11-25. Look it up by its id before writing.",
    "at": "2026-11-23T09:00:00-03:00",
    "key": "task-12-reminder"
  }'
```

News the person must hear, now, whatever the agent finds:

```bash
curl -X POST https://vatio.ai/api/server/v1/acme/events \
  -H "Authorization: Bearer $VATIO_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "to": "+56912345678",
    "event": "Order #1042 shipped today with Starken, tracking 99812. Tell them, and offer the tracking link.",
    "required": true,
    "key": "order-1042-shipped",
    "template": { "name": "order_shipped", "language": "es", "params": ["Ana", "#1042"] }
  }'
```

What this replaces in your backend: the daily job that looks for reminders
that are due, the column that remembers one was sent, the one that keeps the
send's error, and the wording. Your backend says what happened and when; Vatio
keeps the time, and the agent keeps the words.

## Events or messages

| | `POST /events` | [`POST /messages`](https://vatio.ai/docs/api/messages) |
|---|---|---|
| Who writes | The agent, in its own words, after checking with its tools | You: your `text` or a `template`, exactly as given |
| Whether it goes out | The agent decides; with `required: true`, always | Always |
| When | Now, or `at` | Now |
| Several at once for one person | One message for all that fall due together | One message each |
| Channels | WhatsApp | WhatsApp, Instagram DMs, comment replies |

Use an event when the agent should write: it judges the news against the
current state, words it for the person, and is ready for the reply. Add
`required: true` when the person must hear it even if the agent would stay
quiet. Use a message when the words must not change (an amount, a legal
notice, a [verification code](https://vatio.ai/docs/api/messages#verify-a-phone)) or when only a
template will do.

## Body

| Field | | |
|---|---|---|
| `to` | required | The person's WhatsApp number, in international format |
| `event` | required | What happened, or what to tell the person, and how to look it up: a task's id, an order's number. Up to 1,000 characters |
| `at` | optional | When it should reach them, ISO 8601 with its offset. Without it, it goes out within about a minute. A time in the past means now. Up to 400 days ahead |
| `key` | optional | Your name for this event; see [Keys](#keys). Also taken as the `Idempotency-Key` header |
| `required` | optional | `true`: the agent always writes. Without it, the agent may decide there is nothing to say, and the message ends `skipped` |
| `template` | optional | The approved template to open the conversation with if the person's 24-hour window is closed when it falls due, as in [Messages](https://vatio.ai/docs/api/messages#what-goes-out) |
| `external_ref` | optional | Your own id. Filter by it with `GET /events?external_ref=` |

Write `event` for the agent, not as the message to the person: the agent
writes the message, in the person's language, and only from what the event
says and what its tools return. An event that names the record ("task 12")
lets the agent read where it stands now, so a deadline that moved or a task
someone closed needs nothing from you. Say who the news is from and the agent
passes it on as theirs: "Claude finished: …" goes out as "Claude tells me
that…".

## When it falls due

Within about a minute of `at`, the events due for one person go to the agent
together, as one turn, oldest first. Four events your backend sent for one
change of a task ("created", "assigned", "due date set", "file attached")
reach the person as one message. An event without `at` waits 30 seconds for
the others of its burst.

The agent then:

1. Checks each event with its tools, wherever it has one for it.
2. Writes one message about what still matters, or nothing. When one of the
   events is `required`, it always writes.

The message waits out the workspace's
[quiet hours](https://vatio.ai/docs/handoff#hours-when-the-agent-does-not-answer) and waits its turn
behind a conversation in progress. If the person's 24-hour window is closed,
it goes out as your `template`, or fails with `outside_window` when the event
has none. The event's `message` is that message, in the shape of
[`GET /messages/:id`](https://vatio.ai/docs/api/messages#responses) with `mode: "agent"`, and its
status is `sent`, `failed`, or `skipped` when the agent decided there was
nothing to say.

A reply comes to the agent like any other, and the agent knows what the
message was about on every later turn.

## Keys

A `key` names one event. Publishing it again:

- **while it is still `scheduled`** moves it and rewrites it: send
  `task-12-reminder` again with the new `at` when the deadline moves;
- **after it `failed` or was `cancelled`**, schedules it again;
- **once it was `dispatched`**, changes nothing, and answers `200` with
  `Idempotent-Replayed: true` and the event as it stands.

So a retry from a job queue never reaches anyone twice, and a key per
record and purpose (`task-12-assigned`, `task-12-reminder`) is all the state
your backend keeps.

`DELETE /events?key=task-12-reminder`, or `DELETE /events/:id`, cancels one
still waiting. You rarely need it: an event the agent finds stale is skipped.

## Responses

- **`202`**: recorded, or still waiting and now rewritten. The body is the event.
- **`200`** with `Idempotent-Replayed: true`: its key already went out.

```json
{
  "id": 812,
  "status": "scheduled",
  "source": "api",
  "environment": "live",
  "to": "+56912345678",
  "event": "Task 12 (Plans for project 430) is due on 2026-11-25. Look it up by its id before writing.",
  "at": "2026-11-23T09:00:00-03:00",
  "key": "task-12-reminder",
  "external_ref": null,
  "template": null,
  "required": false,
  "error": null,
  "dispatched_at": null,
  "message": null,
  "created_at": "…",
  "updated_at": "…"
}
```

- `status`: `scheduled` until it falls due; `dispatched` once it is in a
  message, which `message` is (the shape of
  [`GET /messages/:id`](https://vatio.ai/docs/api/messages#responses)); `failed` when not even a
  message could start (`error.code` says why: `outside_window` with no
  `template`, `opted_out`, `whatsapp_not_connected`…); `cancelled`.
- `source`: `api` for yours; `tool` for one a [tool's answer](https://vatio.ai/docs/tools#follow-ups)
  scheduled, `agent` for a [reminder the agent set itself](https://vatio.ai/docs/tools#built-in-tools),
  `schedule` for a [schedule](https://vatio.ai/docs/schedules).

## Lists

| Method | Path | |
|---|---|---|
| GET | `/events?status=&key=&external_ref=&limit=&before=` | Newest first, up to 50; page with `before` = the `next_before` of the last page |
| GET | `/events/:id` | One event |
| DELETE | `/events/:id`, `/events?key=` | Cancel one still waiting; `409 event_not_scheduled` otherwise |

## Webhooks

What became of the message is in the [message webhooks](https://vatio.ai/docs/api/webhooks#messages):
`message.sent`, `message.read`, `message.failed`, and `message.skipped` when the
agent decided not to write. `event.failed` comes when an event could not even
become a message, with the event in `data.event`.

## Refusals

| Status | `code` | |
|---|---|---|
| 409 | `too_many_scheduled` | This person has 100 events waiting. Give events that replace each other the same key |
| 422 | `invalid_recipient` | `to` is not a phone number in international format |
| 422 | `event_required`, `event_too_long` | No `event`, or one over 1,000 characters |
| 422 | `invalid_at`, `at_too_far` | `at` is not ISO 8601, or is more than 400 days away |
| 422 | `template_required` | A `template` without `name` and `language` |
| 422 | `key_too_long` | A `key` over 255 characters |

The rest of what can go wrong is known only when it falls due, and ends the
event `failed` with the code [Messages](https://vatio.ai/docs/api/messages#refusals) would have
answered.
