Skip to navigation

Auto top-ups

When a customer’s credits run low, a credit auto top-up buys more by adding a top-up to the customer’s order and invoicing it. It watches the same available balance your customer sees, so the top-up arrives before the work that needs the credits, not after, and usage stays paid from credits rather than running into overage.

How they work

  1. You set a threshold on a credit currency: a number of credits, or a percentage of what’s been granted.
  2. Paid watches the balance. When available falls to the threshold or below it, the balance has crossed it.
  3. The rule buys once per crossing. It doesn’t buy again until the balance has recovered above the threshold.

Because available already leaves out credits reserved by credit holds, a hold can cross a threshold before anything is spent.

Thresholds

An auto top-up’s threshold takes one of two types:

typeCrossed whenExample
availablevalue credits or fewer are available{ "type": "available", "available": { "value": "1000" } }
percentAvailablevalue percent or less of the granted credits are available{ "type": "percentAvailable", "percentAvailable": { "value": "10" } }

A percentage keeps working as the customer’s grants grow.

Set up an auto top-up

An auto top-up buys a one-time top-up price when a customer’s balance falls below a threshold. Adatum first creates a top-up price that grants one credit for each credit bought, at 70¢:

POST /v1/prices
{
"product": "adatum_platform",
"name": "Credit top-up",
"currency": "usd",
"type": "oneTime",
"oneTime": {
"unit": "credit",
"pricing": { "type": "perUnit", "perUnit": { "unitAmount": "70" } },
"grants": [
{
"creditCurrency": "adatum_credits",
"amountPerUnit": "1",
"expiresAfter": { "value": "12", "unit": "month" }
}
]
}
}

Then it sets Brightrail up to buy 5,000 credits whenever 1,000 or fewer are left, at most 3 times a month:

Create a credit auto top-up with the customer, their order, the top-up price and the quantity to buy.

POST /v1/credit-auto-top-ups
{
"customer": "cus_3Vb9Rk",
"creditCurrency": "adatum_credits",
"order": "ord_7Gk3Xs",
"price": "prc_8Tn4Wd",
"quantity": 5000,
"threshold": { "type": "available", "available": { "value": "1000" } },
"collection": "sendInvoice",
"limits": { "maxPerMonth": 3 }
}

The response is the rule, with a ctu_ ID, status: "active", and lastTriggeredAt and lastAmendment set to null until its first purchase.

FieldWhat it’s for
orderThe customer’s active order the top-ups are added to. It must belong to customer, or the request fails with customer_mismatch.
priceA one-time price whose grants include the credit currency.
quantityHow much of the price to buy each time, in the price’s unit.
collectionsendInvoice (the default) sends the top-up invoice on the order’s payment terms. chargeAutomatically charges paymentMethod, or the customer’s default payment method.
limits.maxPerMonthThe most purchases in a calendar month, in UTC. Defaults to 1.

chargeAutomatically depends on payment methods in the v1 API, which aren’t available yet. Use sendInvoice until they are.

Each purchase is added to the order by amendment and invoiced, so it shows on the order’s history and its invoice like anything else your customer buys. When the credits arrive follows the price’s grantedOn: activation, the default, grants them straight away, and invoicePaid waits until the invoice is paid.

A customer can have one enabled auto top-up per credit currency: creating or enabling a second fails with auto_top_up_conflict. Only one purchase is in progress at a time, and a purchase that still leaves the balance below the threshold doesn’t trigger another.

How it bills

Brightrail has 1,200 credits available when an event spends 250 of them:

StepValue
Balance after the event1,200 - 250 = 950 credits, below the threshold of 1,000
Purchase added by amendment5,000 of the Credit top-up price
Invoiced now5,000 × 70¢ = 350,000¢ = $3,500.00
Credits granted, on activation+5,000 adatum_credits
Balance after the top-up950 + 5,000 = 5,950 credits
Purchases this month1 of the 3 allowed

The rule records the purchase in lastTriggeredAt and lastAmendment, and is ready to buy again once the balance has recovered above 1,000 and fallen back to 1,000 or below.

Change or retire

An auto top-up’s status is active, paused with a pausedReason, or ended when its order ends. Three failed collection attempts in a row pause it with pausedReason: "paymentFailed". Update the rule with only the fields to change, and send enabled: true to resume a paused rule. It checks the balance straight away, and buys if the balance is still below the threshold and the monthly limit allows. Archive it to stop it for good. Purchases it already made stay on the order.