Balance alerts
Get a webhook when a customer's prepaid credit runs low or runs out, so you can ask them to top up before usage is charged or blocked.
A credit entitlement can tell you when its balance gets low and when it reaches zero. Turn it on per credit with balance_alerts, then subscribe to the two webhook events.
| Event | Sent when |
|---|---|
usage:balance_low | The balance falls to or below the credit's low line. |
usage:balance_depleted | The balance reaches zero. |
Turn alerts on for a credit
Send balance_alerts when you create or update the credit entitlement:
curl -X POST https://api.comecero.com/api/v1/usage/entitlements \
-H "Authorization: Bearer $COMECERO_SECRET_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"entitlement_id": "credit_cust_8412",
"kind": "credit",
"billing_agreement_id": "agr_cust_8412",
"balance_alerts": { "low_balance": "20", "low_percent": 10 }
}'| Field | What |
|---|---|
low_balance | An amount in the agreement's currency, as a decimal string from 0 to 1000000000 with at most 6 decimals. |
low_percent | A whole number from 1 to 99: a share of the last credit added. |
The low line is the higher of the two. With low_balance: "20" and low_percent: 10, a top-up of 500 makes the line 50, and a top-up of 100 makes it 20. Send neither ("balance_alerts": {}) to get only usage:balance_depleted. Leave balance_alerts out and no balance alerts are sent.
Only a credit entitlement takes balance_alerts. On a metered one it is refused with 400 (invalid_input).
When they are sent
- The balance is checked each time it changes: when a billing run draws on the credit, and when credit is added.
- Each alert is sent once per crossing.
usage:balance_lowis sent again only after added credit takes the balance back above the line.usage:balance_depletedis sent again only after credit is added. - If one change takes the balance from above the line straight to zero, only
usage:balance_depletedis sent. - What happens to usage past the credit is up to the metered entitlement's
overage_policy: charged withallow, capped withblock. The billing run completes, andcapped_amountrecords what was not charged.
Subscribe
Create an event subscription for each event in the dashboard (Webhooks > Event Subscriptions) or with the API. Send "active": true: a new event subscription is off until it is turned on.
curl -X POST https://api.comecero.com/api/v1/event_subscriptions \
-H "Authorization: Bearer $COMECERO_SECRET_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"event_type": "usage:balance_low",
"method": "http",
"format": "json",
"destination": "https://example.com/webhooks/comecero",
"active": true
}'The alert is in the event's data:
{
"object": "usage_balance_alert",
"agreement_id": "agr_cust_8412",
"entitlement_id": "credit_cust_8412",
"customer_id": "cust_8412",
"balance": "18.5",
"threshold": "20",
"last_credit": "100",
"ledger_entry_id": "ule_4f9c2a7d1e8b4c3fa6d05e9b7c1a2f38"
}customer_idis your own id from the agreement, not the Comecero customer id.balanceis what is left after the change that crossed the line.thresholdis the low line forusage:balance_low, and"0"forusage:balance_depleted.last_creditis the last credit added, whichlow_percentis a share of.- Amounts are decimal strings in the agreement's currency. Read the balance at any time with Get an entitlement's balance.
See the webhooks guide for deliveries and signatures.
Credits and burndown
Sell prepaid credit, add it safely with an idempotency key, read the balance, and see exactly what is used first when usage is billed.
Usage with subscriptions
Sell a plan with a monthly fee and usage on top. How usage follows the subscription through renewals, plan changes, cancellations and failed payments.