# Triggers

A Trigger starts a Process each time a [System event](/docs/system-events) of its type occurs and its conditions pass. Each start creates a [Run](/docs/runs). Manage Triggers through the Data API at `/automation/triggers`.

## Create a Trigger

Create the [Process](/docs/processes#create-a-process) first, then link it to a System event type. This Trigger starts the Process when a conversation of one Assistant ends:

```bash title="Create a Trigger"
curl https://data.reecall.io/automation/triggers \
  --header 'Content-Type: application/json' \
  --header 'Authorization: Bearer sk_YOUR_API_KEY' \
  --data '{
    "name": "Call report",
    "description": "Judge each completed call of the support Assistant",
    "eventType": "exchange.completed",
    "processId": "<your-process-id>",
    "conditions": {
      "operator": "AND",
      "conditions": [
        { "source": "event.data.assistantId", "filter": "EQUALS", "value": "<your-assistant-id>" }
      ]
    }
  }'
```

The response is the stored Trigger, with its `id` and `enabled: true`. The Trigger applies to System events that occur after you save it.

## Fields

| Field | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `string` | No | | Name shown in lists |
| `description` | `string` | No | | What the Trigger does |
| `eventType` | `string` | Yes | | The [System event type](/docs/system-events) to listen to |
| `processId` | `string` (UUID) | Yes | | The Process to start. See [Scope](#scope) for the Processes a Trigger can reach. |
| `conditions` | `object` or `null` | No | `null` | Conditions the System event must pass. `null` starts the Process on every System event of the type. See [Conditions](#conditions). |
| `input` | `object` or `null` | No | `null` | Fixed values the Process reads under `input`. See [Input](#input). |
| `retry` | `string` or `object` | No | `"none"` | Retry policy of the Runs this Trigger starts. See [Retry policy](#retry-policy). |
| `enabled` | `boolean` | No | `true` | `false` stops the Trigger from starting new Runs |
| `projectId` | `string` (UUID) or `null` | Depends on the key | | The Project of a project Trigger, or `null` for an organization Trigger. Cannot change after creation. |
| `organizationId` | `string` (UUID) | Set from the key | | Cannot change after creation |

## Scope

A Trigger belongs to one Project or to the whole Organization. The scope sets which System events it receives and which Processes it can start.

| Scope | `projectId` | Receives the System events of | Can start |
| --- | --- | --- | --- |
| Project | The Project's ID | Its Project | A Process of its Project, or an organization-level Process |
| Organization | `null` | Every Project of the Organization | An organization-level Process |

A project-scoped API key always creates project Triggers in its own Project. It can read the organization Triggers but not change them. An organization-scoped key creates a project Trigger when you set `projectId`, and an organization Trigger when you leave it out.

A Run belongs to the Project of the System event that started it, whatever the scope of its Trigger. A project-scoped key reads the Runs of an organization Trigger for its own Project only.

## Conditions

Conditions use the syntax of a [Rule step](/docs/rules). Each condition reads a value from the System event with `source`, applies a `filter`, and compares the result with `value` when the filter needs one:

```json title="Conditions"
{
  "operator": "AND",
  "conditions": [
    { "source": "event.data.assistantId", "filter": "EQUALS", "value": "8f9c2b1e-4d3a-4c7b-9e6f-1a2b3c4d5e6f" },
    { "source": "event.data.duration", "filter": "GREATER_THAN", "value": 30 }
  ]
}
```

- `operator` is `AND` (default) or `OR`.
- `conditions` holds 1 to 20 conditions.
- `source` is a path in `event`: `id`, `type`, `occurredAt`, `refId`, `organizationId`, `projectId`, and the `data` fields of the [System event type](/docs/system-events).
- `filter` is one of the Rule step filters, such as `EQUALS`, `CONTAINS`, `GREATER_THAN`, `EXISTS`, or `IN`.

:::warning{title="Check filter names"}

Reecall does not check filter names when you save a Trigger. A Trigger with a misspelled filter never starts its Process.

:::

## Input

`input` holds fixed values, such as a webhook URL or a language, that the Process reads under `input`. Reecall passes them as they are, without rendering templates. They override the values of the same name in the Process's `defaultContext`. See [Processes](/docs/processes#what-a-process-reads).

Do not put secrets in `input`. Store them as secret [Variables](/docs/variables) and read them with `{{ variables.KEY }}`.

## Retry policy

Retries are off by default. When a Run fails for a reason that is safe to repeat, such as a `429` or `503` answer, the policy decides whether it runs again:

| Value | Effect |
| --- | --- |
| `"none"` | No retry |
| `{ "immediate": 2 }` | Up to 2 retries, right after the failure. From 1 to 3. |
| `{ "delays": ["5m", "1h"] }` | One retry per delay, after waiting that long. 1 to 3 delays, each in minutes (`m`) or hours (`h`), from `1m` to `24h`. |

:::warning{title="A retry runs the whole Process again"}

A retry starts again from the first step. Steps that succeeded before the failure run a second time, and SMS and email steps send their messages again. Turn retries on only for a Process whose steps can safely run twice.

:::

See [Retries](/docs/runs#retries) for the failures that start a retry and the `Idempotency-Key` header of request steps.

## Turn a Trigger off

Set `enabled` to `false` to stop new Runs and keep the Trigger:

```bash title="Turn a Trigger off"
curl --request PATCH https://data.reecall.io/automation/triggers/<trigger-id> \
  --header 'Content-Type: application/json' \
  --header 'Authorization: Bearer sk_YOUR_API_KEY' \
  --data '{ "enabled": false }'
```

Runs already started continue. To stop one, [cancel it](/docs/runs#cancel-a-run).

## Avoid loops

A Process that writes to the Reecall API can cause new System events. Check that it cannot start its own Trigger again, or add a condition that excludes the records it writes. A Run started by `run.failed` never announces another `run.failed`.

## Errors

| Code | Status | Cause |
| --- | --- | --- |
| `INVALID_TRIGGER` | 400 | `eventType` is missing |
| `INVALID_EVENT_TYPE` | 400 | `eventType` is not a System event type. The error lists the allowed types. |
| `INVALID_TRIGGER_CONDITIONS` | 400 | `conditions` has an unknown key, no condition, more than 20, or a condition without `source` or `filter` |
| `INVALID_RETRY_POLICY` | 400 | `retry` is not one of the forms above |
| `PROCESS_OUT_OF_REACH` | 400 | The Process belongs to another Organization or Project, or a Project Process is linked to an organization Trigger |
| `NOT_FOUND` | 404 | No Process with this ID in reach of the key |
| `READ_ONLY_FIELD` | 400 | The update changes `organizationId` or `projectId` |
| `SCOPE_VIOLATION` | 403 | A project-scoped key tries to change an organization Trigger |
