Larz › How to build an API in Python
How to build an API in Python
From a first endpoint to a documented, monetized API — one framework.
pip install larz
1. An endpoint
from larz import Larz
app = Larz(secret="...")
@app.post("/api/echo")
def echo(req): return {"you_sent": req.json()}
2. Validate input
import larz.api as api; api.enable(app)
@app.validate({"text": {"type": str, "required": True}})
@app.post("/api/run")
def run(req): return {"got": req.data["text"]}
3. Docs, pagination, keys, billing
app.enable_docs(title="My API") # /openapi.json + /docs
return api.paginate(items, req, per_page=20) # pagination
@app.api_key_required(plan="pro") # keys
@app.metered("$0.01/call") # billing
That's the full arc: routing, validation, docs, keys, and metering — see the step-by-step tutorial.
FAQ
What's the easiest way to build an API in Python?
Larz: define routes, add @app.validate for input validation, call app.enable_docs() for OpenAPI docs, and add API keys + @app.metered to monetize. Zero dependencies.
Does Larz generate OpenAPI docs?
Yes — app.enable_docs() serves /openapi.json and a browsable /docs page generated from your routes and schemas.