Query Parameters & Defaults
Add optional query parameters with defaults for filtering and pagination.
Path parameters are part of the URL, but I've seen URLs like /items?page=2&limit=10. What are those?
Query parameters — the part after the ? in a URL. In FastAPI, any function parameter that isn't in the path is automatically treated as a query parameter:
@app.get("/items")
def list_items(skip: int = 0, limit: int = 10):
items = ["a", "b", "c", "d", "e", "f"]
return items[skip : skip + limit]
GET /items?skip=2&limit=3 returns ["c", "d", "e"]. The defaults mean GET /items works too — it returns the first 10 items.
So the default value makes it optional?
Exactly. If a parameter has a default, it's optional. If it doesn't, it's required:
@app.get("/search")
def search(q: str, max_results: int = 5):
return {"query": q, "max_results": max_results}
Here q is required — calling GET /search without ?q=something returns a 422 error. But max_results has a default of 5, so it's optional.
Can I make a parameter explicitly optional — like, it might not be there at all?
Use None as the default:
from typing import Optional
@app.get("/tasks")
def list_tasks(
status: Optional[str] = None,
priority: Optional[int] = None,
limit: int = 20
):
result = {"limit": limit, "filters": {}}
if status is not None:
result["filters"]["status"] = status
if priority is not None:
result["filters"]["priority"] = priority
return result
GET /tasks returns all tasks. GET /tasks?status=done filters by status. GET /tasks?status=done&priority=1 filters by both. The function checks what was provided and builds the response accordingly.
How do I combine path parameters and query parameters in the same endpoint?
Just use both. Path parameters go in the URL path, query parameters are the rest:
@app.get("/users/{user_id}/tasks")
def user_tasks(
user_id: int,
status: Optional[str] = None,
skip: int = 0,
limit: int = 10
):
return {
"user_id": user_id,
"status_filter": status,
"skip": skip,
"limit": limit
}
FastAPI knows user_id is a path parameter because it appears in "/users/{user_id}/tasks". The rest are query parameters. Clean, typed, and validated — all from a function signature.
Practice your skills
Sign up to write and run code in this lesson.
Query Parameters & Defaults
Add optional query parameters with defaults for filtering and pagination.
Path parameters are part of the URL, but I've seen URLs like /items?page=2&limit=10. What are those?
Query parameters — the part after the ? in a URL. In FastAPI, any function parameter that isn't in the path is automatically treated as a query parameter:
@app.get("/items")
def list_items(skip: int = 0, limit: int = 10):
items = ["a", "b", "c", "d", "e", "f"]
return items[skip : skip + limit]
GET /items?skip=2&limit=3 returns ["c", "d", "e"]. The defaults mean GET /items works too — it returns the first 10 items.
So the default value makes it optional?
Exactly. If a parameter has a default, it's optional. If it doesn't, it's required:
@app.get("/search")
def search(q: str, max_results: int = 5):
return {"query": q, "max_results": max_results}
Here q is required — calling GET /search without ?q=something returns a 422 error. But max_results has a default of 5, so it's optional.
Can I make a parameter explicitly optional — like, it might not be there at all?
Use None as the default:
from typing import Optional
@app.get("/tasks")
def list_tasks(
status: Optional[str] = None,
priority: Optional[int] = None,
limit: int = 20
):
result = {"limit": limit, "filters": {}}
if status is not None:
result["filters"]["status"] = status
if priority is not None:
result["filters"]["priority"] = priority
return result
GET /tasks returns all tasks. GET /tasks?status=done filters by status. GET /tasks?status=done&priority=1 filters by both. The function checks what was provided and builds the response accordingly.
How do I combine path parameters and query parameters in the same endpoint?
Just use both. Path parameters go in the URL path, query parameters are the rest:
@app.get("/users/{user_id}/tasks")
def user_tasks(
user_id: int,
status: Optional[str] = None,
skip: int = 0,
limit: int = 10
):
return {
"user_id": user_id,
"status_filter": status,
"skip": skip,
"limit": limit
}
FastAPI knows user_id is a path parameter because it appears in "/users/{user_id}/tasks". The rest are query parameters. Clean, typed, and validated — all from a function signature.
Practice your skills
Sign up to write and run code in this lesson.