
FastAPI
Learn the fundamentals of multiple queries in FastAPI.
Multiple query parameters allow you to filter, sort, and paginate data. Most real-world APIs use multiple query parameters for flexible data retrieval.
In this section, you'll understand:
from fastapi import FastAPI
from typing import Optional
app = FastAPI()
@app.get("/items")
async def list_items(
skip: int = 0,
limit: int = 10,
category: Optional[str] = None,
active: bool = True
):
# Multiple query parameters
# GET /items → skip=0, limit=10, category=None, active=True
# GET /items?skip=10&limit=20 → skip=10, limit=20, category=None, active=True
# GET /items?category=electronics&active=false → skip=0, limit=10, category="electronics", active=False
return {
"skip": skip,
"limit": limit,
"category": category,
"active": active
}# Product search with filters
@app.get("/products")
async def search_products(
q: str, # Required search term
category: Optional[str] = None,
min_price: Optional[float] = None,
max_price: Optional[float] = None,
in_stock: bool = True,
sort: str = "relevance",
page: int = 1,
per_page: int = 20
):
# Build dynamic query based on filters
filters = {"q": q, "in_stock": in_stock}
if category:
filters["category"] = category
if min_price:
filters["min_price"] = min_price
if max_price:
filters["max_price"] = max_price
# Search products
results = db.search_products(**filters)
results = sort_results(results, sort)
# Paginate
skip = (page - 1) * per_page
paginated = results[skip:skip + per_page]
return {
"total": len(results),
"page": page,
"per_page": per_page,
"products": paginated
}Multiple query parameters are essential for:
Ready to explore more? Check out the advanced section for production patterns and edge cases.
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward