๐Ÿ’ฐ Software ยท LARZ-301 · Session 4 of 8

Metering & Credit Packs

Charge per call from prepaid credit, sell credit packs, and handle the out-of-credit case gracefully.

⏱ 11 min read📅 Updated 2026-07-25📊 Code examples throughout

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 credit

Sell 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/credits

When 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/credits page are built in.
  • Out-of-credit is a graceful 402, not an error.

More in this course

Course home → · Browse all courses →