Path Parameters in Python
From the FastAPI cheat sheet ยท Path & Query Parameters ยท verified Jul 2026
Path Parameters
Extract values from the URL path with type validation.
python
from fastapi import FastAPI, Path
app = FastAPI()
@app.get("/items/{item_id}")
async def read_item(item_id: int):
return {"item_id": item_id}
# With validation
@app.get("/users/{user_id}")
async def read_user(
user_id: int = Path(gt=0, le=1000, description="The user ID"),
):
return {"user_id": user_id}๐ก Path parameters are required โ they're part of the URL itself
โก Use Path() for validation: gt, ge, lt, le for numbers; min_length, max_length for strings
๐ Enum parameters auto-generate a dropdown in the /docs UI
๐ข Use {param:path} to capture a full file path including slashes
pathparametersvalidation