0
0
FastAPIframework~20 mins

Including routers in main app in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Router 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 /items/42?

Given this FastAPI app with a router included, what will be the JSON response when you visit /items/42?

FastAPI
from fastapi import FastAPI, APIRouter

app = FastAPI()

items_router = APIRouter()

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

app.include_router(items_router)
A404 Not Found error
B{"detail": "Not Found"}
C{"item_id": 42, "name": "Item 42"}
D{"item_id": "42", "name": "Item 42"}
Attempts:
2 left
💡 Hint

Check how the router path and parameter types match the request URL.

📝 Syntax
intermediate
2:00remaining
Which option correctly includes a router with prefix '/users'?

Choose the correct way to include a router named users_router in the main FastAPI app with the prefix /users.

FastAPI
from fastapi import FastAPI, APIRouter

app = FastAPI()
users_router = APIRouter()

# Assume users_router has some routes defined

# Which line correctly includes the router with prefix '/users'?
Aapp.include(users_router, prefix='/users')
Bapp.include_router(users_router, prefix='/users')
Capp.add_router(users_router, prefix='/users')
Dapp.include_router(users_router, path='/users')
Attempts:
2 left
💡 Hint

Check the FastAPI method name and parameter for adding routers with prefixes.

🔧 Debug
advanced
2:00remaining
Why does this router path not respond as expected?

Consider this router code included in the main app. Why does accessing /api/data return 404?

FastAPI
from fastapi import FastAPI, APIRouter

app = FastAPI()

api_router = APIRouter(prefix='/api')

@api_router.get('/data')
async def get_data():
    return {"data": "value"}

app.include_router(api_router, prefix='/api')
AThe route path becomes '/api/api/data' due to double prefix, so '/api/data' is not found.
BThe router prefix is ignored, so '/data' is the actual path.
CThe function get_data is missing async keyword causing runtime error.
DThe app.include_router call is missing parentheses.
Attempts:
2 left
💡 Hint

Check how prefixes combine when including routers.

state_output
advanced
2:00remaining
What is the response after calling /counter twice?

Given this FastAPI app with a router that keeps a counter, what JSON response do you get on the second call to /counter?

FastAPI
from fastapi import FastAPI, APIRouter

app = FastAPI()
counter_router = APIRouter()

count = 0

@counter_router.get('/counter')
async def get_counter():
    global count
    count += 1
    return {"count": count}

app.include_router(counter_router)
A{"count": 0}
B{"count": 1}
C500 Internal Server Error due to global variable
D{"count": 2}
Attempts:
2 left
💡 Hint

Think about how the global variable changes with each request.

🧠 Conceptual
expert
2:00remaining
What error occurs if you include the same router twice without a prefix?

What happens if you include the same APIRouter instance twice in a FastAPI app without using different prefixes?

FastAPI
from fastapi import FastAPI, APIRouter

app = FastAPI()

router = APIRouter()

@router.get('/hello')
async def say_hello():
    return {"msg": "Hello"}

app.include_router(router)
app.include_router(router)
ARuntimeError: Duplicate route path '/hello' detected
BThe second include_router call is ignored silently
CThe app runs but only one route is registered
DSyntaxError due to duplicate function definitions
Attempts:
2 left
💡 Hint

Think about route conflicts when including routers multiple times.