๐Ÿ’ฐ Software ยท LARZ-301 · Session 2 of 8

Your First Endpoint (with Validation)

Build a JSON endpoint and validate its input so bad requests fail cleanly with field-level errors.

⏱ 10 min read📅 Updated 2026-07-25📊 Code examples throughout

An endpoint

from larz import Larz
import larz.api as api
app = Larz(secret="...")
api.enable(app)

@app.post("/api/wordcount")
def wordcount(req):
    return {"words": len(req.json()["text"].split())}

Validate the input

Never trust the caller. A schema rejects bad requests with a 400 and field-level errors before your code runs.

@app.validate({"text": {"type": str, "required": True, "maxlen": 5000}})
@app.post("/api/wordcount")
def wordcount(req):
    return {"words": len(req.data["text"].split())}   # req.data is validated

Takeaways

  • Return a dict and Larz serializes JSON automatically.
  • @app.validate(schema) guards type/required/min/max/maxlen/choices.
  • Validated input lands on req.data.

More in this course

Course home → · Browse all courses →