Larz โบ Metered billing in Python
Usage-based billing in Python
Selling an API by the call โ an AI wrapper, a data API โ is the classic indie product. Larz makes the billing one decorator.
pip install larz
@app.metered("$0.01/call")
@app.post("/api/enrich")
def enrich(req):
return {"result": do_work(req.json())} # caller debited 1c; 402 when out
How it works
Each call debits the caller's prepaid credit balance. When they run out, Larz returns
402 Payment Required pointing at a top-up page โ you write no billing code.
Metering follows the API key or account, not the browser
session, so you can sell a public API.
Sell credit packs
app.money.credit_pack("starter", price="$5", credit="$5")
app.money.credit_pack("bulk", price="$20", credit="$25") # +25% bonus
Full runnable example:
examples/paid_api.py in the repo โ a metered
summarizer API with keys and OpenAPI docs.FAQ
How do I do usage-based billing in Python?
Put @app.metered("$0.01/call") on a route. Larz debits prepaid credit per call and returns 402 when the caller runs out.
Can I bill per API key?
Yes โ metering follows the authenticated API key (or user), so each key has its own balance.