Router prefix helps group related routes under a common path. Tags organize routes in the API docs for easy reading.
Router prefix and tags in FastAPI
from fastapi import APIRouter router = APIRouter(prefix="/prefix_path", tags=["tag1", "tag2"]) @router.get("/endpoint") async def example(): return {"message": "Hello"}
The prefix adds a common path before all routes in this router.
The tags list adds labels to routes for API docs grouping.
router = APIRouter(prefix="/users", tags=["Users"]) @router.get("/") async def get_users(): return ["Alice", "Bob"]
router = APIRouter(prefix="/items", tags=["Items", "Inventory"]) @router.get("/{item_id}") async def read_item(item_id: int): return {"item_id": item_id}
router = APIRouter() @router.get("/status", tags=["Health"]) async def status(): return {"status": "ok"}
This FastAPI app has two routers. One for users with prefix '/users' and tag 'Users'. Another for items with prefix '/items' and tag 'Items'. This organizes routes and API docs clearly.
from fastapi import FastAPI, APIRouter app = FastAPI() user_router = APIRouter(prefix="/users", tags=["Users"]) @user_router.get("/") async def list_users(): return ["Alice", "Bob"] item_router = APIRouter(prefix="/items", tags=["Items"]) @item_router.get("/{item_id}") async def get_item(item_id: int): return {"item_id": item_id, "name": "Sample Item"} app.include_router(user_router) app.include_router(item_router)
Router prefixes help avoid repeating common path parts in every route.
Tags improve the auto-generated API docs readability by grouping endpoints.
You can add tags either on the router or on individual routes.
Router prefix groups routes under a shared path.
Tags label routes for better API documentation.
Using both keeps your API organized and easy to understand.
Practice
prefix in a FastAPI router?Solution
Step 1: Understand what prefix does in FastAPI routers
The prefix adds a common path segment to all routes defined in that router, grouping them logically.Step 2: Compare options with prefix purpose
Only To group routes under a common URL path segment correctly describes grouping routes under a shared URL path segment.Final Answer:
To group routes under a common URL path segment -> Option BQuick Check:
Router prefix = group routes [OK]
- Thinking prefix changes HTTP methods
- Confusing prefix with authentication
- Assuming prefix disables routes
Solution
Step 1: Recall correct parameter types for prefix and tags
Prefix expects a string starting with '/', tags expects a list of strings.Step 2: Check each option's syntax
router = APIRouter(prefix='/items', tags=['items']) uses prefix as string and tags as list correctly; others mix types incorrectly.Final Answer:
router = APIRouter(prefix='/items', tags=['items']) -> Option AQuick Check:
Prefix string + tags list = correct syntax [OK]
- Using list for prefix instead of string
- Using string for tags instead of list
- Mixing order of parameters incorrectly
from fastapi import APIRouter
router = APIRouter(prefix='/users', tags=['users'])
@router.get('/profile')
async def get_profile():
return {'msg': 'User profile'}Solution
Step 1: Identify router prefix and route path
The router has prefix '/users' and the route path is '/profile'.Step 2: Combine prefix and route path
Full path is prefix + route path = '/users' + '/profile' = '/users/profile'.Final Answer:
/users/profile -> Option DQuick Check:
Prefix + route path = full endpoint path [OK]
- Ignoring prefix and using only route path
- Swapping prefix and route path order
- Confusing tags with path
router = APIRouter(prefix=['/items'], tags='items')
Solution
Step 1: Check expected types for prefix and tags
Prefix must be a string starting with '/', tags must be a list of strings.Step 2: Analyze given code
Prefix is given as a list, tags as a string, both reversed from expected types.Final Answer:
prefix should be a string, tags should be a list of strings -> Option CQuick Check:
Prefix = string, tags = list [OK]
- Using list for prefix
- Using string for tags
- Assuming both can be strings
/products tagged 'products' and another for /orders tagged 'orders'. How do you correctly set prefixes and tags to keep your API organized?Solution
Step 1: Recall correct types for prefix and tags
Prefix must be a string starting with '/', tags must be a list of strings.Step 2: Evaluate each option for correctness and organization
router_products = APIRouter(prefix='/products', tags=['products'])\nrouter_orders = APIRouter(prefix='/orders', tags=['orders']) correctly assigns string prefixes and list tags for both routers, organizing API paths and documentation clearly.Final Answer:
router_products = APIRouter(prefix='/products', tags=['products']) router_orders = APIRouter(prefix='/orders', tags=['orders']) -> Option AQuick Check:
Correct prefix and tags keep API organized [OK]
- Using list for prefix or string for tags
- Omitting prefix or tags on one router
- Mixing tags and prefix types
Ready to prove it?
Pass the test and this pattern moves unknown
