Full answer
Query parameters are the key=value pairs after ? in a URL: /students?class_name=10A&page=2.
FastAPI decides the source of each parameter from its declaration:
- Name appears in the path: path parameter.
- Simple type (int, str, bool, float, enum) not in the path: query parameter.
- Pydantic model: request body (unless marked otherwise).
Required vs optional:
def list_students(class_name: str, page: int = 1, search: str | None = None): ...class_namehas no default, so it is required; leaving it out gives 422.pagedefaults to 1.searchis optional andNonewhen absent.
Booleans are flexible: ?active=true, 1, yes, on become True.
Validation with Annotated and Query:
page: Annotated[int, Query(ge=1, le=500)] = 1
search: Annotated[str | None, Query(min_length=2, max_length=40)] = NoneLists need Query() explicitly, otherwise FastAPI would treat list[str] as a body:
subject: Annotated[list[str], Query()] = []?subject=maths&subject=science gives ["maths", "science"].
Other options: alias="class" lets the URL use a name that is a Python keyword, deprecated=True marks it in the docs, and include_in_schema=False hides it. Since FastAPI 0.115 you can group many query parameters into one Pydantic model with Annotated[Filters, Query()].
from typing import Annotated
from fastapi import FastAPI, Query
from fastapi.testclient import TestClient
app = FastAPI()
@app.get("/students")
def list_students(
class_name: Annotated[str, Query(alias="class")],
page: Annotated[int, Query(ge=1, le=500)] = 1,
search: Annotated[str | None, Query(min_length=2)] = None,
subject: Annotated[list[str], Query()] = [],
active: bool = True,
):
return {"class": class_name, "page": page, "search": search, "subject": subject, "active": active}
c = TestClient(app)
print(c.get("/students", params={"class": "10A"}).json())
print(c.get("/students?class=10B&page=3&search=an&subject=maths&subject=science&active=no").json())
r = c.get("/students")
print(r.status_code, r.json()["detail"][0]["loc"], r.json()["detail"][0]["msg"])
print(c.get("/students?class=10A&page=0").json()["detail"][0]["msg"])
Standalone script. Run it with `python file.py`; it calls the app through TestClient, so no server is needed. Output (ran twice with identical results; Python 3.13.5, FastAPI 0.165.1, Pydantic 2.14.4, Starlette 1.10.0, SQLAlchemy 2.1.9, httpx 0.29.0): {'class': '10A', 'page': 1, 'search': None, 'subject': [], 'active': True} {'class': '10B', 'page': 3, 'search': 'an', 'subject': ['maths', 'science'], 'active': False} 422 ['query', 'class'] Field required Input should be greater than or equal to 1
Key points
- Non-path simple types are query parameters
- No default = required; default = optional
- Annotated + Query() for validation
- list[...] needs Query() to read repeated keys
- alias handles names like class that are Python keywords
Common mistakes
- Declaring list[str] without Query() and getting a body parameter
- Sending ?subject=maths,science and expecting two items; FastAPI collects repeated keys, it does not split on commas
- Leaving a query parameter without a default and being surprised by 422
- Trying to name a parameter class without an alias
Follow-ups
- How would you group ten filter parameters into one model?
- How does FastAPI decide whether a parameter is from the path, query or body?
- How do you mark a query parameter as deprecated in the docs?
Tips
- State the source-detection rule clearly
- Show required, default and optional side by side
- Mention the list needs Query() gotcha