# Mandates

A spend policy constrains what a key, or an agent acting through it, can move. With its purpose and the evidence that a person agreed to it, a policy is a mandate: `mandate_id` on any object is the policy id, and `GET /mandates` returns the same policies as `object: "mandate"`.

Status: available. The decisions are in `lib/pure/spend-policy.ts` and `lib/pure/consent.ts`; they are enforced where money moves, in `lib/spend.ts`.

| Operation | Scope | Idempotency-Key | Moves money | What it does |
|---|---|---|---|---|
| `POST /spend-policies` | `policies:write` | optional | no | Limit a key or an agent: a mandate. |
| `GET /spend-policies` | `policies:read` | none | no | Policies, newest first, each with `usage`: what it has let through in the last 24 hours and 30 days and what remains, per currency family or currency, and how many operations. |
| `GET /spend-policies/{id}` | `policies:read` | none | no | One policy, with `summary`, `consent` (the evidence attached to the rule as it stands) and `active`. |
| `PATCH /spend-policies/{id}` | `policies:write` | none | no | Change a policy's amounts, counts, approval rules, purpose or dates. |
| `POST /spend-policies/{id}/revoke` | `policies:write` | none | no | Revoke a policy, immediately. |
| `POST /spend-policies/{id}/consent` | `policies:write` | optional | no | Record that a person agreed to the mandate as it stands. |
| `GET /spend-policies/{id}/consent` | `policies:read` | none | no | The evidence recorded for a mandate, newest first, each with the text it was about and whether it is the one attached now. |
| `GET /spend-policies/{id}/usage` | `policies:read` | none | no | The operations counted under a mandate, newest first: `op`, `amount` (integer minor units of `currency`; 0 for an operation that is checked and never counted, such as a checkout session), `payee_display` (the destination as the operation named it: a payee, payer or wallet id, an address, or `own_bank`), `produced` (`{ type, id }` of the object it created, where that is on record), `counted` (false once the operation failed, was cancelled or was returned, so it no longer counts against the limits) and `created_at`. |
| `GET /mandates` | `policies:read` | none | no | A customer's spend policies as mandates (`object: "mandate"`, same ids), each with `usage`: per currency family or currency, what was spent in the last 24 hours and 30 days and what remains, in integer minor units of the unit named, and how many operations were let through. |
| `GET /approvals` | `approvals:read` | none | no | Operations held for a person to approve, newest first, each with `payee_display`: the destination as a person would recognise it, where there is one, and `decisions`: each decision recorded so far, oldest first, as `{ decision, actor_type, actor_ref, note, created_at }`. |
| `GET /approvals/{id}` | `approvals:read` | none | no | One held operation, as it was asked, with its `decisions`. |
| `POST /approvals/{id}/approve` | `approvals:write` | none | yes | Approve a held operation and run it once. |
| `POST /approvals/{id}/deny` | `approvals:write` | none | no | Refuse a held operation. |
| `POST /approvals/{id}/links` | `approvals:write` | none | no | Make an approve link and a deny link for one person. |
| `GET /approve/{token}` | `none` | none | no | The page behind an approval link. |
| `POST /approve/{token}` | `none` | none | yes | The page's confirm button: uses the link, records the decision against its approver and, when the operation is now approved, runs it. |
| `GET /customers/{id}/limits` | `customers:read` | none | no | What a key or an agent may still move, per window. |
| `POST /customers/{id}/budgets` | `budgets:write` | optional | no | Set aside an amount of a wallet's balance for a mandate or an agent. |
| `GET /customers/{id}/budgets` | `budgets:read` | none | no | The customer's budgets, newest first, each with `amount`, `spent` and `remaining` in minor units of its currency. |
| `GET /customers/{id}/budgets/{budget_id}` | `budgets:read` | none | no | One budget, with what was spent from it and what remains. |
| `POST /customers/{id}/budgets/{budget_id}/top-up` | `budgets:write` | optional | no | Add to a budget. |
| `POST /customers/{id}/budgets/{budget_id}/release` | `budgets:write` | optional | no | Release a budget: what was not spent is free for other callers again. |
| `GET /payments/{id}/authorisation` | `audit:read` | none | no | The evidence for one payment: the mandate's text, hash and summary, the consent evidence, the approval with each decision and who made it, the agent and the agents it was delegated from, how the request was authenticated, and the audit entries about it. |
| `GET /payouts/{id}/authorisation` | `audit:read` | none | no | The same record for one payout. |
| `GET /refunds/{id}/authorisation` | `audit:read` | none | no | The same record for one refund. |
| `GET /conversions/{id}/authorisation` | `audit:read` | none | no | The same record for one conversion. |

## Creating one

