# Usage billing
Source: https://docs.lomi.africa/build/billing/usage-billing

Sell a prepaid usage pack, then spend the units as events come in.

***

title: Usage billing
description: Sell a prepaid usage pack, then spend the units as events come in.
-------------------------------------------------------------------------------

import { Callout } from '@/components/docs/docs-callout';

Usage billing sells a **pack**. The price is what the customer pays. The pack is how many units that payment buys. Each event spends those units. A short balance refuses the event. A low balance sends the same pack again.

<TaskSurfaces task="meter-usage" />

<Callout type="info">
  For product setup (`product_type: usage_based`), see **[Products](/build/billing/products)**. Cards and mobile money both pay the pack on hosted checkout.
</Callout>

## Golden path

### 1. Create the pack

Create a product with `product_type: usage_based`. Set the pack price, how many units that payment buys (`included_units`), and the unit label. lomi. creates one meter that sums `quantity`. The meter code is the meter name.

### 2. Send checkout

Create a checkout session with that product. The customer pays by card, Wave, or MTN. A completed payment credits `included_units` times the quantity.

You can enroll a customer first with `POST /usage/subscriptions`. The balance starts at zero, and events are refused until they pay.

### 3. Record events

Send usage as it happens. Use a stable `transaction_id` per logical event.

```bash
curl -X POST "https://sandbox.api.lomi.africa/usage/events" \
  -H "Authorization: Bearer $LOMI_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "transaction_id": "evt_unique_123",
    "code": "api_calls",
    "customer_id": "cus_...",
    "subscription_id": "sub_...",
    "quantity": 1
  }'
```

Returns **202 Accepted**. Processing spends the balance when the event fits.

→ [Record usage event](/api/usage/events/UsageEventsController_ingest)

### 4. Read remaining units

| Goal                      | Endpoint                                                                      |
| ------------------------- | ----------------------------------------------------------------------------- |
| Units remaining           | [Get meter balance](/api/meters/MetersController_getBalance)                  |
| Usage on the subscription | [Get subscription usage](/api/subscriptions/SubscriptionsController_getUsage) |

`balance` is units still available (`credited_units` minus `consumed_units`). Gate access on that number.

## When the pack runs out

If the event is larger than the remaining units, it is stored as `failed` with `insufficient_balance`. Nothing is consumed. Send a new event after the customer pays for another pack.

A successful event that leaves the balance under 20% of the last pack opens one top-up for the same pack.

* **Card.** If the first pack was paid by card, that saved card is charged without the customer present. A successful charge credits another pack immediately.
* **Wave and MTN.** Mobile money cannot be pulled. The customer receives the pack checkout link and pays it the same way as the first pack. Completing that checkout credits the units.
* If the saved card charge fails, the customer receives that same link.

## Reconciliation

* **List or get usage events** when you need the processing status: [List usage events](/api/usage/events/UsageEventsController_findAll), [Get usage event](/api/usage/events/UsageEventsController_findOne).
* **Revenue reporting** across MRR, packs, and one-time payments: [Combined revenue metrics](/api/usage/UsageBillingController_getRevenue).

## Related

* [Subscriptions](/build/billing/subscriptions): recurring checkout subscriptions (distinct from usage packs)
* [Products](/build/billing/products): catalog and `usage_based` configuration
* [Verify payments](/build/reliability/verify-payments): confirm the pack payment before treating units as credited
