# Automatic top-up

An owner can let Atomside buy a pack of credits on the account's card when the credits that can be spent fall below a level they choose. Your agent can read whether it is on and what is left of the month's ceiling, so it can plan a run and explain a wait. It cannot turn it on, change it, or buy credits: no tool, REST route or CLI command does that, and no scope grants it.

## Read it on get_account

`get_account` has `credits.auto_top_up`:

```json
"auto_top_up": {
  "state": "on",
  "real": true,
  "when_below": { "credits": 100, "gbp": "10.00" },
  "adds": { "credits": 800, "gbp": "80.00" },
  "left_this_month": { "purchases": 1, "credits": 800, "gbp": "80.00" },
  "resets_at": "2026-12-01T00:00:00.000Z",
  "next_possible_at": null,
  "can_change": false,
  "can_trigger": false,
  "says": "If fewer than 100 credits can be spent, 800 more may be bought automatically, once more before 1 Dec. It can fail. You cannot buy credits or change this."
}
```

| `state`                                | What it means                                                                            |
| -------------------------------------- | ---------------------------------------------------------------------------------------- |
| `off`                                  | Nothing is bought automatically. When credit runs out, letters wait for a person.        |
| `on`                                   | A pack may be bought when fewer than `when_below` can be spent.                          |
| `buying`                               | A pack is being bought now. Letters waiting for credit go by themselves when it arrives. |
| `one_in_24_hours`                      | A pack was bought less than 24 hours ago. `next_possible_at` says when the next may be.  |
| `ceiling_reached`                      | This month's automatic purchases are used. Nothing is bought before `resets_at`.         |
| `card_declined`, `needs_bank_approval` | The last purchase did not go through and a person has been asked. Do not count on it.    |
| `held`                                 | On hold while sending is stopped or an agent is paused. Nothing is bought.               |

`can_change` and `can_trigger` are always false. The summary gains one sentence while it is on. `real` is false in test mode.

## Plan on the balance

- **"May", never "will".** A purchase can fail: the bank can decline the card or ask the owner to approve it. Plan a run on the credits that can be spent now and treat automatic top-up as cover for the end of it. Say so to the person: "the credit should cover it, though the second half depends on your card going through".
- **It raises no limit.** Your daily, monthly and per-letter limits are unchanged. The most you can send today is still what `limits` says.
- **What causes a purchase.** Only a letter that holds credits, or waits for credit, inside your limits, that leaves fewer credits to spend than the level. A quote, `check_letter`, a refused send or a retry with the same idempotency key never does. There is at most one purchase in 24 hours, and at most the owner's ceiling in a UK calendar month.

For a run larger than the balance, add up the cost from a quote, compare it with `credits.balance` plus what `left_this_month` could add, and ask the person before you start. Asked to turn it on or buy credits, say you cannot and give them the Billing page.

## When a letter waits for credit

`send_letter` answers `waiting_for_credit` (`not_yet`, retry `not_needed`) when the balance does not cover the letter. Its `details` carry `auto_top_up`:

```json
"details": {
  "cost": { "credits": 15, "gbp": "1.50" },
  "spendable": { "credits": 4, "gbp": "0.40" },
  "lapses_at": "2026-11-26T17:20:00.000Z",
  "auto_top_up": {
    "state": "buying",
    "will_buy": true,
    "next_possible_at": null,
    "real": true
  }
}
```

`will_buy` is true only while a pack is being bought for this wait; `because` is added for `held` (`sending_stopped`, `agent_paused` or `price_changed`). The answer's `say` ends with what automatic top-up is doing, and `person.ask` says what the person is asked, if anything.

A wait is not a signal to act. The letter exists and goes by itself when credits arrive, or lapses with its quote. Pass on the link and stop. **Never send it again**: sending again returns the same waiting letter and buys nothing. You are never told about the card: not its last four digits, not the pounds charged, not why a bank said no.

## The credits.bought event

Every pack that arrives, bought by hand or automatically, sends `credits.bought` to your webhook endpoints:

```json
{
  "type": "credits.bought",
  "data": {
    "receipt": "rcpt_8k3t27mw",
    "credits": { "credits": 800, "gbp": "80.00" },
    "automatic": true,
    "ceiling": { "purchases_left": 1, "resets_at": "2026-12-01T00:00:00.000Z" },
    "at": "2026-11-17T11:03:00.000Z",
    "mode": "live"
  }
}
```

`ceiling` is null for a pack bought by hand. Letters waiting for credit go on by themselves in the order they were sent; you do not need to do anything when this arrives. Use it to follow the balance, or to tell your person.

## Rehearse it in test mode

In test mode an owner can turn on a rehearsal in the console and choose what the "bank" does with the next purchase: goes through, card declined, the bank asks, or declined twice. Nothing touches a card: a purchase that goes through adds free test credits, and `credits.bought` arrives with `receipt` null and `mode` `"test"`. Everything else is as in live mode: the same states on `get_account` (with `real: false`), the same `auto_top_up` on a `waiting_for_credit` answer, the same 24 hours and ceiling. Use it to check that your agent plans on the balance, says "may", and does not send again while it waits.
