Boolean query parameters let users send true or false values in URLs. This helps control how the server responds.
Boolean query parameters in FastAPI
from fastapi import FastAPI, Query from typing import Optional app = FastAPI() @app.get("/items/") async def read_items(show_details: Optional[bool] = Query(False)): if show_details: return {"message": "Showing detailed info"} return {"message": "Showing basic info"}
Use Optional[bool] with Query to declare a boolean query parameter.
FastAPI automatically converts query strings like ?show_details=true or ?show_details=false to Python booleans.
from fastapi import FastAPI, Query from typing import Optional app = FastAPI() @app.get("/users/") async def get_users(active: Optional[bool] = Query(None)): if active is None: return {"users": "all users"} elif active: return {"users": "only active users"} else: return {"users": "only inactive users"}
debug defaults to false if not given.from fastapi import FastAPI app = FastAPI() @app.get("/debug/") async def debug_mode(debug: bool = False): return {"debug": debug}
This FastAPI app has a GET endpoint at /items/. It accepts a boolean query parameter show_details. If true, it returns a message with detailed info. Otherwise, it returns basic info.
from fastapi import FastAPI, Query from typing import Optional app = FastAPI() @app.get("/items/") async def read_items(show_details: Optional[bool] = Query(False)): if show_details: return {"message": "Showing detailed info"} return {"message": "Showing basic info"}
Boolean query parameters accept values like true, false, 1, 0 (case insensitive).
If you want the parameter to be optional, use Optional[bool] and set a default like None or False.
FastAPI handles the conversion automatically, so you get a Python boolean in your function.
Boolean query parameters let users control API behavior with true/false flags.
FastAPI converts query strings to Python booleans automatically.
Use Optional[bool] with Query to declare them clearly.
Practice
Solution
Step 1: Identify the purpose of Boolean query parameters
Boolean query parameters are flags that can be true or false, used to control how the API behaves.Step 2: Identify the correct description
Only "A true or false flag passed in the URL to control API behavior" describes a true/false flag passed via URL query parameters.Final Answer:
A true or false flag passed in the URL to control API behavior -> Option AQuick Check:
Boolean query parameter = true/false flag [OK]
- Confusing Boolean parameters with numbers or strings
- Thinking Boolean parameters come from request body
- Assuming Boolean parameters are lists
Solution
Step 1: Recall FastAPI syntax for optional Boolean query parameters
Use Optional[bool] with Query(None) to allow the parameter to be true, false, or missing.Step 2: Match the correct function signature
def read_items(active: Optional[bool] = Query(None)): correctly uses Optional[bool] and Query(None) for an optional Boolean query parameter.Final Answer:
def read_items(active: Optional[bool] = Query(None)): -> Option BQuick Check:
Optional[bool] with Query(None) = correct syntax [OK]
- Using bool without Optional for optional parameters
- Using str or int instead of bool
- Omitting Query import or default value
/items?active=true?
from fastapi import FastAPI, Query
from typing import Optional
app = FastAPI()
@app.get('/items')
async def read_items(active: Optional[bool] = Query(None)):
return {"active": active}Solution
Step 1: Understand FastAPI Boolean query parsing
FastAPI automatically converts query strings like 'true' to Python boolean True.Step 2: Check the returned dictionary
The function returns {"active": active}, so with active=true in URL, it returns {"active": true} as JSON.Final Answer:
{"active": true} -> Option DQuick Check:
Query 'true' converts to True boolean [OK]
- Expecting string 'true' instead of boolean true
- Assuming missing parameter returns false
- Thinking it causes an error
from fastapi import FastAPI, Query
app = FastAPI()
@app.get('/items')
async def read_items(active: bool = Query()):
return {"active": active}Solution
Step 1: Check Query parameter usage
Query() without a default value means the parameter is required. For optional, a default like None is needed.Step 2: Identify the missing default
Since active is bool and no default is given, FastAPI expects the query parameter always present, else error.Final Answer:
Query() requires a default value for optional parameters -> Option CQuick Check:
Query() needs default for optional params [OK]
- Omitting default value for optional query parameters
- Thinking async is not allowed
- Trying to convert Boolean to string manually
available. If available is true, return only available items; if false, only unavailable items; if missing, return all items. Which code correctly implements this behavior?Solution
Step 1: Analyze the required filtering behavior
If available is None, return all items; else filter by matching available status.Step 2: Check code correctness
from fastapi import FastAPI, Query from typing import Optional app = FastAPI() items = [{"name": "item1", "available": True}, {"name": "item2", "available": False}] @app.get('/items') async def read_items(available: Optional[bool] = Query(None)): if available is None: return items return [item for item in items if item["available"] == available] uses Optional[bool] with Query(None) and filters items correctly based on available value.Final Answer:
from fastapi import FastAPI, Query from typing import Optional app = FastAPI() items = [{"name": "item1", "available": True}, {"name": "item2", "available": False}] @app.get('/items') async def read_items(available: Optional[bool] = Query(None)): if available is None: return items return [item for item in items if item["available"] == available] -> Option AQuick Check:
Optional[bool] with filtering logic = correct [OK]
- Using bool without Optional for optional parameter
- Not filtering items based on Boolean value
- Using string type instead of Boolean
Ready to prove it?
Pass the test and this pattern moves unknown
