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

> ## Agent Instructions
> Start with https://docs.scrip.dev/agents.md and https://docs.scrip.dev/which-endpoints-you-need.md. Your application only needs a few API calls as users earn and spend (`POST /v1/events`, balance/history reads, and redemptions); program setup happens in the dashboard or setup scripts.
> Use `https://api-sandbox.scrip.dev` with `sk_test_` keys for testing, and `https://api.scrip.dev` with `sk_live_` keys for production. Keep API keys on your server.
> Pass your own user ID as `external_id` on events and prefix it with `ext:` in participant URL paths (e.g., `GET /v1/participants/ext:user_123/balances`). By default (`on_unknown_participant: CREATE`), the first event for a new `external_id` creates and enrolls the participant automatically.
> Events process asynchronously: `POST /v1/events` returns `202 Accepted` when queued, and rules evaluate in the background. Pass an `idempotency_key` (such as an order ID) on every event, and wait for the `event.completed` webhook or poll `GET /v1/events/{event_id}` before reading updated balances.
> Connect the hosted MCP server at https://mcp.scrip.dev/mcp (OAuth, defaults to Sandbox) to inspect programs, validate and simulate rules, and trace events. Append `.md` to any docs URL for Markdown, or fetch https://docs.scrip.dev/openapi.json for full endpoint schemas.

# Refunds and clawbacks

> Claw back earned rewards on returns using return rules, event reversals, exact debits, or zero-floor caps.

A cardholder returns an item or disputes a charge, and the rewards it earned need to come back. Choose the pattern that matches how your program calculates rewards and whether balances can go negative.

