PythiloLesson 2 of 14 · progress kept in this browser

Validate query bounds at the route

Query strings carry text, even when a caller means an integer or a boolean. Declare the intended type and constraints in the route signature. FastAPI then parses values, supplies declared defaults and reports invalid requests before executing the endpoint.

Annotated keeps validation metadata next to a normal Python type. Distinguish a missing query parameter from an explicitly invalid value: a default applies to absence, not to every conversion failure. Test the boundary values themselves, since inclusive bounds are easy to describe incorrectly in documentation.

from typing import Annotated
from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/page")
async def page(limit: Annotated[
    int, Query(ge=1, le=20)] = 10):
    return {"limit": limit}
print(app.openapi()["paths"].keys())
Your task

Implement build_app() with GET /page, integer limit default 10 constrained to 1..100, returning {"limit": limit}. Invalid values must return 422.

The first run loads fastapi, httpx into this browser.