> ## 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.

# Set up rules and spending limits (API)

> Create the rules and spending limits that Tracer decides with, activate them, and read the usage of a limit.

Create the rules and spending limits that Tracer decides with, and turn them on.

## Participants

***

* **Your institution.** Your system writes your policy into Tracer as rules and spending limits.
* **Tracer.** Stores the rules and limits, and uses the ACTIVE ones in each decision.

## Before you start

***

* Your system can call the Tracer API with a valid credential. See the [Tracer integration guide](https://docs.lerian.studio/en/products/tracer/integration-guide.md).
* You know your policy: the condition and the action of each rule, and the amount, period and scope of each limit. See [rules engine](https://docs.lerian.studio/en/products/tracer/rule-engine.md) and [spending limits](https://docs.lerian.studio/en/products/tracer/spending-limits.md).

## Diagram

***

![Set up rules and spending limits](https://mintcdn.com/lerian-49cb71fc/KypD0TIKUCqZ014J/images/en/d2/id-tracer-set-up-rules-and-limits.svg?fit=max\&auto=format\&n=KypD0TIKUCqZ014J\&q=85\&s=9f9995ea432e4c0c15e16d2bcded42de)

## Steps

***

| # | From | To | Message | Lerian API | What happens |
| - | - | - | - | - | - |
| 1 | Your institution | Tracer | Lerian API | [`POST /v1/rules`](https://docs.lerian.studio/en/reference/products/tracer/create-rule) | Create a rule with a name, a CEL expression, an action and optional scopes. |
| 2 | Tracer | Your institution | Lerian API | Response to step 1 | Tracer compiles the expression and returns the rule in `DRAFT` with `201`. |
| 3 | Your institution | Tracer | Lerian API | [`POST /v1/rules/{rule_id}/activate`](https://docs.lerian.studio/en/reference/products/tracer/activate-rule) | Activate the rule. |
| 4 | Tracer | Your institution | Lerian API | Response to step 3 | Tracer returns the rule in `ACTIVE`. |
| 5 | Your institution | Tracer | Lerian API | [`POST /v1/limits`](https://docs.lerian.studio/en/reference/products/tracer/create-limit) | Create a spending limit with a period, a maximum amount, an asset and scopes. |
| 6 | Tracer | Your institution | Lerian API | Response to step 5 | Tracer returns the limit in `DRAFT` with `201`. |
| 7 | Your institution | Tracer | Lerian API | [`POST /v1/limits/{limit_id}/activate`](https://docs.lerian.studio/en/reference/products/tracer/activate-limit) | Activate the limit. |
| 8 | Tracer | Your institution | Lerian API | Response to step 7 | Tracer returns the limit in `ACTIVE`. |
| 9 | Your institution | Tracer | Lerian API | [`GET /v1/limits/{limit_id}/usage`](https://docs.lerian.studio/en/reference/products/tracer/retrieve-limit-usage) | Read the usage of the limit. |
| 10 | Tracer | Your institution | Lerian API | Response to step 9 | Tracer returns the current usage, held amounts included, and the limit amount. |

## How you know it finished

***

The rule and the limit show `ACTIVE`.

## Rules

***

* The period of a limit is `DAILY`, `WEEKLY`, `MONTHLY`, `PER_TRANSACTION` or `CUSTOM`. A scope names an account, a segment, a portfolio, a merchant, a scheme or a `subType`.

## What can go wrong

***

| Situation | What you see | What to do |
| - | - | - |
| The CEL expression does not compile. | An error response to step 1. | Correct the expression and create the rule again. |
| You activate a rule or a limit that you deleted. | An error response to step 3 or 7. | Create a new rule or limit. |

## Related

***

* [Validate a transaction before the ledger](https://docs.lerian.studio/en/diagrams/lerian-products/tracer-validate-before-ledger.md)
* [Check Midaz transactions with Tracer](https://docs.lerian.studio/en/diagrams/lerian-products/tracer-check-ledger-transactions.md)
* [Rules engine](https://docs.lerian.studio/en/products/tracer/rule-engine.md)
* [Spending limits](https://docs.lerian.studio/en/products/tracer/spending-limits.md)


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