comecerodocs
Usage billing

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.

EventSent when
usage:balance_lowThe balance falls to or below the credit's low line.
usage:balance_depletedThe 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 }
  }'
FieldWhat
low_balanceAn amount in the agreement's currency, as a decimal string from 0 to 1000000000 with at most 6 decimals.
low_percentA 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_low is sent again only after added credit takes the balance back above the line. usage:balance_depleted is sent again only after credit is added.
  • If one change takes the balance from above the line straight to zero, only usage:balance_depleted is sent.
  • What happens to usage past the credit is up to the metered entitlement's overage_policy: charged with allow, capped with block. The billing run completes, and capped_amount records 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_id is your own id from the agreement, not the Comecero customer id.
  • balance is what is left after the change that crossed the line.
  • threshold is the low line for usage:balance_low, and "0" for usage:balance_depleted.
  • last_credit is the last credit added, which low_percent is 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.

On this page