Query Parameters in Python

From the FastAPI cheat sheet ยท Path & Query Parameters ยท verified Jul 2026

Query Parameters

Accept query string parameters with defaults and validation.

python
from fastapi import FastAPI, Query

app = FastAPI()

# Basic query parameters
@app.get("/items/")
async def read_items(skip: int = 0, limit: int = 10):
    return {"skip": skip, "limit": limit}
# GET /items/?skip=5&limit=20

# Required + validated
@app.get("/search/")
async def search(
    q: str = Query(min_length=3, max_length=50),
):
    return {"query": q}
๐Ÿ’ก Any function parameter not in the URL path is automatically a query parameter
โšก Use Query() for string validation: min_length, max_length, pattern (regex)
๐Ÿ“Œ Use list[str] with Query() to accept repeated query params (?tag=a&tag=b)
๐ŸŸข Set deprecated=True to mark a parameter as deprecated in the docs
queryparametersvalidation

More Python tasks

Back to the full FastAPI cheat sheet