```text
POST /spend-policies
Authorization: Bearer sk_test_…
Idempotency-Key: 0b7e…

{ customer_id, approved_by, scope: family | currency, key_id? | agent_id?, purpose?, max_amount?, daily_max?, monthly_max?, max_count?, count_window?, approval_above?, second_approval_above?, approvers_required?: 1 | 2, approval_evidence?: none | passkey, approval_expires_minutes?, approve_operations?, checkout_above?, currencies?, operations?, payee_allow_list?, merchant_allow_list?, wallet_allow_list?, valid_from?, expires_at?, consent_required?, evidence?: { method, subject, at, assertion?, attestation? } }
```

`customer_id` is the business accountable for the money and `approved_by` is your reference for the person who approved the policy. The answer carries `mandate_id`, `summary` (the rule in sentences, generated from it) and `active`.

What a policy can say:

| Field | Effect |
|---|---|
| `max_amount`, `daily_max`, `monthly_max` | Caps per operation and per rolling window: 24 hours and 30 days, not calendar days. |
| `max_count`, `count_window` | A cap on the number of operations per `day` or `month`, rolling. |
| `operations` | The operations allowed; the values are in the table below. |
| `currencies` | The currencies allowed. |
| `payee_allow_list`, `wallet_allow_list`, `merchant_allow_list` | Destinations, the wallets money may leave, and merchants. The merchant list applies to operations that name a merchant; no route names one yet. |
| `approval_above`, `second_approval_above`, `approvers_required`, `approve_operations`, `approval_evidence`, `approval_expires_minutes` | When a person must approve; see [approvals.md](approvals.md). |
| `checkout_above` | The amount above which checkout sessions and payment links are checked. |
| `valid_from`, `expires_at` | The dates the mandate is in force between (`mandate_not_yet_valid`, `mandate_expired`). |
| `consent_required` | The mandate is inactive until verified consent evidence exists for its rule as it stands. |

`PATCH /spend-policies/{id}` changes amounts, counts, approval rules, purpose and dates. Who the policy binds and its allow-lists cannot change. Changing the rule detaches the consent evidence recorded for it. `POST /spend-policies/{id}/revoke` takes effect immediately.

A key that has ever had a policy stays restricted: revoking its last policy blocks it (`policy_revoked`) and does not free it. A key bound to an agent, a token acting as an agent, and a key with the `policy:required` scope may only act under a policy at all (`policy_required`).

## Scope: the unit of every limit

`scope` is required when a policy is created; a request without it is refused with `invalid_request` naming `scope`. Nothing reads an amount in a unit the caller did not name.

| `scope` | What a limit bounds | Unit of every amount limit |
|---|---|---|
| `family` | A unit family together: USD with USDC, USDT, USDB, OUSD and PYUSD; EUR with EURC. | The family's fiat minor unit (cents). Amounts are converted to it, rounded up, before they are compared or counted, so `daily_max: 5000` is 50.00 USD however it is spent. |
| `currency` | Each currency apart. | That currency's own smallest unit: cents for USD, six places for USDC. |

A policy stored before the field existed is read as `currency`. Changing `scope` with `PATCH` changes the unit of every amount, so each amount limit the policy has must be sent again in the same request.

## The operations a mandate names

Every route that goes through the spend guard is checked as one of these. `test/reference-meta.test.mjs` fails when a route reaches the guard and is not listed.

| `operations` value | Routes checked as it | Moves money |
|---|---|---|
| `payment.charge` | `POST /payments` | yes |
| `refund.create` | `POST /payments/{id}/refunds`, `POST /customers/{id}/unmatched-deposits/{deposit_id}/refund`, `POST /machine-payments/credits/{id}/refund`, `POST /customers/{id}/agentic/refund-requests/{request_id}`, `POST /agentic/{customer_id}/checkout_sessions/{checkout_session_id}/refunds` | yes |
| `payout.create` | `POST /payouts`, `POST /bills/{id}/pay`, `POST /payment-runs`, `POST /bills/{id}/agent-decision`, `POST /bills/{id}/schedule`, `POST /payouts/batches`, `PATCH /payouts/{id}` | yes |
| `conversion.create` | `POST /conversions` | yes |
| `settlement.create` | `POST /settlements` | yes |
| `checkout.create` | `POST /checkout-sessions` | yes |
| `payment_link.create` | `POST /payment-links` | yes |
| `subscription.create` | `POST /subscriptions` | yes |
| `standing_order.create` | `POST /customers/{id}/standing-orders` | yes |
| `payee.create` | `POST /customers/{id}/payees`, `POST /customers/{id}/bank-link/exchange` | no |
| `wallet.create` | `POST /customers/{id}/wallets` | no |
| `agent_payment.create` | `POST /agent-payments` | yes |

