Building a Monetized API
Validate requests, auto-generate OpenAPI docs, paginate results, and send signed webhooks โ everything a paid JSON API needs.
Validate input
import larz.api as api
api.enable(app)
@app.validate({"text": {"type": str, "required": True, "maxlen": 500}})
@app.post("/api/summarize")
def summ(req):
return {"got": req.data["text"]} # 400 with field errors if invalid
Automatic docs
One call generates a machine-readable spec at /openapi.json and a browsable
page at /docs โ from your routes and schemas.
app.enable_docs(title="My API", version="1.0")
Pagination & webhooks
return api.paginate(Post.all(order="-id"), req, per_page=20)
from larz.api import Webhooks
wh = Webhooks(secret="whsec")
wh.deliver("https://customer.com/hook", "order.paid", {"id": 1}) # signed + retried
Takeaways
- Validation, OpenAPI docs, pagination and webhooks are all built in.
- Combine with API keys + metering (last session) to ship a complete paid API.
- Full runnable example:
examples/paid_api.py.