> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ana.fi/llms.txt
> Use this file to discover all available pages before exploring further.

# Rule language overview

> What a rule document is, what it looks like, and how it gets from your code to the Anafi engine.

After you read this page, you can write a small rule document, and you know which page holds each part of the language.

## What a rule document is

A rule document is one YAML text. It tells the Anafi engine what to do for one account: when to act, and what to pay, swap or send. Each account has at most one committed rule document at a time. A new commit replaces the full document.

A document holds named rules. Each rule has one trigger and one or more actions:

* The trigger says when the rule fires: on a schedule, at fixed instants, when a transfer arrives or leaves, or when a token price crosses a level.
* The actions say what the engine does when the rule fires: send tokens, swap tokens, pay a bank account, or send an email.

The engine acts for the account through the adapter that the owner granted a permission to. See [how Anafi works](https://docs.ana.fi/concepts/how-anafi-works.md) and [Contracts overview](https://docs.ana.fi/contracts/overview.md).

## A first complete document

This document pays 25 USDC every Friday at 09:00 in Berlin, and sends 20% of each incoming USDC payment of 100 USDC or more to a savings address.

```yaml theme={null}
variables:
  landlord: "0x1111111111111111111111111111111111111111"
  savings: "0x2222222222222222222222222222222222222222"

global:
  fees_method: { type: fixed_token, token: USDC }

rules:
  weekly_payment:
    trigger:
      type: calendar
      day_of_week: ["fri"]
      time_of_day: "09:00"
      timezone: "Europe/Berlin"
    actions:
      - type: send_one
        to: "$landlord"
        value: "25 USDC"

  save_a_fifth:
    trigger:
      type: incoming_tx
      value: "100.. USDC"
    actions:
      - type: send_one
        to: "$savings"
        value: "trigger.value * 20%"
```

This document is valid on `polygon`, `xlayer` and `polygon-amoy`, for both account kinds, `safe_module` and `erc20_allow`. Each of the three networks has `USDC`, and `fees_method` names an ERC-20 fee token, which an `erc20_allow` account needs.

## The shape of a document

A document is a YAML mapping with at most four top-level keys. Each key is optional, but the document must have `rules` or `agent_envelope`.

| Key | Type | What it holds |
| - | - | - |
| `variables` | mapping of name to text | Names for addresses and IBANs. You refer to an entry as `$name`. |
| `global` | mapping | Limits and settings for the full document: allowlists, the fee token, the email inbox. |
| `rules` | mapping of rule name to rule | The rules. `rules: {}` is valid and means "run nothing". |
| `agent_envelope` | mapping | A declaration for an agent key. The engine accepts it and enforces nothing in it. |

Every other top-level key is refused. The full field reference is on [Document structure](https://docs.ana.fi/rules/document.md).

## How a document reaches the engine

The document never goes on chain as text. Your app sends it encrypted to the engine, and the owner commits its salted hash on chain.

**1. Stage**

The SDK encrypts the salt and the YAML on the device to the key of the engine. The Anafi API relays the encrypted text. The engine opens it, runs every check on [Validation](https://docs.ana.fi/rules/validation.md), and answers with a signed attestation and a list of warnings. A document that fails a check gets a refusal and is not stored.

**2. Commit**

The owner signs one transaction that writes the commitment `keccak256(salt ‖ yaml)` on chain. The engine adopts the document when it sees the commit.

**3. Fire**

The engine fires the rules of the adopted document. Each payment goes through the adapter of the account.

The full flow, with each wallet prompt and each HTTP call, is on [Flows overview](https://docs.ana.fi/flows/overview.md). The SDK calls are on [Documents](https://docs.ana.fi/sdk/documents.md).

## Author with a coding agent

Each Anafi API host serves the full rule language as one Markdown page. Give the URL of your network to your coding agent:

| Network | URL |
| - | - |
| `polygon` | `https://api.polygon.ana.fi/api/dsl/docs` |
| `xlayer` | `https://api.xlayer.ana.fi/api/dsl/docs` |
| `polygon-amoy` | `https://api.polygon-amoy.ana.fi/api/dsl/docs` |

The route is `GET /api/dsl/docs`. It needs no sign-in and no key. The three pages are the same, except for these parts:

* The first paragraph names the network.
* The paragraph about tokens lists the tokens that the engine can move on that network.
* Only the `polygon-amoy` page has the `off_ramp` example, because only that network has a bank payout rail.

> [!WARNING]
>
> Read every document that an agent writes before the owner signs the commit. The commit is the consent of the owner.

## The reading principle

A rule must have one meaning for a person who reads it. The language helps you:

* Every mapping accepts only its known keys. An unknown key, such as `max_executions: 5`, is refused. It does not silently do nothing.
* An empty allowlist is refused, because `[]` looks like "nobody" but would mean "anybody".
* An amount always names its token. A bare `"50%"` or `"100"` is not an amount.
* A token symbol is case-sensitive. `usdc` is refused.

Some choices stay open in the grammar. Make them in the direction that keeps the rule clear:

* Use `calendar` for a repeated schedule. Use `at_fixed_time` only for instants that no calendar pattern describes.
* Set `timezone` on each `calendar` trigger that names a time of day. The default is UTC.
* Write `start` and `expiry` as absolute instants. Resolve "tomorrow" or "for one month" before you write the document.
* Do not rename a rule that is in use. The rule name is part of its identity. See [rule identity](https://docs.ana.fi/rules/document.md#rule-names-and-identity).

## Pages in this section

> [card] [Document structure](https://docs.ana.fi/rules/document.md)
>
> Top-level keys, `global`, rule keys, YAML scalar rules, size limits.

> [card] [Triggers](https://docs.ana.fi/rules/triggers.md)
>
> `periodic`, `calendar`, `at_fixed_time`, `incoming_tx`, `outgoing_tx`, `price_crossed`.

> [card] [Actions](https://docs.ana.fi/rules/actions.md)
>
> `send_one`, `send_batch`, `exchange`, `notify`, `off_ramp`.

> [card] [Values](https://docs.ana.fi/rules/values.md)
>
> Amounts, percentages, `trigger.value`, arithmetic, ranges, rounding.

> [card] [Limits](https://docs.ana.fi/rules/limits.md)
>
> Allowlists, rule bounds, and the on-chain caps of an `erc20_allow` account.

> [card] [Validation](https://docs.ana.fi/rules/validation.md)
>
> Every refusal and warning, and what validates but does not run.

> [card] [Examples](https://docs.ana.fi/rules/examples.md)
>
> Complete documents for real use cases.

> [card] [Grammar reference](https://docs.ana.fi/rules/grammar.md)
>
> The full schema, the expression grammar, and every numeric limit in one place.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.