Selling an API by the call in Python — the whole stack in Larz
Keys, per-call metering, validation, OpenAPI docs and checkout — the whole paid-API stack, built in.
An API you sell by the call — an AI wrapper, a data lookup, an image tool — is one of the cleanest products a solo developer can ship. No UI to design, usage scales with revenue, and customers integrate once and keep paying. The hard part was always the plumbing. Larz makes all of it built in.
Validate, then bill per call
from larz import Larz
import larz.api as api, larz.money as money
app = Larz(secret="..."); api.enable(app); money.enable(app)
@app.api_key_required(plan="pro")
@app.validate({"text": {"type": str, "required": True, "maxlen": 5000}})
@app.metered("$0.01/call")
@app.post("/api/wordcount")
def wordcount(req):
return {"words": len(req.data["text"].split())}
Bad input returns a 400 with field-level errors before your code runs. A missing
key is a 401; the wrong plan a 403; an out-of-credit key a clean
402 pointing at the top-up page. Your handler only runs when everything is valid and
paid for.
Sell credit, document, ship
app.money.credit_pack("bulk", price="$20", credit="$25") # +25% bonus
app.enable_docs(title="My API", version="1.0") # /openapi.json + /docs
Customers buy credit at /larz/credits, read auto-generated docs at
/docs, and you deploy with gunicorn. Swap Stripe for Paddle or crypto without
touching a route.
FAQ
How does per-call billing work?
@app.metered("$0.01/call") debits the caller's prepaid credit on each successful call. When the balance hits zero, Larz returns a 402 and your handler doesn't run.
Where do customers buy credit?
Larz serves a built-in credit page at /larz/credits and an account portal at /larz/account. You define credit packs with app.money.credit_pack(...).
apiguide