Getting your change back: the wallet side of service-issued Cashu change

Swap before re-spend, drop token+origin, treat 404/425/410 as three facts, replay the payment token to recover a lost change, and price small change against input_fee_ppk (measured).

Published as a client-facing page in routstr-core docs in PR 9555af0a (commit ea69befc): docs/client/change.md. Written against routstr-core 0dece8e; fee figures measured 2026-11-24.

Getting Your Change Back

Routstr is pre-paid: you pay before a request runs, and whatever the request did not
cost is owed back to you. That remainder is change, and it arrives as a Cashu token.

This page is the wallet-side half of that lifecycle — what to do with the token the
moment you have it, what each error from the node actually means, and how to recover
change whose response you never saw. It is advice for wallet implementers, not a
protocol claim
: Cashu does not define service-issued change, and Routstr is one
implementation of the convention. The endpoint itself, with its full error table, is
documented under Refund Balance.

Where change comes from

There are two shapes, and they fail differently.

Per-request payment (X-Cashu header). You send the token you are paying with; the
node reserves against it, forwards the request, and sends the remainder back in an
X-Cashu response header on the same response. Nothing else identifies that change:
not a key, not a URL — just the token in the header. If minting the remainder fails, the
response carries X-Routstr-Refund-Error: refund_failed instead, and the change has to be
claimed (see Recovering a lost change).

A key balance (Authorization: Bearer sk-...). The balance is the account; change is
whatever is left when you are done:

curl -X POST https://provider.example/v1/balance/refund \
  -H "Authorization: Bearer sk-..."

The token comes back in the body and in the X-Cashu header. Change for a per-request
payment is claimed from the same endpoint by sending the original payment token in the
X-Cashu header instead of an Authorization — that request is the only handle you have
on it.

Rule 1: swap before you spend, especially at the issuer

A token is bearer money, and the proofs inside it are not yours until the mint re-issues
them to you. Two payments by the same user can be joined exactly if you spend the change
back at the node that issued it:

  • the node records the token it sent you against the request that produced it
    (routstr/upstream/base.py:4947, key refunds the same way at routstr/refund.py:439),
    and it also keeps the proofs it handed out in its own database, marked as reserved for
    sending (routstr/wallet.py:791-793). It knows their secrets.
  • depositing that exact token back at the same node is therefore a perfect match between
    two of your payments — no heuristic, no timing inference.
  • a NUT-03 swap at the mint replaces
    those proofs with new ones the node has never seen, which cuts the join. It costs one
    mint round trip and the input fee, and it is the price of not being a table row you
    built yourself.

So: receive change by swapping it, and spend the swapped proofs. Prefer a swap
especially when the change is going back to the node that issued it. If your wallet sends
the token onward to a different service, the swap is still cheap and still removes an
origin you would otherwise be carrying.

Rule 2: do not store “token plus origin”

A wallet that keeps a token next to the service URL that issued it has written, on disk,
the linkage table an observer would want. Once the proofs are swapped:

  • keep proofs (amount, secret, C, keyset id) and the mint URL — that is what you
    need to spend them;
  • drop the original token string, the service URL, and any request id the token came with;
  • keep the payment’s request id only if you are still in the middle of a recovery (the
    operator needs it to find the payment), and delete it once the change is in hand.

The mint URL must be kept: it is where the proofs are spendable. The service URL is not
needed for anything, and is the part that turns a wallet into a map.

What the node’s answers mean

When you claim change for a per-request payment, these are the possible answers
(routstr/balance.py:293-430). Only one of them means “nothing is owed”:

Answer Meaning What to do
200 + X-Cashu The change token, in the body as token and in the header Swap it, then drop the token string
404 No request was paid with this token at this node Wrong node, or not a payment token — stop
425 + Retry-After: 2 Still in flight, or the refund is being minted right now Retry after the delay; the sats are not gone
409 token_already_spent / payment_fully_charged The request used the whole payment; no change is owed Drop the token
409 refund_failed / refund_unresolved Minting the refund failed, or its outcome is unknown Contact the operator with the body’s payment_request_id
409 refund_unavailable / refund_not_recorded Paid before the node tracked refund outcomes Contact the operator with payment_request_id
410 The change was swept after going unclaimed Contact the operator; see below

404, 425 and 410 are not interchangeable. 404 is “this token never paid anything
here”. 425 is “not yet” and must be retried. 410 is “too late, but the node can still
tell you what happened”: an uncollected change token is re-absorbed into the node’s own
wallet after 180 days (refund_sweep_ttl_seconds = 15_552_000,
routstr/core/settings.py:229, swept in routstr/wallet.py:2204), which spends its proofs
at the mint. From that point the token is dead and the payment is an operator
reconciliation, not a client retry.

