0
0
FastAPIframework~10 mins

Route decorator syntax in FastAPI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Route decorator syntax
Define function
Apply @app.route decorator
Register route with path and method
Wait for HTTP request
If request matches path and method
Call decorated function
Return response to client
This flow shows how a function is decorated with a route, registered, and called when a matching HTTP request arrives.
Execution Sample
FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/hello")
async def say_hello():
    return {"message": "Hello!"}
Defines a GET route at /hello that returns a JSON greeting message.
Execution Table
StepActionEvaluationResult
1Define async function say_helloFunction createdFunction object stored
2Apply @app.get("/hello") decoratorRegister route '/hello' with GET methodRoute added to app's routing table
3Start FastAPI serverServer listens for requestsReady to accept HTTP requests
4Receive GET request at /helloMatch request path and methodRoute matched, call say_hello()
5Execute say_hello()Return {'message': 'Hello!'}Response JSON generated
6Send response to clientHTTP 200 with JSON bodyClient receives greeting message
💡 Execution stops after response sent to client.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
say_helloundefinedfunction objectfunction objectfunction objectfunction object
app.routesempty listcontains route '/hello' GETcontains route '/hello' GETcontains route '/hello' GETcontains route '/hello' GET
requestnonenoneGET /hello request objectGET /hello request objectnone
responsenonenonenone{'message': 'Hello!'}sent to client
Key Moments - 3 Insights
Why do we use @app.get("/hello") above the function?
The decorator registers the function as the handler for GET requests to '/hello'. Without it, the function won't be called on requests. See execution_table step 2.
What happens if a request comes to a different path?
FastAPI checks registered routes and finds no match, so it returns a 404 error. This is because only '/hello' is registered here (execution_table step 4).
Why is the function defined as async?
FastAPI supports async functions to handle requests efficiently. The async keyword allows the server to handle other tasks while waiting for this function to complete (execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of app.routes after step 2?
AIt contains the route '/hello' with GET method
BIt is still empty
CIt contains the route '/hello' with POST method
DIt contains multiple routes
💡 Hint
Check the 'app.routes' variable in variable_tracker after step 2.
At which step does FastAPI call the decorated function to handle the request?
AStep 2
BStep 5
CStep 4
DStep 6
💡 Hint
Look at execution_table where the function execution and response generation happen.
If we change @app.get to @app.post, what changes in the execution?
ABoth GET and POST requests will call the function
BGET requests to '/hello' will still call the function
CPOST requests to '/hello' will call the function instead of GET
DNo requests will call the function
💡 Hint
Consider how the route method affects which HTTP requests trigger the function (see execution_table step 2 and 4).
Concept Snapshot
Route decorator syntax in FastAPI:
- Use @app.get('/path') or @app.post('/path') above async function
- Registers function as handler for HTTP method and path
- When matching request arrives, FastAPI calls function
- Function returns response (usually JSON)
- Async functions improve server efficiency
Full Transcript
In FastAPI, you define a function and decorate it with @app.get or @app.post to register it as a route handler. The decorator tells FastAPI which URL path and HTTP method the function should respond to. When the server runs and receives a request, it checks if the path and method match any registered routes. If yes, it calls the decorated function and sends back its return value as the response. This example shows a GET route at '/hello' that returns a JSON greeting. The function is async to allow efficient handling of requests. The execution table traces defining the function, applying the decorator, receiving a request, calling the function, and sending the response. Variables like the function object, app routes, request, and response change state during these steps. Common confusions include why decorators are needed, what happens on unmatched paths, and why async is used. The visual quiz tests understanding of route registration, function calling, and HTTP method effects.