| Approach | Use it when | If the participant already spent the value | What your backend does |
| - | - | - | - |
| [Return rule](#return-rule) | Every award is a flat rate on the purchase amount | Debits in full; balance goes negative (`allow_negative`) | Send a `refund` event with the refund amount and merchant details |
| [Reversal endpoint](#reverse-the-original-event) | A refund must take back what one purchase credited, in full or by one fraction | Recovers what remains in the purchase's lots, reports shortfall, never goes below zero | Find the original event and call `POST /v1/events/{id}/reverse` (`LOT` assets only) |
| [Exact clawback](#exact-clawback) | Refund policy differs per award (e.g., proportional base points, all-or-nothing milestone bonus) | Debits in full; balance goes negative (`allow_negative`) | Read `/impact`, compute each clawback amount, and send a refund event |
| [Zero-floor clawback](#claw-back-without-a-negative-balance) | Balances must never go negative on `SIMPLE` assets or settled pending rewards | Caps the debit at the participant's available balance | Same as exact clawback, using `math.least` in the rule |

Whether a refund should also change state, such as decrementing a visit counter, is your program's policy. Return rules and exact clawbacks change counters and tags only through actions you add to the rule (see [Refund handling](https://docs.scrip.dev/examples/cashback-card.md#refund-handling)). Event reversals report the original event's counter, tag, and tier changes and leave them in place.

Each example below assumes a program (`8f14e45f-ceea-4e70-9df8-2a7d1b3c9f01`) and an asset (`a3bb189e-8bf9-4c8b-9be0-6b5d2f4e7a02`).

## Return rule

A return rule takes back what the refunded amount would have earned as a purchase: a refund event with the same merchant category code (MCC) debits at the same rate. Use it when every award is a rate on the purchase amount, such as 5% on dining, 3% on groceries, and 1% on everything else. Scrip does not link a refund event to the original purchase, and a return rule does not need that link.

| Field | Example | Purpose |
| - | - | - |
| `event.type` | `"refund"` | Distinguishes from a purchase |
| `event.amount` | `85.00` | Amount being refunded (can be partial) |
| `event.mcc` | `"5812"` | Merchant category code, used to match the original earn rate |

Create one return rule per earn rate in the order shown (specific to catch-all) so `stop_after_match` mirrors your earning cascade:

```bash theme={null}
POST /v1/rules
```

```json theme={null}
{
  "program_id": "8f14e45f-ceea-4e70-9df8-2a7d1b3c9f01",
  "name": "refund_dining",
  "stop_after_match": true,
  "condition": "event.type == 'refund' && event.amount > 0 && event.mcc in ['5812', '5813', '5814']",
  "actions": [
    {
      "type": "DEBIT",
      "asset_id": "a3bb189e-8bf9-4c8b-9be0-6b5d2f4e7a02",
      "amount": "${{ event.amount * 0.05 }}",
      "allow_negative": true,
      "description": "Refund of dining 5% cashback"
    }
  ]
}
```

```bash theme={null}
POST /v1/rules
```

```json theme={null}
{
  "program_id": "8f14e45f-ceea-4e70-9df8-2a7d1b3c9f01",
  "name": "refund_grocery",
  "stop_after_match": true,
  "condition": "event.type == 'refund' && event.amount > 0 && event.mcc in ['5411', '5422']",
  "actions": [
    {
      "type": "DEBIT",
      "asset_id": "a3bb189e-8bf9-4c8b-9be0-6b5d2f4e7a02",
      "amount": "${{ event.amount * 0.03 }}",
      "allow_negative": true,
      "description": "Refund of grocery 3% cashback"
    }
  ]
}
```

```bash theme={null}
POST /v1/rules
```

```json theme={null}
{
  "program_id": "8f14e45f-ceea-4e70-9df8-2a7d1b3c9f01",
  "name": "refund_base",
  "condition": "event.type == 'refund' && event.amount > 0",
  "actions": [
    {
      "type": "DEBIT",
      "asset_id": "a3bb189e-8bf9-4c8b-9be0-6b5d2f4e7a02",
      "amount": "${{ event.amount * 0.01 }}",
      "allow_negative": true,
      "description": "Refund of base 1% cashback"
    }
  ]
}
```

**How it works:** A \$40 partial refund on a dining purchase debits `40 * 0.05` = \$2.00. `"allow_negative": true` allows the debit to succeed even if the participant already spent the cashback (taking their balance negative until future earnings repay it).

Because a return rule recalculates from the refund event rather than looking up the original purchase, use the [reversal endpoint](#reverse-the-original-event) or [exact clawback](#exact-clawback) if your program has budgets, promotions, or state-dependent bonuses where the refund must take back what the original purchase actually earned.

## Reverse the original event

Use the reversal endpoint when a refund maps cleanly onto one event: a full refund, or a partial refund where every award scales by the same fraction. It takes back what the event credited, up to what remains in the lots it created, whatever budget or promotion applied; anything spent, expired, or transferred is reported as shortfall rather than forced, so the balance never goes below zero.

Find the original event by its idempotency key, then reverse it:

```bash theme={null}
GET /v1/events?program_id=8f14e45f-ceea-4e70-9df8-2a7d1b3c9f01&idempotency_key=order-1001-completed
```

```bash theme={null}
POST /v1/events/e5dc1b6a-7f3d-4e2a-8c1b-9a0f4d7e6c05/reverse
```

```json theme={null}
{
  "idempotency_key": "refund-order-1001-1",
  "fraction": "0.5",
  "reason": "partial refund"
}
```

Omit `fraction` to reverse everything that remains. Cumulative reversals are capped at each original award, so repeated partial refunds cannot over-claw.

The API returns `201 Created`. Each entry in `lines` is one original award: here the award credited `54.25`, half is `27.13`, and the participant still held `20`:

```json theme={null}
{
  "id": "9e3c5b71-6d28-4a94-8f1e-0c7b2a4d6e12",
  "event_id": "e5dc1b6a-7f3d-4e2a-8c1b-9a0f4d7e6c05",
  "idempotency_key": "refund-order-1001-1",
  "fraction": "0.500000000000000000",
  "shortfall_policy": "report",
  "reason": "partial refund",
  "status": "COMPLETED",
  "created_at": "2027-02-01T09:00:00Z",
  "lines": [
    {
      "original_journal_entry_id": "657a0312-1aaa-4ba7-842b-edfbb39edf14",
      "ledger_entity_id": "5e7a9c13-2d4f-4b68-9a0c-e1b3d5f7a920",
      "asset_id": "a3bb189e-8bf9-4c8b-9be0-6b5d2f4e7a02",
      "original_amount": "54.25",
      "requested_amount": "27.13",
      "recovered_amount": "20",
      "shortfall_amount": "7.13",
      "reversal_journal_entry_id": "657a0312-1aaa-4ba7-842b-edfbb39edf15"
    }
  ],
  "state_changes": []
}
```

`state_changes` lists the original event's counter, tag, and tier changes, which the reversal leaves in place.

Reversal requires a `LOT`-mode asset (`SIMPLE`-mode assets return `422 nothing_reversible`). A settlement credit that settled pending rewards is also ineligible: it appears in `ineligible_entries`, and an event with nothing else to reverse returns `422 nothing_reversible`. See [Reversing an event](https://docs.scrip.dev/guides/event-processing.md#reversing-an-event) for the full contract.

## Exact clawback

Use exact clawback when refund policy differs per award, so a uniform reversal fraction is not enough and a return rule would take back the wrong amount. Your backend reads what the original event credited, applies your policy per asset, and sends a refund event with the exact amounts.

Find the original event by idempotency key and read its impact:

```bash theme={null}
GET /v1/events?program_id=8f14e45f-ceea-4e70-9df8-2a7d1b3c9f01&idempotency_key=order-1001-completed
GET /v1/events/e5dc1b6a-7f3d-4e2a-8c1b-9a0f4d7e6c05/impact
```

`balance_impact` reports the net effect per entity, asset, and bucket. For an event whose rules only credited, the participant's row is what was earned (including milestone or threshold awards). If the original event mixed credits and debits, derive the earned amount from the positive postings on `CREDIT` journal entries in `journal_entries` instead:

```json theme={null}
{
  "balance_impact": [
    {"entity_type": "PARTICIPANT", "entity_id": "f7a8b9c0-1d2e-4f3a-b4c5-d6e7f8a9b006", "asset_symbol": "PTS", "bucket": "AVAILABLE", "amount": "54.25"}
  ]
}
```

Send the refund event with the computed clawback amount:

```bash theme={null}
POST /v1/events
```

```json theme={null}
{
  "program_id": "8f14e45f-ceea-4e70-9df8-2a7d1b3c9f01",
  "external_id": "user_123",
  "idempotency_key": "refund-order-1001-1",
  "event_timestamp": "2027-02-01T09:00:00Z",
  "event_type": "refund_exact",
  "event_data": {
    "order_id": "order-1001",
    "clawback_amount": 54.25
  }
}
```

One rule handles every rate and award, with no cascade to maintain:

```bash theme={null}
POST /v1/rules
```

```json theme={null}
{
  "program_id": "8f14e45f-ceea-4e70-9df8-2a7d1b3c9f01",
  "name": "refund_exact",
  "condition": "event.type == 'refund_exact' && event.clawback_amount > 0",
  "actions": [
    {
      "type": "DEBIT",
      "asset_id": "a3bb189e-8bf9-4c8b-9be0-6b5d2f4e7a02",
      "amount": "${{ event.clawback_amount }}",
      "allow_negative": true,
      "description": "Refund of recorded award"
    }
  ]
}
```

The refund event above debits `54.25` from `user_123`, even when that takes the balance below zero.

> [!WARNING]
>
> Track cumulative clawbacks per original order in your backend and cap them at the recorded credit. A refund event is not linked to the purchase it corrects, so repeated partial refunds can otherwise claw back more than was earned. To have Scrip enforce that cap automatically, use the [reversal endpoint](#reverse-the-original-event).

> [!NOTE]
>
> On `LOT` assets this `DEBIT` consumes the oldest available lots first, not specifically the lots the original purchase created: the total corrects exactly, but remaining expiration dates can shift (see [Oldest-first spending](https://docs.scrip.dev/guides/lots-and-expiration.md#oldest-first-spending)). The [reversal endpoint](#reverse-the-original-event) targets the original award's lots directly.

## Claw back without a negative balance

Some programs must never show a negative balance. The [reversal endpoint](#reverse-the-original-event) never overdraws, but it requires a `LOT`-mode asset and cannot reverse a settlement credit that settled pending rewards. For those cases, cap the debit at what the participant still holds by replacing `refund_exact` with this rule:

```bash theme={null}
POST /v1/rules
```

```json theme={null}
{
  "program_id": "8f14e45f-ceea-4e70-9df8-2a7d1b3c9f01",
  "name": "refund_exact_capped",
  "condition": "event.type == 'refund_exact' && event.clawback_amount > 0 && participant.balance.PTS > 0",
  "actions": [
    {
      "type": "DEBIT",
      "asset_id": "a3bb189e-8bf9-4c8b-9be0-6b5d2f4e7a02",
      "amount": "${{ math.least(event.clawback_amount, participant.balance.PTS) }}",
      "description": "Refund of recorded award, up to the available balance"
    }
  ]
}
```

`participant.balance.PTS` is the participant's available `PTS` balance at event start. With `20.00` available, a refund event with `"clawback_amount": 54.25` debits `20.00` and leaves the balance at `0` (the remaining `34.25` is not recorded as debt). Keep `participant.balance.PTS > 0` in the condition so the rule skips when the balance is already `0`.

> [card] [Reversing an event](https://docs.scrip.dev/guides/event-processing.md#reversing-an-event)
>
> Full contract for lot-level reversals, shortfalls, and ineligible entries.

> [card] [Pending rewards](https://docs.scrip.dev/guides/pending-rewards.md)
>
> Hold awards during a return window so you can cancel instead of clawing back.

> [card] [Common patterns](https://docs.scrip.dev/examples/common-patterns.md)
>
> More copy-paste recipes for earn rates, bonuses, streaks, and tiers.


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