0
0
FastAPIframework~10 mins

Route ordering and priority in FastAPI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Route ordering and priority
Define routes in order
Incoming request arrives
Check first route pattern
Run first route handler
No
Check next route pattern
Run matched route handler
No
404 Not Found
FastAPI checks routes in the order they are defined. The first matching route handles the request. If none match, a 404 error is returned.
Execution Sample
FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/items/{item_id}")
async def read_item(item_id: str):
    return {"item_id": item_id}

@app.get("/items/special")
async def read_special():
    return {"message": "special item"}
Defines two routes: a dynamic route for any item_id and a specific route for '/items/special'. The order affects which handler runs for '/items/special'.
Execution Table
StepRequest PathRoute CheckedMatch?Handler RunResponse
1/items/special/items/{item_id}Yesread_item{"item_id": "special"}
2/items/special/items/specialNoNoneNot reached
3/items/123/items/{item_id}Yesread_item{"item_id": "123"}
4/items/123/items/specialNoNoneNot reached
5/items/unknown/items/{item_id}Yesread_item{"item_id": "unknown"}
6/items/unknown/items/specialNoNoneNot reached
7/unknown/items/{item_id}NoNoneNot matched
8/unknown/items/specialNoNoneNot matched
9/unknownNo more routesN/ANone404 Not Found
💡 Request handled by first matching route or returns 404 if none match.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5After Step 7Final
request_pathNone/items/special/items/123/items/unknown/unknown/unknown
matched_routeNone/items/{item_id}/items/{item_id}/items/{item_id}NoneNone
handler_runNoneread_itemread_itemread_itemNoneNone
responseNone{"item_id": "special"}{"item_id": "123"}{"item_id": "unknown"}None404 Not Found
Key Moments - 3 Insights
Why does the dynamic route '/items/{item_id}' handle the '/items/special' request instead of the specific '/items/special' route?
Because the dynamic route is defined before the specific route, FastAPI matches it first and stops checking further routes, as shown in execution_table step 1.
What happens if no route matches the incoming request path?
FastAPI returns a 404 Not Found error, as shown in execution_table step 9 where '/unknown' matches no routes.
How can we ensure the specific route '/items/special' handles requests to that path?
Define the specific route before the dynamic route so it matches first, preventing the dynamic route from capturing '/items/special'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what handler runs for the request path '/items/special' at step 1?
A404 Not Found
Bread_item
Cread_special
DNone
💡 Hint
Check the 'Handler Run' column at step 1 in the execution_table.
At which step does the request path '/unknown' result in a 404 Not Found?
AStep 9
BStep 8
CStep 7
DStep 6
💡 Hint
Look for the row where 'Response' is '404 Not Found' in the execution_table.
If we move the '/items/special' route above '/items/{item_id}', what changes in the execution_table for '/items/special'?
AStep 1 still matches dynamic route and runs read_item
BNo route matches and returns 404
CStep 1 matches '/items/special' route and runs read_special
DBoth routes run for the same request
💡 Hint
Consider route order priority explained in concept_flow and key_moments.
Concept Snapshot
FastAPI matches routes in the order they are defined.
The first route pattern that matches the request path handles it.
Specific routes should be defined before dynamic ones to get priority.
If no route matches, FastAPI returns a 404 error.
Route order affects which handler runs for overlapping paths.
Full Transcript
In FastAPI, routes are checked in the order they are defined. When a request comes in, FastAPI looks at each route pattern one by one. If the request path matches a route, FastAPI runs that route's handler and stops checking further routes. If no routes match, it returns a 404 Not Found error. For example, if a dynamic route like '/items/{item_id}' is defined before a specific route like '/items/special', the dynamic route will handle requests to '/items/special' because it matches first. To give priority to specific routes, define them before dynamic routes. This ordering controls which handler runs for overlapping paths.