Metering & Credit Packs
Charge per call from prepaid credit, sell credit packs, and handle the out-of-credit case gracefully.
Bill per call
import larz.money as money; money.enable(app)
@app.api_key_required(plan="pro")
@app.metered("$0.01/call")
@app.post("/api/run")
def run(req):
return {"ok": True} # 1c debited; 402 when the key is out of creditSell credit
app.money.credit_pack("small", price="$5", credit="$5")
app.money.credit_pack("bulk", price="$20", credit="$25") # +25% bonus
# customers buy at /larz/creditsWhen a key runs out, Larz returns a clean 402 Payment Required pointing at the top-up page โ your handler never runs. Add per-key credit programmatically for free trials.
Takeaways
@app.metered("$0.01/call")= usage billing in one line.- Credit packs + the
/larz/creditspage are built in. - Out-of-credit is a graceful 402, not an error.