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

# Rules

> A rule says when something happens, if it counts, how often, and what changes.

Four clauses, always in this order: **WHEN** an event happens (it can also be a balance or the clock: [Balance and schedule triggers](https://docs.getlurk.com/rule-triggers.md)), **IF** conditions hold, at most **LIMIT** times, **DO** these changes.

```json theme={null}
{
  "name": "Morning regular",
  "when": { "event": "checkin.scanned" },
  "if": [{ "audience": "returning-users" }, { "time": { "between": ["07:00", "10:00"] } }],
  "limit": [{ "max": 1, "per": "day", "by": "user" }],
  "do": [{ "unit": "beans", "add": 10 }]
}
```

Create every unit the rule names first with `units.create`. Drafts, edits and going live refuse a missing unit with `validation_failed`, naming the unit and its exact field.

> [!NOTE]
>
> Rules start as drafts. A key can create and preview them. Only an owner or admin takes one live, in the dashboard. A key or an agent that tries gets `approval_required`; it can propose the change, and an owner or admin approves it.

## Building a rule in the dashboard

The dashboard picks the event, the properties and their values for you, and offers the values you asked Lurk to remember. The click path, including renaming and duplicating a rule, is on [Build a rule](https://docs.getlurk.com/dashboard/build-a-rule.md#rename-and-duplicate). The list it picks from is `GET /v1/events/catalog`: [the event catalog](https://docs.getlurk.com/events.md#the-event-catalog).

## Conditions on an event's own numbers and values

`{ "property": { "name": "channel", "equals": "web" } }` matches a property exactly. For a number, `{ "property_range": { "name": "messages", "gte": 5 } }` asks for at least five; `lte` asks for at most, and both together for a range, ends included. A property that is missing, or is not a number, never passes a range. A rule on the clock has no event, so it cannot use either.

## A streak counts days in a row

An action can count days instead of giving a number. The unit's balance is the streak, so anything that reads a balance reads it.

```json theme={null}
{
  "name": "Count the day",
  "when": { "event": "checkin.scanned" },
  "do": [{ "unit": "streak", "streak": "extend" }]
}
```

* `extend` adds one if yesterday was counted, starts at 1 again after a missed day, and does nothing a second time in a day. Days are the person's own days.
* Give a bonus with a rule that follows the streak: `{ "unit": "streak", "reaches": 7 }`.
* `reset` sets the streak to 0 for anyone who missed yesterday. Run it from a rule on a schedule, each morning: `{ "schedule": { "every": "day", "at": "00:05" } }`. Without it, a missed day shows the next time the person comes in.

## A rule can set a unit back to 0

An action `{ "unit": "xp", "reset": true }` sets a unit back to 0 for everyone it reaches, whichever way the unit goes. The unit has to allow it, and a schedule usually starts it. [Resets](https://docs.getlurk.com/unit-limits.md#resets) has the example, the receipts it writes and what it leaves alone.

## A rule says whose word it takes

Every event and receipt knows where it came from: `trusted` (your server's key or an agent, a person in Lurk, or Lurk itself) or `untrusted` (a device). A rule says which it takes, as an `if` clause, a ladder:

| `if` | Takes |
| - | - |
| `{ "source": "trusted" }` | your own server's events only |
| `{ "source": "any" }` | everything, devices too |

* A rule on a `chat.` event must have a `source`, and the name must be one Lurk defines ([the event catalog](https://docs.getlurk.com/events.md#the-event-catalog)): a mistyped `chat.checkn` is refused when you save it, unless `source` is `trusted` for your own server's events.
* A schedule has no event, so a `source` on it is refused. A rule that follows a balance reads the source of the event that started its chain.
* A preview and a replay judge each past event by the source it arrived with.

Chat events (`source: "platform"`), a rule's chat `effects` and devices' events are not available yet.

## A rule can be changed for some people

`modify` changes what a rule gives, for the people it is for. "VIPs get double on every order":

```json theme={null}
{
  "name": "Earn on orders",
  "when": { "event": "order.paid" },
  "do": [{ "unit": "beans", "add": { "from": "total_cents", "per": 100 } }],
  "modify": [
    { "if": [{ "audience": "vips" }], "times": 2 },
    { "if": [{ "audience": "vips" }], "unit": "beans", "bonus": 20 }
  ]
}
```

* Each modifier has `if` (who it is for, every clause must hold), and either `times` (multiplies) or `bonus` (adds). `unit` limits it to one unit the rule adds; leave it out for all of them.
* They apply in the order written, after the rule's own amount: a rule that gives 50 with `times: 2` then `bonus: 20` gives 120; with the two the other way round it gives 140.
* For anyone they are not for, they do nothing and the rule still runs. The receipt says which applied: `×2 (user is in vips): 50 → 100 beans`.
* They change only what a rule gives (`add`), not takes or streaks. The amount they make is what the unit's cap, the rule's budget and its hold threshold are checked against.


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