402 insufficient_credits while nobody is watching the balance. This page covers the one route that configures it, the rules it validates, when a charge actually happens, and how to confirm it worked.
You can set the same two numbers on the Billing page in the console, in the fields When below, .
Before you start
- You need
billing_write. Owners, admins and billing users hold it; members and viewers do not. See Credits and billing. - You need a saved card.
GET /v1/billingreports it ashas_payment_method. Paying through checkout saves the card, or you can add one in the billing portal (POST /v1/billing/portal) without buying anything. Without a card, auto-recharge can be enabled but never charges.
Turn it on
PUT /v1/billing/auto-recharge sets auto-recharge and returns the updated billing state, the same body as GET /v1/billing.
The body refuses any other field. Every amount is micro-USD: 1,000,000 micros is 1 US dollar.
Turn it off
Sendenabled: false. The threshold and amount are optional then.
When a charge happens
Saving the setting does not charge anything. The check runs in the background at two moments:- after each run is admitted by
POST /v1/runs, and - after each run is refused with
402 insufficient_credits.
The charge is
amount_micros on the saved card. The credit lands when the payment succeeds, as a purchase transaction with the description Auto-recharge.
What follows from those rules:
- Nothing happens without traffic. If no run is sent, no check runs, even when the balance is below the threshold. Buy credit through checkout if you need the balance up now.
- The run that triggers a charge is not held for it. A run admitted while the balance is low goes ahead on the current balance. A run refused with
402stays refused: the charge it queued helps the next attempt, once the payment has landed. - One charge per hour is the ceiling. If one hour’s spend is larger than
amount_micros, the balance can still run out before the next hour. Sizeamount_microsabove your busiest hour, or buy extra credit through checkout. - Changing the setting takes effect at the next check. The next admitted or refused run sees the new values.
Pick the numbers
- Threshold: at least your spend over the time a charge takes to land, plus 20,000 micros so the next run can still start. Every run needs 20,000 micros of available credit to be admitted.
- Amount: at least one busy hour of spend, because one charge per hour is the most you get. Read your spend per day from
GET /v1/usage/dailyto size it.
Confirm it works
- Read
GET /v1/billingand checkauto_recharge.enabledistrue, both numbers are what you sent, andhas_payment_methodistrue. - After the balance has dropped below the threshold and a run has been sent, look for a new
purchaserow describedAuto-rechargeintransactions. - If none appears, check the saved card in the billing portal, and check that a charge was not already made in the same clock hour.
Errors
Malformed JSON, an unknown field, a wrong type or a missing
Content-Type gets a plain-text 400, 415 or 422 with no JSON envelope. Check the body against the field table above.
Related
- Credits and billing - the balance, checkout and the billing portal.
- Handling insufficient credits - what your code does when a run gets
402. - Billing in the console - set the threshold and amount without code.
- Usage reporting - measure your daily spend to size the amount.