# Rules

Read rules, save drafts, test them, and turn them on, pause, run or archive them.

Web version: https://www.svanja.com/docs/api/rules/

A rule runs automatically when a matching email arrives or at a scheduled time. You can view them in [Rules](https://app.svanja.com/rules). These endpoints let you draft and test rules from code, or pause all of them at once. To read rules, your key needs the Rules area, which is included in Full access and Read-only. To make changes, it needs Full access or Rules set to **Read and write**. Turning a rule on always requires a person's approval in the app, and whether a key can change a rule also depends on the person it belongs to. For more on how rules work, see [Rules](https://www.svanja.com/docs/rules/).

Use `GET /rules` and `GET /rules/{id}` to read rules. `POST /rules` creates a draft, and `PATCH /rules/{id}` updates it. Drafts never run.

`POST /rules/{id}/tests` tests a rule and saves the result without running any of its actions. By default, it checks recent events and builds previews for up to 10 matches, which uses a few credits. To test a scheduled rule as if it were due right now, send `{"mode":"schedule_now"}`.

`POST /rules/{id}/actions` handles lifecycle actions: `enable`, `pause`, `resume`, `run_now`, `replay`, `stop_with_tasks`, `archive`, and `restore`. The first time you call `enable`, the API returns `authorization_required` along with an `approval_url`, and the rule stays unchanged. Once a person approves it, send the same action again with `authorization_request_id`. Replaying a run follows the same approval flow. For general API details, see [The REST API](https://www.svanja.com/docs/api/), or use [svanja rule](https://www.svanja.com/docs/cli/rules/) to manage rules from a terminal.

## Operations

| Operation | What it does | Effect | Endpoints |
| --- | --- | --- | --- |
| `rule.enable` | Enable a Rule. | Changes access or automation | `POST /rules/{id}/actions` |
| `rule.read` | Read Rules. | Reads only | `GET /rules`<br>`GET /rules/{id}` |
| `rule.reassign_executor` | Reassign a Rule executor. | Changes access or automation | `POST /rules/{id}/actions` |
| `rule.replay` | Replay a Rule run. | Acts outside Svanja | `POST /rules/{id}/actions` |
| `rule.save_draft` | Save a Rule draft. | Changes data in Svanja | `POST /rules`<br>`PATCH /rules/{id}` |
| `rule.test` | Test a Rule. | Reads only | `POST /rules/{id}/tests` |

## Endpoints

### GET /api/v1/rules

List Rules

### POST /api/v1/rules

Create a Rule draft

### GET /api/v1/rules/{id}

Read a Rule

| Parameter | In | Type | Required | Notes |
| --- | --- | --- | --- | --- |
| `id` | path | string | yes |  |

### PATCH /api/v1/rules/{id}

Update a Rule draft

| Parameter | In | Type | Required | Notes |
| --- | --- | --- | --- | --- |
| `id` | path | string | yes |  |

### POST /api/v1/rules/{id}/actions

Run a Rule lifecycle action

| Parameter | In | Type | Required | Notes |
| --- | --- | --- | --- | --- |
| `id` | path | string | yes |  |

### POST /api/v1/rules/{id}/tests

Test a Rule

| Parameter | In | Type | Required | Notes |
| --- | --- | --- | --- | --- |
| `id` | path | string | yes |  |
