Difficulty: Beginner
How does FastAPI tell path parameters apart from query parameters, and how do you declare required and optional ones?
FastAPI decides by looking at the route path and the function signature. If a parameter name appears inside {} in the path, it is a path parameter and is always required. Any other simple-typed parameter (int, str, bool, enum, etc.) is treated as a query parameter: it is required if it has no default and optional if it has one.
When FastAPI inspects a handler, each parameter is classified like this:
{placeholder} in the path: path parameter.Query): request body.int, str, float, bool, UUID, date or an Enum: query parameter.Query(), Path(), Body(), Header(), Cookie().| Declaration | Where it comes from | Required? | |
|---|---|---|---|
order_id: int with /orders/{order_id} | Path | Always | |
status: str | Query string | Yes, no default | |
limit: int = 20 | Query string | No, defaults to 20 | |
| `q: str \ | None = None` | Query string | No, may be None |
Everything in a URL is a string. FastAPI converts it using the type hint:
/orders/42 with order_id: int gives you the integer 42; /orders/abc returns a 422.bool query params accept true, 1, yes, on (and their false counterparts), case-insensitively.Enum types restrict values and show a dropdown in Swagger UI.list[str] must be wrapped in Query() to read repeated params like ?tag=veg&tag=spicy; without it FastAPI would expect a body./restaurants/{restaurant_id}/menu. If the value is missing, the URL means something else./restaurants?city=bengaluru&veg_only=true&page=2.A good rule: if removing the value changes which resource you mean, it belongs in the path; if it only changes how you see a collection, it belongs in the query.
q: str = None. It works at runtime but is a wrong type; use q: str | None = None (or Optional[str] on older Python).{file_path:path} to capture slashes.The modern style is Annotated, e.g. limit: Annotated[int, Query(ge=1, le=100)] = 20, which keeps the default as a normal Python default and adds validation metadata.
from enum import Enum
from typing import Annotated
from fastapi import FastAPI, Query
app = FastAPI()
class SortBy(str, Enum):
rating = "rating"
delivery_time = "delivery_time"
@app.get("/cities/{city}/restaurants")
def list_restaurants(
city: str, # path: always required
cuisine: str, # query: required (no default)
veg_only: bool = False, # query: optional
sort: SortBy = SortBy.rating, # query: restricted values
q: str | None = None, # query: optional, may be None
tags: Annotated[list[str], Query()] = [], # repeated: ?tags=a&tags=b
page: Annotated[int, Query(ge=1)] = 1,
):
return {"city": city, "cuisine": cuisine, "veg_only": veg_only,
"sort": sort, "q": q, "tags": tags, "page": page}
Each parameter's source is inferred from the path and its type. Query() is needed for list parameters and for adding constraints like ge=1.
from fastapi import FastAPI
app = FastAPI()
@app.get("/files/{file_path:path}")
def read_file(file_path: str):
# GET /files/reports/2026/q1.csv -> file_path = "reports/2026/q1.csv"
return {"file_path": file_path}
The :path converter, inherited from Starlette, lets a path parameter match across slashes.
path parameters, query parameters, type coercion, optional parameters, Annotated