# Create server rules

> Create automation rules on the Rules tab, which we store but do not run yet.

Source: https://docs.coritan.com/managed-containers/rules/

In the dashboard:

- /dashboard/servers/…/rules: https://www.coritan.com/dashboard/servers

The **Rules** tab holds a server's automation rules. A rule names a *trigger*, optional *conditions* and the *actions* to take, each written as JSON.

> [!WARNING]
> We do not run rules yet. A rule you create is saved, but nothing triggers it, so it never restarts the server or sends a command. To run a command, a power action or a snapshot at set times, use a [schedule](/managed-containers/schedules/).

## Before you begin

- On a server someone shared with you, the owner must have given you the Manage rules permission.
- The rule's fields take JSON. Put names and text in double quotes, for example `{"type": "power", "action": "restart"}`.

## Create a rule

1. In the [dashboard](https://www.coritan.com/dashboard/servers), go to **Container Apps** and open the server, then the **Rules** tab.
2. Select **New rule…**.
3. In **Name**, enter a name, for example `Restart on crash`.
4. Under **Trigger**, choose what the rule reacts to: **Manual**, **Scheduled**, **Event**, **Crash** or **Resource threshold**.
5. In **Trigger config**, enter the trigger's settings as JSON, or leave `{}`.
6. In **Conditions**, enter what must be true for the rule to act, or leave `{}` for none.
7. In **Actions**, enter what the rule does.
8. Leave **Enabled** ticked to switch the rule on, or clear it to save it switched off.
9. Select **Create rule**.

## Rule format

We store each field as you enter it and check only that it is JSON. **Actions** takes one action, or a list of them:

| Action | Example |
|---|---|
| Run a console command | `{"type": "command", "command": "say Restarting in 5 minutes"}` |
| Send a power signal | `{"type": "power", "action": "restart"}` with `start`, `stop`, `restart` or `kill` |

**Conditions** can limit a rule to a window of hours in UTC, for example `{"type": "time_window", "start_hour": 2, "end_hour": 5}` for 02:00 to 05:59.

## Turn a rule off or delete it

- To turn a rule off or on, use the switch in its **Enabled** column.
- To delete a rule, select the bin icon at the end of its row, then **Delete rule**.

## Result

The dashboard confirms a new rule with `Rule created.`. The list shows each rule's name, its **Trigger**, when you created it and whether it is enabled. Under the name, a rule that has never acted reads `Never fired`, which is what every rule shows while we do not run them.

## Troubleshooting

`the Actions field is not valid JSON.`
: The field it names holds something other than JSON. Check the brackets and put every name and text value in double quotes.

`Could not update the rule` or `Could not delete the rule`
: Turning a rule off and deleting a rule fail with a server error at the moment. A rule has no effect while we do not run rules, so leaving it in place does no harm. [Contact support](https://www.coritan.com/dashboard/support) if you need it removed.

`Insufficient permissions`
: On a server someone shared with you, ask the owner for the Manage rules permission.

## Related

- [Schedule server tasks](/managed-containers/schedules/) runs commands, power actions and snapshots at set times.
- [Share a server with other users](/managed-containers/users/) explains the Manage rules permission.

## With the API

Create a rule with `POST /api/v1/client/servers/{uuid}/rules`. The body takes `name`, `trigger_type` (the dashboard offers `manual`, `scheduled`, `event`, `crash` and `resource`), `trigger_config` (an object, default `{}`), `conditions` (an object, a list or `null`), `actions` (an object or a list) and `enabled` (default `true`):

```bash
curl -X POST https://api.coritan.com/api/v1/client/servers/$SERVER/rules \
  -H "Authorization: Bearer $CORITAN_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"name": "Restart on crash", "trigger_type": "crash", "actions": [{"type": "power", "action": "restart"}]}'
```

The response (`201`) is the rule, with `uuid`, `name`, `trigger_type`, `trigger_config`, `conditions`, `actions`, `enabled`, `created_at`, `updated_at` and `last_triggered_at`, which stays `null` while we do not run rules.

| Route | What it does |
|---|---|
| `GET /api/v1/client/servers/{uuid}/rules` | Lists the server's rules, oldest first. |
| `PUT /api/v1/client/servers/{uuid}/rules/{rule_uuid}` | Changes the fields you send. It answers `500` at the moment. |
| `DELETE /api/v1/client/servers/{uuid}/rules/{rule_uuid}` | Deletes the rule. It answers `500` at the moment. |

Creating, changing and deleting need `settings.automation`. Any user on the server can list its rules.

## API

- `GET /api/v1/client/servers/{uuid}/rules`: List rules for a server (https://docs.coritan.com/api/reference/client/container-apps/servers-rules/#op-get-api-v1-client-servers-uuid-rules)
- `POST /api/v1/client/servers/{uuid}/rules`: Create a new rule (https://docs.coritan.com/api/reference/client/container-apps/servers-rules/#op-post-api-v1-client-servers-uuid-rules)
- `PUT /api/v1/client/servers/{uuid}/rules/{rule_uuid}`: Update a rule (https://docs.coritan.com/api/reference/client/container-apps/servers-rules/#op-put-api-v1-client-servers-uuid-rules-rule-uuid)
- `DELETE /api/v1/client/servers/{uuid}/rules/{rule_uuid}`: Delete a rule (https://docs.coritan.com/api/reference/client/container-apps/servers-rules/#op-delete-api-v1-client-servers-uuid-rules-rule-uuid)
