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

# Units

> Anything a user has, counts or holds: points, XP, levels, badges, perks, roles.

A **unit** is one kind of thing a person can have: Beans, XP, a level, a streak, a badge, a free drink, a ticket, a role. Rules change them, and every change leaves a [receipt](https://docs.getlurk.com/activity.md).

A unit has a `group` (`points`, `progress`, `badge` or `perk`), a name, and **switches** that decide what it can do. Make one with `POST /v1/units`, change one with `PATCH /v1/units/{id}`. A change to a switch applies from then on and never changes anybody's balance.

```json theme={null}
{ "name": "Beans", "group": "points", "switches": { "goes_down": true, "spendable": true } }
```

## What each switch does

| Switch | What it does |
| - | - |
| `goes_up`, `goes_down` | Rules can add to it, or take from it. Off for `goes_down` makes it earn-only (XP, a level). |
| `cap`, `min_balance` | The most a person can hold (a badge is 1), and the lowest a balance can go (0 normally). |
| `spendable` | `units.spend` can take it: your server taking value for a reward. [Spending](#spending-and-using) |
| `usable` | `units.use` can take one: a person redeeming the thing itself, a coupon or a ticket. |
| `sendable`, `send_cap_per_day` | People can send it to each other, up to a daily limit. [Sending](#sending-between-people) |
| `resets` | A rule can set it back to 0. [Resets](https://docs.getlurk.com/unit-limits.md#resets) |
| `starts_at`, `held_by` | A level that starts at 1, or a pass that follows an audience. [Levels and passes](https://docs.getlurk.com/unit-patterns.md) |
| `supply` | The most that can ever be given, in all. [Supply](https://docs.getlurk.com/unit-limits.md#a-limited-supply) |
| `expires_after_days`, `expires_at` | What is given expires, or the whole unit ends. [Expiry](https://docs.getlurk.com/unit-limits.md#expiry) |
| `visible` | A label for your own app: Lurk does not act on it. `shows_as` and `color` are on [Levels, passes and appearance](https://docs.getlurk.com/unit-patterns.md#how-it-looks). |

An earn-only unit (it cannot go down) is never spendable, usable or sendable: turning `goes_down` off turns those off with it, and turning one of them on needs `goes_down`.

## Spending and using

Two ways to take a unit from a person, for two different things. Both need a unit that can go down (an earn-only unit has nothing to take) and are all or nothing: if the person holds less than the amount above the unit's lowest balance, the call is `insufficient_balance` and nothing is taken. Both take a `reference` of your own (an order or scan ID), which is optional and can be used **once per unit**: a second use with it is `reference_used`, and a retry of the same request returns the first answer.

* **Spend** (`POST /v1/units/spend`) is your server taking value for a reward: "a free coffee for 100 Beans". It needs a `reason`. The receipt has `cause: "spend"`.
* **Use** (`POST /v1/units/use`) is a person redeeming the thing itself: a coupon at the till, a ticket at the door. It takes one by default. The receipt has `cause: "use"` and the webhook `unit.used` fires.

```bash theme={null}
curl https://api.getlurk.com/v1/units/use \
  -H "Authorization: Bearer $LURK_KEY" -H "Content-Type: application/json" \
  -H "Idempotency-Key: scan-8841" \
  -d '{ "user": "maya@example.com", "unit": "coupon", "reference": "scan-8841", "reason": "Table 4" }'
```

What expires first is taken first. Your server does both with a project key; in the dashboard only an owner or admin can. Neither can be done by an agent.

## Sending between people

A unit with `sendable` on can be moved from one person to another with `POST /v1/units/send`: two receipts, `send` and `receive`, that share a `transfer` ID and happen together or not at all.

```json theme={null}
{ "from": "maya@example.com", "to": "arjun@example.com", "unit": "stars", "amount": 20, "reason": "Thanks" }
```

* The sender must hold the amount above the unit's lowest balance, and the receiver must be able to hold it (the cap).
* A person can send at most `send_cap_per_day` of it a day (100 unless you change it), counted in your project's time zone. Past it the call is `rate_limited`.
* Both people must already be known to Lurk.
* What expires keeps its expiry: the receiver gets the sender's clock.
* A send makes nothing. It is not counted as given or spent, so a thank-you never looks like issuing. Find the two receipts with `GET /v1/activity?transfer=<id>`.
* A send cannot be undone from one side. The way back is a send the other way.
* A unit is **off by default**: nothing can be sent until you turn `sendable` on for that unit.
* Units cannot be bought, cashed out or given a money value, and sending does not change that.

## Giving by hand

An owner or admin can give the same amount of one unit to up to 200 people at once, in the dashboard. A project key and an agent cannot; Lurky can propose a give as a change card, and a person applies it.

* People are `usr_…` IDs or your own IDs, each counted once, and each gets one receipt with the optional `note`. An ID that matches nobody is returned in `not_found`, and nothing is created for it.
* The unit's own settings apply. Someone it cannot be given to (a cap it would pass, for example) is skipped with the reason, and the others still get theirs.
* `count_against` names a rule with a budget in this unit. The give is counted in that budget, and if it would pass it, nothing is given.
* `POST /v1/units/give/preview` takes the same input and writes nothing: it says who would get the unit, who would be skipped and why, and what is left of the budget. A project key and an agent can call it.

## The leaderboard

`GET /v1/units/{unit}/leaderboard` lists the top of one unit, most first: up to `limit` people (1 to 10, 10 if you leave it out). Each row has a `rank`, the person's `usr_…` ID and the `amount`; people with the same amount share a rank (1, 1, 3). Only people holding more than 0 are listed, and a person merged into another is not (what they held moved to the person kept). It never carries your own identifier for a person or an email, so it is safe to show to your own users. The agent tool may leave the unit out: it then uses the oldest unit that someone holds, or the oldest unit if nobody holds any.

## Audiences

A list, or a condition that keeps itself up to date. [Audiences](https://docs.getlurk.com/audiences.md) has the conditions, the refresh and the limits.


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