0
0
FastAPIframework~20 mins

APIRouter for modular routes in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
APIRouter Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when accessing the '/items/42' route?

Given the following FastAPI app with APIRouter, what will be the JSON response when a client requests /items/42?

FastAPI
from fastapi import FastAPI, APIRouter

router = APIRouter()

@router.get('/{item_id}')
async def read_item(item_id: int):
    return {"item_id": item_id, "name": f"Item {item_id}"}

app = FastAPI()
app.include_router(router, prefix='/items')
A404 Not Found
B{"item_id": "42", "name": "Item 42"}
C{"item_id": 42}
D{"item_id": 42, "name": "Item 42"}
Attempts:
2 left
💡 Hint

Check how the item_id parameter is typed and how the router prefix affects the route.

📝 Syntax
intermediate
1:30remaining
Which option correctly registers a router with prefix '/users'?

Choose the correct way to include an APIRouter named user_router into a FastAPI app with the prefix /users.

FastAPI
from fastapi import FastAPI, APIRouter

user_router = APIRouter()

app = FastAPI()
Aapp.include_router(user_router, path='/users')
Bapp.include_router(user_router, prefix='/users')
Capp.include_router('/users', user_router)
Dapp.add_router(user_router, prefix='/users')
Attempts:
2 left
💡 Hint

Check the method name and parameter names for including routers.

🔧 Debug
advanced
2:30remaining
Why does this route never get called?

Consider this FastAPI app code:

from fastapi import FastAPI, APIRouter

router = APIRouter()

@router.get('/profile')
async def get_profile():
    return {"profile": "user"}

app = FastAPI()
app.include_router(router, prefix='/user')

@app.get('/user/profile')
async def direct_profile():
    return {"profile": "direct"}

When a client requests /user/profile, which response is returned and why?

A{"profile": "user"} because the router route matches first
B500 Internal Server Error due to duplicate routes
C404 Not Found because of route conflict
D{"profile": "direct"} because the app route overrides the router
Attempts:
2 left
💡 Hint

Consider the order of route registration and how FastAPI resolves routes: routes are matched in the order they are registered (first registered matches first).

state_output
advanced
2:00remaining
What is the output of this modular route setup?

Given these two router modules and main app, what JSON is returned when requesting /api/v1/products/5?

# products_router.py
from fastapi import APIRouter

router = APIRouter()

@router.get('/products/{product_id}')
async def get_product(product_id: int):
    return {"product_id": product_id, "status": "ok"}

# main.py
from fastapi import FastAPI
from products_router import router as products_router

app = FastAPI()
app.include_router(products_router, prefix='/api/v1')
A404 Not Found
B{"product_id": "5", "status": "ok"}
C{"product_id": 5, "status": "ok"}
D{"status": "ok"}
Attempts:
2 left
💡 Hint

Check how the prefix affects the full route path and the parameter type.

🧠 Conceptual
expert
3:00remaining
Which statement about APIRouter usage is TRUE?

Choose the correct statement about using APIRouter in FastAPI for modular route design.

AAPIRouter allows grouping routes and can be included multiple times with different prefixes in the same app.
BAPIRouter routes must be unique across the entire app and cannot share path names even with different prefixes.
CIncluding an APIRouter with a prefix automatically merges all route parameters into the app's global parameters.
DAPIRouter cannot be used to define routes with dependencies; dependencies must be defined only in the main app.
Attempts:
2 left
💡 Hint

Think about modular design and route reuse with different prefixes.