Usage-based billing without the plumbing
Metered pricing โ per call, per job, per unit โ usually means a metering service, a billing job and reconciliation. In Larz it's a decorator.
pip install larz
@app.metered("$0.01/call")
@app.post("/api/run")
def run(req):
return {"ok": True} # 1c debited; clean 402 when out of credit
How it works
Each successful call debits the caller's prepaid credit. The caller is whoever the request
belongs to โ a logged-in account, an API key, or a session. When the balance hits zero, Larz
returns a 402 Payment Required pointing at the top-up page, and your handler never
runs.
Sell the credit
app.money.credit_pack("bulk", price="$20", credit="$25") # +25% bonus
Customers buy at the built-in /larz/credits page and see their balance and
history in the portal. Combine metering with plans and API keys to build a full paid API.
FAQ
Can I meter anything, not just API calls?
Yes. Put @app.metered on any route โ an export, a render, a job trigger โ and each hit is billed.
What happens when a customer runs out of credit?
Larz returns a 402 and links to the top-up page; your handler doesn't execute, so you never do unpaid work.