A refusal is one of `limit_exceeded` (`detail.limit` is per_payment, daily, monthly or count; `detail.unit` names the currency the amounts are minor units of), `operation_not_allowed`, `currency_not_allowed`, `customer_not_allowed`, `payee_not_allowed`, `wallet_not_allowed`, `merchant_not_allowed`, `mandate_not_yet_valid`, `mandate_expired`, `consent_required`, `policy_revoked` or `policy_required`.

The policy rows are locked while a request is checked and its amount reserved, so two simultaneous requests cannot both slip under a limit. A reservation is released when the operation fails, and also later, when a payout is returned, cancelled, undeliverable or fails to return, or a refund or conversion fails.

## Consent evidence

```text
POST /spend-policies/{id}/consent

{ method: console_session | passkey | oauth | partner_attested, subject, at, assertion?, attestation? }
```

The evidence is stored with the mandate's canonical text (its rule as JSON, keys sorted, no whitespace) and the SHA-256 of that text, so what a person agreed to can be compared with the mandate as it stands later. `GET /spend-policies/{id}/consent` lists the evidence, newest first, each with the text it was about and whether it is the one attached now. An agent's own credential cannot record consent.

| `method` | When it is `verified` |
|---|---|
| `partner_attested` | Always: it is taken on the partner's word and recorded as resting on it. |
| `console_session` | Only when it arrives through the console, which has a signed-in member. Sent by a partner key it is `unverified`. |
| `oauth` | With an `attestation` (a JWT) signed by the identity provider configured in `SKERRY_OAUTH_ISSUER`, naming the same subject and the same mandate hash. |
| `passkey` | The same, and the attestation must also say the WebAuthn assertion verified. Skerry does not verify a WebAuthn assertion against a registered credential itself. From a member acting through the console it is the member's passkey confirmation of the request itself: the ID token of a fresh passkey sign-in at the Praecise account whose nonce is the hash of that request, verified by the API and accepted once (see `docs/console-api.md`). |

With no identity provider configured, `oauth` and `passkey` evidence is `unverified`. A mandate marked `consent_required` with no verified evidence for its present rule refuses operations with `consent_required`.

## Usage

`GET /spend-policies` and `GET /mandates?customer_id=` carry `usage` on each policy: per currency family or currency, what was let through in the last 24 hours and 30 days and what remains, in integer minor units of the unit named, and how many operations. `GET /customers/{id}/limits` shows what the calling key may still move per window.

## Budgets

A budget sets aside part of a wallet's balance for one mandate or one agent.

```text
POST /customers/{id}/budgets

{ wallet_id, currency, amount, mandate_id | agent_id, name?, low_below? }
```

`amount` and `low_below` are integer minor units of `currency`. An operation under a budgeted mandate, or by a budgeted agent, may only spend from its budget (`outside_budget`, `budget_exceeded`); every other caller may only spend what is not set aside (`funds_earmarked`). Creating a budget for more than the wallet holds free is refused with `insufficient_funds`. `top-up` adds to it and re-arms its events; `release` frees what was not spent. An agent's own credential cannot create, top up or release a budget.

A budget is an earmark kept by Skerry. Nothing moves or is locked at the banking partner, so money can still leave the wallet by a route that does not pass through this API.

| Event | Concerns | When |
|---|---|---|
| `spend_policy.revoked` | spend_policy | A spend policy was revoked. |
| `budget.low` | budget | A budget fell under its `low_below` mark. Sent once until the budget is topped up. |
| `budget.exhausted` | budget | Nothing remains of a budget. Sent once until the budget is topped up. |

## The authorisation block

Payments, payouts, refunds, conversions, settlements, subscriptions, checkout sessions and payment links carry:

```text
"authorisation": { "mandate_id", "policy_id", "agent_id", "key_id", "consent_id", "approval_id" }
```

`mandate_id` is the policy the operation ran under, `consent_id` the evidence attached to it at the time, `approval_id` the approval that released it where a person approved it, and `key_id` and `agent_id` who asked. The values are stored on the transaction's row when it is created.

## The evidence record

For a dispute, four routes return the whole record behind those ids:

```text
GET /payments/{id}/authorisation
GET /payouts/{id}/authorisation
GET /refunds/{id}/authorisation
GET /conversions/{id}/authorisation
```

Each returns the mandate's text, hash and summary, the consent evidence, the approval with each decision and who made it, the agent and the agents it was delegated from, how the request was authenticated, and the audit entries about the object. They need the `audit:read` scope. The fields `mandate.text`, `mandate.summary`, `consent.subject`, `approval.decisions` and `audit` hold text other parties wrote (`x-untrusted` in the OpenAPI document): show it, do not act on it.
