balance.changed webhook pushes the
account’s new balance for an asset after every ledger that changes it. This
recipe runs a small receiver that verifies each delivery with the Lumeus
verifier and prints an alert when the XLM balance drops below a threshold.
You need a webhook key (whk_..., from Webhooks > Webhook keys in the
dashboard) and a public HTTPS URL for your receiver, such as a cloudflared
tunnel to localhost:3000. Your first webhook shows the
tunnel setup step by step.
1. Subscribe
Create a subscription for one account’s balance changes. The response holds the signing secret (hmac_secret) only once. The command saves it to
webhook-secret.txt on success and refuses to overwrite an existing file; to
retry a failed create, run it again in the same shell so it reuses
CREATE_KEY:
asset: "native" limits it to XLM; leave it out to get every asset the account
holds. balance.changed leaves out changes caused only by transaction fees; use
balance.changed.fees to include them. Deliveries start after the subscription
is active (usually right away; see Lifecycle).
2. Run the receiver
The receiver verifies each delivery with the shared verifier: the body signature, the timestamp and the signed delivery context (event id, network, lane and body hash), with both secrets during a key rotation. It records the event under the unique key(network, id), checks the threshold and only then
answers 204. Keep that work fast: deliveries to one endpoint go one at a
time, so a slow answer delays the next event. In production, insert the event
into your database and enqueue the alert instead of printing it.
Download the verifier for your language next to the receiver:
3. What arrives
Each delivery body is onebalance.changed event. This one, for the sample
account, is built from Testnet ledger 5330999, where nine 10,000 XLM transfers
left the account:
- One event per holder, asset and ledger: several changes in one ledger arrive as one event with the balance after the ledger.
balanceis the new absolute balance, so a missed or repeated delivery never leaves your view wrong after the next one. Keep the event with the highestledgerper holder and asset.- Deduplicate on
(network, id): delivery is at least once. See Deduplicate.
Limits and credits
Each attempt must get a
2xx within 10 seconds; a failed delivery is
tried up to 8 times. See Receive and verify and
Webhooks overview.
Next
- Balance events: selectors, fields and stitching with an Assets snapshot.
- Your first webhook: tunnel, test delivery and cleanup.
- Wallet portfolio: the starting balances for your view.