0
0
FastAPIframework~10 mins

Basic query parameter declaration in FastAPI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Basic query parameter declaration
Start HTTP GET Request
Check URL for query parameters
Match query parameters to function args
Assign default if missing
Execute function with parameters
Return response with parameter values
This flow shows how FastAPI reads query parameters from the URL, matches them to function arguments, assigns defaults if needed, and returns the response.
Execution Sample
FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/items/")
async def read_items(q: str = None):
    return {"q": q}
This code defines a GET endpoint that reads an optional query parameter 'q' and returns it in the response.
Execution Table
StepRequest URLQuery Parameter 'q'Parameter Value in FunctionResponse
1GET /items/NoneNone (default){"q": null}
2GET /items/?q=appleapple"apple"{"q": "apple"}
3GET /items/?q=123123"123"{"q": "123"}
4GET /items/?q=Empty string""{"q": ""}
5GET /items/?other=xyzNo 'q' paramNone (default){"q": null}
💡 Execution stops after sending the response for each request.
Variable Tracker
VariableStartRequest 1Request 2Request 3Request 4Request 5
qNoneNone"apple""123"""None
Key Moments - 3 Insights
Why is 'q' None when the URL has no query parameter?
Because the function argument 'q' has a default value of None, so if the query parameter is missing, FastAPI assigns None (see execution_table row 1 and 5).
What happens if the query parameter 'q' is present but empty?
FastAPI passes an empty string "" to the function argument 'q' (see execution_table row 4), not None.
Does FastAPI automatically convert query parameters to types?
Yes, but here 'q' is a string, so values like '123' remain strings (see execution_table row 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'q' in the function when the request URL is '/items/?q=apple'?
A"apple"
BNone
C""
D123
💡 Hint
Check execution_table row 2 under 'Parameter Value in Function'
At which request does the query parameter 'q' get the value None?
ARequest 2
BRequest 1
CRequest 3
DRequest 4
💡 Hint
Look at execution_table rows 1 and 5 for 'Parameter Value in Function'
If you remove the default value '= None' from 'q', what happens when the URL has no 'q' parameter?
Aq becomes empty string
Bq becomes None automatically
CFastAPI returns an error for missing required parameter
DFunction returns null
💡 Hint
Default values make parameters optional; without default, parameter is required (see key_moments explanation)
Concept Snapshot
Basic query parameter declaration in FastAPI:
- Define function argument with type and optional default
- FastAPI reads query param from URL
- If missing, uses default value
- Returns parameter value in response
Example: async def read_items(q: str = None):
Full Transcript
This example shows how FastAPI handles basic query parameters. When a GET request is made to the endpoint, FastAPI looks for query parameters in the URL. It matches them to the function's arguments by name. If a query parameter is missing, FastAPI uses the default value specified in the function argument, here None. The function then returns the value it received. For example, if the URL is /items/?q=apple, the function receives 'apple' as q. If the URL has no q parameter, q is None. If q is empty like /items/?q=, q is an empty string. This behavior helps create flexible APIs that handle optional query parameters easily.