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

# Policies and bindings

> Group Tracer rules and limits into versioned policies, publish and revert revisions, and bind each policy to a ledger with ENFORCE, ADVISORY, AUDIT, or OFF per control.

A policy is how you decide which rules and limits apply, in which order, and how strictly, for each ledger. You publish a policy as an immutable revision, and you bind it to a ledger with one instruction for its rules and one for its limits.

**What changes in your operation:** every change to what a rule decides becomes a version. You apply it at once, or you edit a draft and publish it when you are ready. Every policy that holds the rule then gets a new revision that you can read back and revert. One ledger can enforce a policy while another ledger only reports what it would decide.

**Trade-off:** you manage three objects instead of one. After your first publication, a rule or limit outside every published policy does not apply to the decide flow or to `POST /v1/validations`.

> [!NOTE]
>
> Policies, bindings, and rule and limit versions are available from the Midaz release that introduces policies. Policies apply to the [decide flow](https://docs.lerian.studio/en/products/tracer/ledger-decide-flow.md) of the Midaz ledger and to `POST /v1/validations`. The reserve flow of the ledger never reads them. It keeps evaluating every active rule and limit.

> [!TIP]
>
> **Who is this guide for?** Risk and fraud analysts who decide what applies to each ledger, and the developers who automate that through the API. Read the [Rules engine](https://docs.lerian.studio/en/products/tracer/rule-engine.md) first for the expressions themselves.

## How the pieces fit

***

| Object | What it holds | How it changes |
| - | - | - |
| Rule or limit version | What a rule decides (expression, action, scopes) or what a limit enforces (maximum amount, scopes, time window, custom period) | `PATCH` makes a new version current at once. `PATCH ...?draft=true` writes a draft version, and `POST .../publish` makes the draft current |
| Policy | A working set (an ordered list of rules and a set of limits) and its immutable revisions | `PUT .../rules` and `PUT .../limits` change the working set. `POST .../publish` creates the next revision |
| Binding | One policy, one ledger, a selector, and one action for each control | Takes effect when you write it. It has no draft and no history |
| Fallback mode | The ledger's `tracer.mode` (`enforce` or `advisory`) | Applies to each control that no binding decides |

A policy has two controls: `RULES` and `LIMITS`. Each control gets a **verdict** (what it decided) and an **instruction** (what the caller does with the verdict). The instruction comes from a binding, or from the fallback mode when no binding decides it.

***

## Versions of rules and limits

***

What a rule decides and what a limit enforces are versioned. Every rule and limit that existed at the upgrade became version 1, current.

* `PATCH /v1/rules/{rule_id}` with `expression`, `action`, or `scopes` applies at once, as in earlier releases. Tracer writes a new version and publishes it in one step. The same applies to `PATCH /v1/limits/{limit_id}` with `maxAmount`, `scopes`, the time window, or the custom period. The expression of a rule accepts this edit only while the rule is `DRAFT` (code `0351`).
* With `?draft=true`, the same `PATCH` writes a draft version instead, in any status. No evaluation reads a draft. A second draft edit replaces the draft.
* `POST /v1/rules/{rule_id}/publish` and `POST /v1/limits/{limit_id}/publish` make the draft the current version. In the same transaction, every published policy that holds the rule or limit gets a new revision with the new version. When the transaction commits, Tracer emits `rule.updated` or `limit.updated`.
* Pass `?draftVersion=` with the draft version you reviewed to publish only that draft. If someone saves a new draft between your review and your publish, the pending draft is another version. Tracer then answers `409` with code `0603` and publishes nothing. Read the new draft, review it, and publish it with its own number. With `draftVersion` and no pending draft, the answer is also `0603`. Without `draftVersion`, publish takes the pending draft, whatever its number.
* While a draft is pending, a `PATCH` without `draft=true` that changes versioned content answers `409` with code `0602`. Otherwise a later publish of the draft would undo that change in silence. Publish the draft first, or send the change with `draft=true`.
* `name` and `description` are not versioned. A `PATCH` of them changes the rule or limit at once, also while a draft is pending.
* Status (`ACTIVE`, `INACTIVE`) is not versioned. A policy reads it live. When you deactivate a rule, it stops within one rule sync cycle (`RULE_SYNC_POLL_INTERVAL_SECONDS`). A deactivated limit takes no new hold, and the holds it already gave settle normally.

Write a new threshold of a rule as a draft:

```bash theme={null}
curl -X PATCH "http://localhost:4020/v1/rules/4d9c2e70-8b1a-4f36-9c07-5e2a1b3d4f68?draft=true" \
  -H "X-API-Key: your-secure-api-key" \
  -H "Content-Type: application/json" \
  -d '{ "expression": "transaction.scheme == \"PIX\" && transaction.amount > 3000" }'
```

The response is the current rule with its `currentVersion` and `draftVersion`. The `expression` in it is still the current one:

```json theme={null}
{
  "ruleId": "4d9c2e70-8b1a-4f36-9c07-5e2a1b3d4f68",
  "name": "Deny Pix above BRL 5,000",
  "expression": "transaction.scheme == \"PIX\" && transaction.amount > 5000",
  "action": "DENY",
  "scopes": [],
  "status": "ACTIVE",
  "currentVersion": 1,
  "draftVersion": 2,
  "createdAt": "2026-11-02T10:00:00Z",
  "updatedAt": "2026-11-09T14:05:00Z"
}
```

Publish the draft you reviewed, version 2:

```bash theme={null}
curl -X POST "http://localhost:4020/v1/rules/4d9c2e70-8b1a-4f36-9c07-5e2a1b3d4f68/publish?draftVersion=2" \
  -H "X-API-Key: your-secure-api-key"
```

```json theme={null}
{
  "id": "4d9c2e70-8b1a-4f36-9c07-5e2a1b3d4f68",
  "version": 2,
  "policyRevisions": [
    { "policyId": "7b1e0c52-3f4a-4d8e-9a61-2c5d8e0f1a37", "revision": 4 }
  ]
}
```

`policyRevisions` lists the new revision of each published policy that holds the rule. `GET /v1/rules/{rule_id}/versions` and `GET /v1/limits/{limit_id}/versions` list every version, oldest first, with `createdBy` (`user:<sub>`, `api_key:<label>`, or `system`) and `createdAt`.

> [!WARNING]
>
> A draft does not reach traffic until you publish it. Publish without a draft answers `422` with code `0597`, or `409` with code `0603` when you pass `draftVersion`.

***

## Policies

***

A policy holds a **working set** and its **revisions**. The working set is what the next revision will hold. A revision is immutable: it pins each rule (with its position) and each limit at the version that was current when you published.

### Create a policy and set its working set

**1. Create the policy**

```bash theme={null}
curl -X POST http://localhost:4020/v1/policies \
  -H "X-API-Key: your-secure-api-key" \
  -H "Content-Type: application/json" \
  -d '{ "name": "retail", "description": "Antifraud rules and daily limits for retail accounts" }'
```

The name is unique among the policies that are not deleted, from 1 to 255 characters. A name another policy holds answers `409` with code `0589`. The policy starts unpublished, with an empty working set, and evaluates nothing:

```json theme={null}
{
  "policyId": "7b1e0c52-3f4a-4d8e-9a61-2c5d8e0f1a37",
  "name": "retail",
  "description": "Antifraud rules and daily limits for retail accounts",
  "createdAt": "2026-11-09T14:10:00Z",
  "updatedAt": "2026-11-09T14:10:00Z"
}
```

**2. Set the rules, in evaluation order**

```bash theme={null}
curl -X PUT http://localhost:4020/v1/policies/7b1e0c52-3f4a-4d8e-9a61-2c5d8e0f1a37/rules \
  -H "X-API-Key: your-secure-api-key" \
  -H "Content-Type: application/json" \
  -d '{ "ruleIds": [
        "4d9c2e70-8b1a-4f36-9c07-5e2a1b3d4f68",
        "a3c81f07-2d6e-4b19-8e5a-7f0c3d1b9e24"
      ] }'
```

The list replaces the rules of the working set. It adds, removes, and reorders in one call. Each rule must exist and appear once. An empty list removes all rules.

**3. Set the limits**

```bash theme={null}
curl -X PUT http://localhost:4020/v1/policies/7b1e0c52-3f4a-4d8e-9a61-2c5d8e0f1a37/limits \
  -H "X-API-Key: your-secure-api-key" \
  -H "Content-Type: application/json" \
  -d '{ "limitIds": ["e5f20a91-6c3b-4f7d-8a12-9b4e0d6c2f58"] }'
```

Both calls answer with the working set:

```json theme={null}
{
  "policyId": "7b1e0c52-3f4a-4d8e-9a61-2c5d8e0f1a37",
  "ruleIds": [
    "4d9c2e70-8b1a-4f36-9c07-5e2a1b3d4f68",
    "a3c81f07-2d6e-4b19-8e5a-7f0c3d1b9e24"
  ],
  "limitIds": ["e5f20a91-6c3b-4f7d-8a12-9b4e0d6c2f58"]
}
```

Nothing evaluates differently until you publish.

### Publish a revision

```bash theme={null}
curl -X POST http://localhost:4020/v1/policies/7b1e0c52-3f4a-4d8e-9a61-2c5d8e0f1a37/publish \
  -H "X-API-Key: your-secure-api-key"
```

```json theme={null}
{
  "policyId": "7b1e0c52-3f4a-4d8e-9a61-2c5d8e0f1a37",
  "revision": 1,
  "publishedBy": "api_key:tracer-default",
  "publishedAt": "2026-11-09T14:12:00Z",
  "rules": [
    { "ruleId": "4d9c2e70-8b1a-4f36-9c07-5e2a1b3d4f68", "version": 2, "position": 1 },
    { "ruleId": "a3c81f07-2d6e-4b19-8e5a-7f0c3d1b9e24", "version": 1, "position": 2 }
  ],
  "limits": [
    { "limitId": "e5f20a91-6c3b-4f7d-8a12-9b4e0d6c2f58", "version": 1 }
  ]
}
```

Revisions are numbered from 1. Only the latest revision of a policy evaluates. `publishedBy` is `user:<sub>` under Access Manager, `api_key:<label>` with the API key, or `system`.

Publish answers `422` in two cases:

* `0593`: two bindings of the policy in one ledger have no relative order and can match the same transaction.
* `0594`: published policies would hold different versions of the same limit. The error names the policies and the versions.

### Read and revert revisions

* `GET /v1/policies/{policy_id}/revisions` lists the revisions, oldest first, with who published each one and when.
* `GET /v1/policies/{policy_id}/revisions/{revision}` returns one revision with the rules and limits it pins.
* `POST /v1/policies/{policy_id}/revert` with `{"revision": 2}` publishes a new revision that copies the pins of revision 2. The new revision carries `revertedFrom`. The published revision only moves forward, and the reverted one stays readable.

A revert never reactivates an inactive rule or limit, because status is live. A revert to a revision the policy does not have answers `404` with code `0590`. A revert that would leave two versions of one limit across published policies answers `422` with code `0594`.

### Change or delete a policy

* `PATCH /v1/policies/{policy_id}` changes `name` or `description`. Neither is part of a revision, so the change applies at once and publishes nothing.
* `DELETE /v1/policies/{policy_id}` soft-deletes the policy, which then stops evaluating. Its revisions stay readable. A policy that bindings still attach to a ledger answers `409` with code `0600`. Delete its bindings first.
* `GET /v1/policies` lists the policies that are not deleted, by name, with their `publishedRevision`. `GET /v1/policies/{policy_id}` also returns the working set.

A rule or limit that a revision or a working set references cannot be deleted: Tracer answers `409` with code `0595` (rule) or `0596` (limit). Deactivate it instead.

***

## How a policy evaluates

***

A policy evaluates its rules in revision order:

1. Tracer walks the rules whose scopes cover the transaction. A rule that is not `ACTIVE` is skipped.
2. The first matching `ALLOW` ends the policy with `ALLOW`. An `ALLOW` is an explicit exception: it shadows only the rules after it. Put a `DENY` that must win before it.
3. A matching `DENY` or `REVIEW` decides the policy.
4. A rule that cannot conclude (an evaluation error, an inconclusive field, or the end of the `MAX_RULES_PER_REQUEST` budget) leaves the policy `REVIEW` with the reason `rule_evaluation_error`, unless a later `DENY` or `REVIEW` matches. An `ALLOW` after it never lets the transaction through. This holds on the reserve and decide flows. On `POST /v1/validations`, a rule with an inconclusive field is left out of the decision instead, so a later `ALLOW` or the no-match default decides, and the response answers `conclusive: false`. Treat that answer as incomplete screening. See [Absent and unavailable fields](https://docs.lerian.studio/en/products/tracer/rule-engine.md#absent-and-unavailable-fields).
5. When no rule matches, the policy answers `ALLOW` on the decide flow. On `POST /v1/validations`, it answers `DEFAULT_DECISION_WHEN_NO_MATCH`.

Policies evaluate in name order, then by id. One `MAX_RULES_PER_REQUEST` budget covers all of them. A rule pinned in two policies evaluates in each, under the instruction of each policy.

A limit pinned in several policies is read once for each decision, at the most restrictive instruction among the policies that hold it, and holds capacity once.

> [!NOTE]
>
> **Order replaces precedence.** Without policies, Tracer evaluates every matching rule and decides `DENY` over `REVIEW` over `ALLOW`. Inside a policy, the order you set decides. That is what makes an `ALLOW` exception possible. Across policies, the combined `verdict` still takes `DENY` over `REVIEW` over `ALLOW`, but it is diagnostic only.

***

## Bindings

***

A binding ties one policy to one ledger and, optionally, to one payment scheme. It has a selector, a `position`, and one action for each control: `rulesAction` and `limitsAction`.

| Action | Rules | Limits |
| - | - | - |
| `ENFORCE` | Evaluated. A `DENY` or `REVIEW` refuses the transaction | Held. Over the ceiling, the transaction is refused and every hold is released |
| `ADVISORY` | Evaluated and reported. Never refuses | Held when within the ceiling. Over it, nothing is held and the control reports `DENY`. The `ENFORCE` holds are kept |
| `AUDIT` | Evaluated and reported. Never refuses | Compared with the current and reserved usage. Never held |
| `OFF` | Not evaluated (`NOT_EVALUATED`) | Not read |
| `INHERIT` | The next binding decides, or the fallback mode | The next binding decides, or the fallback mode |

### Selector

The selector has one optional field, `scheme`. A binding with `{"scheme": "PIX"}` matches the Pix transactions of the ledger, compared without case. An empty selector (`{}`) matches every transaction of the ledger. Any other key in the selector answers `400` with code `0094`.

### Create a binding

```bash theme={null}
curl -X POST http://localhost:4020/v1/bindings \
  -H "X-API-Key: your-secure-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "ledgerId": "9c858901-8a57-4791-81fe-4a34d4dd8ab5",
    "policyId": "7b1e0c52-3f4a-4d8e-9a61-2c5d8e0f1a37",
    "position": 1,
    "selector": { "scheme": "PIX" },
    "rulesAction": "INHERIT",
    "limitsAction": "OFF"
  }'
```

```json theme={null}
{
  "bindingId": "c0d4e8a1-5b27-4f63-9e18-3a6f2b7d0c95",
  "ledgerId": "9c858901-8a57-4791-81fe-4a34d4dd8ab5",
  "policyId": "7b1e0c52-3f4a-4d8e-9a61-2c5d8e0f1a37",
  "position": 1,
  "selector": { "scheme": "PIX" },
  "rulesAction": "INHERIT",
  "limitsAction": "OFF",
  "createdAt": "2026-11-09T14:20:00Z",
  "updatedAt": "2026-11-09T14:20:00Z"
}
```

`position` orders the bindings of one policy in one ledger, starting at 1. Omit it, or send `0`, to append. A position another binding of the pair holds answers `409` with code `0592`.

### How Tracer picks the instruction

For each policy and each control, Tracer walks the bindings of the ledger by position. The first binding that matches and does not `INHERIT` the control decides it. When none does, the control takes the fallback mode.

### Manage bindings

| Operation | Endpoint |
| - | - |
| List the bindings of a ledger, a policy, or both | `GET /v1/bindings?ledger_id=...&policy_id=...` |
| Read one binding | `GET /v1/bindings/{binding_id}` |
| Change the position, the selector (replaced whole), or the actions | `PATCH /v1/bindings/{binding_id}` |
| Reorder the bindings of a policy in a ledger | `PUT /v1/bindings/order` |
| Delete a binding | `DELETE /v1/bindings/{binding_id}` |

With only `policy_id`, the list returns the bindings of the policy across every ledger, ordered by ledger and then by position.

Each binding create, change, reorder, and delete writes an audit event in the same transaction: `BINDING_CREATED`, `BINDING_UPDATED`, `BINDING_REORDERED`, or `BINDING_DELETED`. A reorder writes one `BINDING_REORDERED` event for each binding whose position changes. A policy delete writes `POLICY_DELETED`. Query them with `GET /v1/audit-events?resource_type=binding` or `resource_type=policy`. See [Audit and compliance](https://docs.lerian.studio/en/products/tracer/audit-compliance.md).

A binding's ledger and policy never change. To swap two positions, use `PUT /v1/bindings/order` with every binding of the policy in the ledger, once each. They take positions 1 to n in that order, in one write:

```bash theme={null}
curl -X PUT http://localhost:4020/v1/bindings/order \
  -H "X-API-Key: your-secure-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "ledgerId": "9c858901-8a57-4791-81fe-4a34d4dd8ab5",
    "policyId": "7b1e0c52-3f4a-4d8e-9a61-2c5d8e0f1a37",
    "bindingIds": [
      "f2a6c1d8-0e93-4b57-8d24-6c1a9e3f7b02",
      "c0d4e8a1-5b27-4f63-9e18-3a6f2b7d0c95"
    ]
  }'
```

A list that does not name each binding exactly once answers `400` with code `0598`. After a delete, the remaining positions keep their gaps and still order the bindings.

***

## Check the effective instruction

***

`GET /v1/bindings/effective` shows, for one ledger and, optionally, one scheme, the instruction that each control of every published policy gets, and which binding decided it. It evaluates nothing and moves no limit.

Tracer does not read ledger settings. Pass the ledger's `tracer.mode` as `fallback_mode` (`ENFORCE` or `ADVISORY`). Without it, a control that no binding decides answers `LEDGER_FALLBACK`.

| Query parameter | Description |
| - | - |
| `ledger_id` | Ledger (UUID). Required |
| `fallback_mode` | `ENFORCE` or `ADVISORY` |
| `scheme` | The transaction's payment scheme, compared without case. Without it, only bindings with an empty selector match |

```bash theme={null}
curl "http://localhost:4020/v1/bindings/effective?ledger_id=9c858901-8a57-4791-81fe-4a34d4dd8ab5&fallback_mode=ENFORCE&scheme=PIX" \
  -H "X-API-Key: your-secure-api-key"
```

```json theme={null}
{
  "ledgerId": "9c858901-8a57-4791-81fe-4a34d4dd8ab5",
  "fallbackMode": "ENFORCE",
  "policies": [
    {
      "policyId": "7b1e0c52-3f4a-4d8e-9a61-2c5d8e0f1a37",
      "policyName": "retail",
      "publishedRevision": 4,
      "rules": { "action": "ENFORCE", "source": "FALLBACK" },
      "limits": {
        "action": "OFF",
        "source": "BINDING",
        "bindingId": "c0d4e8a1-5b27-4f63-9e18-3a6f2b7d0c95",
        "bindingPosition": 1
      }
    }
  ]
}
```

`source` is `BINDING` when a binding decided the control, `FALLBACK` otherwise.

***

## Examples

***

**Pix limits off, antifraud intact.** Policy `retail` pins the antifraud rules and the daily limits. Ledger L has one binding of `retail`: selector `{"scheme": "PIX"}`, `rulesAction` `INHERIT`, `limitsAction` `OFF`. A Pix transaction of an `enforce` ledger evaluates the rules at `ENFORCE` (inherited, so the fallback decides) and reads no limit. A TED transaction matches no binding, and both controls take `ENFORCE`.

**Advisory for one ledger.** Policy `antifraud` has one binding on ledger L2: selector `{}`, both controls `ADVISORY`. A transaction of L2 that matches a `DENY` rule answers the verdict `DENY`, the control `ADVISORY` with the effect `PROCEED`, and the outcome `PROCEED`. The ledger proceeds. The same transaction on any other ledger takes that ledger's fallback, and under `enforce` it is refused.

**Limit on an unread segment.** Ledger L3 runs `advisory`. Policy `segments` has one binding on L3: selector `{}`, both controls `ENFORCE`. A limit of the policy is scoped on segment S. When the ledger could not read the account's segment, that limit answers `REVIEW` with the reason `limit_context_unavailable`, and the transaction is refused. When the account has no segment at all, the segment limit does not apply.

***

## The first published policy

***

The upgrade to the release that introduces policies publishes no policy, and a new tenant database starts without policies. Until a tenant publishes its first policy, the decide flow and `POST /v1/validations` evaluate every active rule and limit, as the reserve flow does.

> [!WARNING]
>
> Plan your first publication. From the moment a tenant publishes its first policy, a rule or limit outside every published policy no longer applies to the decide flow or to `POST /v1/validations`. Put every active rule and limit that must still apply in that first policy. A rule you create or reactivate later applies there only after you add it to a policy and publish that policy. The reserve flow keeps evaluating every active rule and limit.

To add a new rule to a published policy:

1. Read the working set with `GET /v1/policies/{policy_id}`.
2. Send the full list, with the new rule at the position you want, to `PUT /v1/policies/{policy_id}/rules`.
3. Publish with `POST /v1/policies/{policy_id}/publish`.

***

## Error codes

***

| Code | Status | Meaning |
| - | - | - |
| `0604` | 404 | No policy has this id, or it was deleted |
| `0589` | 409 | Another policy that is not deleted holds this name |
| `0590` | 404 | The policy has no revision with this number |
| `0591` | 404 | No binding has this id |
| `0592` | 409 | Another binding of this policy in this ledger holds the position |
| `0593` | 422 | Two bindings of the policy in one ledger have no relative order and can match the same transaction |
| `0594` | 422 | Published policies would hold different versions of the same limit |
| `0595` | 409 | A policy references the rule. Deactivate it instead of deleting it |
| `0596` | 409 | A policy references the limit. Deactivate it instead of deleting it |
| `0597` | 422 | The rule or limit has no draft version to publish |
| `0598` | 400 | The order must list each item exactly once |
| `0599` | 400 | The policy, revision, or binding is invalid |
| `0600` | 409 | Bindings still attach the policy to a ledger. Delete them first |
| `0601` | 409 | A concurrent change to the same policy, rule, or limit won. Nothing was saved. Retry the request |
| `0602` | 409 | A draft is pending, so the change cannot apply at once. Publish the draft, or send the change with `draft=true` |
| `0603` | 409 | The pending draft is not the `draftVersion` you sent, or no draft is pending. Nothing was published |

The complete list is in the [Tracer error list](https://docs.lerian.studio/en/reference/products/tracer/tracer-error-list.md).

***

## Quick reference

***

| Operation | Method | Endpoint |
| - | - | - |
| Publish a rule draft | POST | `/v1/rules/{rule_id}/publish` |
| List rule versions | GET | `/v1/rules/{rule_id}/versions` |
| Publish a limit draft | POST | `/v1/limits/{limit_id}/publish` |
| List limit versions | GET | `/v1/limits/{limit_id}/versions` |
| Create a policy | POST | `/v1/policies` |
| List policies | GET | `/v1/policies` |
| Read a policy and its working set | GET | `/v1/policies/{policy_id}` |
| Rename or describe a policy | PATCH | `/v1/policies/{policy_id}` |
| Delete a policy | DELETE | `/v1/policies/{policy_id}` |
| Set the rules of the working set | PUT | `/v1/policies/{policy_id}/rules` |
| Set the limits of the working set | PUT | `/v1/policies/{policy_id}/limits` |
| Publish a revision | POST | `/v1/policies/{policy_id}/publish` |
| Revert to an earlier revision | POST | `/v1/policies/{policy_id}/revert` |
| List revisions | GET | `/v1/policies/{policy_id}/revisions` |
| Read a revision | GET | `/v1/policies/{policy_id}/revisions/{revision}` |
| Create a binding | POST | `/v1/bindings` |
| List bindings | GET | `/v1/bindings` |
| Effective instruction | GET | `/v1/bindings/effective` |
| Reorder bindings | PUT | `/v1/bindings/order` |
| Read, change, or delete a binding | GET, PATCH, DELETE | `/v1/bindings/{binding_id}` |


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