Performance: Router prefix and tags
This concept affects the initial server response time and the clarity of API documentation loading, impacting how quickly clients can discover and interact with endpoints.
from fastapi import FastAPI, APIRouter app = FastAPI() user_router = APIRouter(prefix="/users", tags=["users"]) @user_router.get("") def get_users(): return [{"name": "Alice"}] item_router = APIRouter(prefix="/items", tags=["items"]) @item_router.get("") def get_items(): return [{"item": "Book"}] app.include_router(user_router) app.include_router(item_router)
from fastapi import FastAPI app = FastAPI() @app.get("/users") def get_users(): return [{"name": "Alice"}] @app.get("/items") def get_items(): return [{"item": "Book"}]
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No router prefixes or tags | N/A | N/A | N/A | [!] OK |
| Using router prefixes and tags | N/A | N/A | N/A | [OK] Good |
prefix in a FastAPI router?from fastapi import APIRouter
router = APIRouter(prefix='/users', tags=['users'])
@router.get('/profile')
async def get_profile():
return {'msg': 'User profile'}router = APIRouter(prefix=['/items'], tags='items')
/products tagged 'products' and another for /orders tagged 'orders'. How do you correctly set prefixes and tags to keep your API organized?Pass the test and this pattern moves unknown