Recovering a lost change

The response carrying your change can be lost to a dropped connection, a crash between
reading the body and storing it, or a client that never read the header. The only path
back to the truth is to replay the payment you already made:

# the token you paid with, not the change token
curl -X POST https://provider.example/v1/balance/refund \
  -H "X-Cashu: cashuAeyJ0..."

This is safe to repeat: while the change is uncollected the node answers the same
token each time, and it never mints a second one. It stops being repeatable only when the
refund is being swept (425) or has been swept (410). That makes the call idempotent
from your side — replay until you get a 200, then swap what you got. Both halves are
pinned by tests: claiming twice returns the same token
(tests/integration/test_x_cashu_settlement.py::test_change_is_refunded_and_claimable_once_stored),
and a payment still in flight answers 425 with Retry-After: 2
(::test_in_flight_payment_answers_pending).

Two conditions worth building for:

  • the node keeps the payment row keyed by the original token string, so the string has
    to survive your process dying. Store it as a pending payment before the request, mark it
    settled when the change is swapped, and replay any pending payment on the next start;
  • do not replay against a different node because 404 came back. A token is only known to
    the node that accepted it.

What a small change is really worth

A change token’s face value is not what you can spend. The mint charges an input fee per
proof when you redeem it (NUT-02):

fee    = ceil(sum(input_fee_ppk of each input proof) / 1000)     sats
net    = face_value - fee

Measured on the two mints a Routstr node trusts by default (/v1/keysets, active sat
keysets, 2026-11-24):

Mint input_fee_ppk Redeeming 1–10 proofs costs
mint.minibits.cash/Bitcoin 0 nothing
mint.cubabitcoin.org 100 1 sat

There the smallest change a token can carry — a single 1-sat proof — is worth zero: the
input fee is the whole face value, so a wallet that swaps it pays 1 sat to receive nothing.
Before you redeem, compute the net value and compare it to the fee; if the change cannot cover
its own input fee, tell the user the amount is below the mint’s redeemable floor instead of
attempting a swap that costs more than it recovers. Your wallet can read the same numbers:

curl -s https://mint.example/v1/keysets | jq '.keysets[] | select(.active)'

Which mint the change comes from

Do not assume it is the mint you paid at. The node pays change from its own wallet,
preferring the mint your payment was made at and falling back to another trusted mint when
that one cannot cover the amount (routstr/wallet.py:1186, routstr/refund.py:424). Read
the mint URL out of the token, and compute the fee against that keyset, not against the
mint you deposited to.

Checking a token without leaking

If you need to know whether change was already redeemed, ask the mint, not the
service: NUT-07 POST /v1/checkstate
answers UNSPENT, PENDING or SPENT for the proofs’ Y = hash_to_curve(secret).

  • PENDING means a transaction is in flight — it is not SPENT, and it is not a failure;
  • UNSPENT means the proofs are still yours to spend;
  • SPENT means they were redeemed.

The same document warns that frequent state checks can help a mint correlate sender and
receiver, so check on a schedule (on startup and on user request), not in a tight loop.

Timing. Re-spending change immediately after receiving it is fast, free and adds a
timing link for anyone watching both payments. A randomised, small delay before the first
re-spend — with an explicit override for users who need the sats now — costs nothing and
removes the fixed interval that makes the link cheap to see. Treat the delay as a default,
not a rule, and revisit it if measurements show a privacy benefit not worth the latency.

Minimal recipe

pay:       store (original_token, node_url, request_id) as pending, then send the request
response:  if X-Cashu present        -> change = header value
           elif X-Routstr-Refund-Error -> change = replay(original_token) until 200
           else                        -> no change owed
replay:    POST {node}/v1/balance/refund with X-Cashu: original_token
           200 -> token; 425 -> wait Retry-After and replay; 404/409/410 -> classify, stop
receive:   swap the token at its mint (NUT-03) and keep the new proofs
forget:    drop original_token, node_url and request_id; keep proofs + mint URL
redeem:    net = face - ceil(fees/1000); if net <= 0 tell the user it is below the
           redeemable floor instead of swapping

References

  • Refund Balance — the endpoint’s full answer table
    and the one-shot refund rules for failed requests.
  • Payment Flow — deposit, spend, withdraw.
  • NUT-02 — keysets and input_fee_ppk.
  • NUT-03 — swap, to send and to receive.
  • NUT-07 — token state check.

Line references are to routstr-core 0dece8e; fee figures were measured on
2026-11-24 from each mint’s /v1/keysets. If the code moves, the behaviour described
here is the contract; the line numbers are only the evidence.


Write a comment