tezvyn:

Define a FastAPI endpoint with path and query parameters

AI-drafted, machine-checkedSource: fastapi.tiangolo.combeginner

Tests if you know FastAPI infers parameter location from the route string. Good answer: route with {item_id}, signature item_id: int, q: str | None = None, noting any param not in the path becomes a query param.

WHAT THIS TESTS: Whether you understand FastAPI's declarative binding mechanism. The framework inspects the route path string and the function signature to decide if a parameter is a path variable or a query string argument. It also checks if you know that optionality in Python is expressed through default values rather than type hints alone, and that FastAPI respects standard Python semantics.

A GOOD ANSWER COVERS: First, the route decorator must declare the path parameter in braces, for example app.get("/items/{item_id}"). Second, the function signature should list item_id without a default value to make it required, and q: str | None = None to make it an optional query parameter. Third, you should state the core rule explicitly: FastAPI examines the path template, and any parameter name that appears inside braces is treated as a path parameter. Every other parameter in the function signature is automatically interpreted as a query parameter regardless of its type. Fourth, emphasize that default values determine whether a parameter is optional. A query parameter without a default becomes required, and a path parameter cannot have a default because it is part of the URL routing logic. Fifth, mention that type hints drive automatic validation and OpenAPI documentation generation.

COMMON WRONG ANSWERS: Insisting that you must import Query or Path from fastapi to distinguish them for basic cases. Overcomplicating the answer by discussing Header, Cookie, or Body parameters when they were not asked about. Claiming that the type hint Optional[str] alone makes a parameter optional without assigning a default value like None. Confusing the order of arguments in the function signature with how FastAPI resolves them. Saying that query parameters must appear after path parameters in the function definition, which is a Python restriction for positional arguments but not a FastAPI rule.

LIKELY FOLLOW-UPS: How would you make a query parameter required? What happens if the path parameter name in the decorator does not match the function argument name? How do you add validation or metadata to a query parameter without changing its fundamental behavior? Can a path parameter be optional, and why or why not? How does FastAPI handle type conversion for path and query parameters?

ONE CONCRETE EXAMPLE: Write app.get("/items/{item_id}") followed by async def read_item(item_id: int, q: str | None = None): return {"item_id": item_id, "q": q}. Explain that a GET request to /items/42?q=foo maps 42 to item_id and foo to q, while /items/42 is valid because q defaults to None. If you omit item_id from the URL, FastAPI returns a 404 because the route no longer matches. If you call /items/42?q=foo&q=bar, FastAPI will use the last value by default unless you use List[str].

Read the original → fastapi.tiangolo.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.