# Balance alerts (/docs/usage-billing/balance-alerts)

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`](/api/webhooks/usage-balance-low) | The balance falls to or below the credit's low line. |
| [`usage:balance_depleted`](/api/webhooks/usage-balance-depleted) | The balance reaches zero. |

## Turn alerts on for a credit [#turn-alerts-on-for-a-credit]

Send `balance_alerts` when you [create or update the credit entitlement](/api/usage-entitlements/save-usage-entitlement):

````bash
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 [#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`](/docs/usage-billing/entitlements#overage-policy-soft-or-hard-limits): charged with `allow`, capped with `block`. The billing run completes, and `capped_amount` records what was not charged.

## Subscribe [#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.

````bash
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`:

````json
{
  "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](/api/usage-entitlements/get-entitlement-balance).

See the [webhooks guide](/docs/guides/webhooks) for deliveries and signatures.
