0
0
FastAPIframework~10 mins

APIRouter for modular routes in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import APIRouter from FastAPI.

FastAPI
from fastapi import [1]
Drag options to blanks, or click blank then click option'
ADepends
BFastAPI
CRequest
DAPIRouter
Attempts:
3 left
💡 Hint
Common Mistakes
Importing FastAPI instead of APIRouter
Using Request or Depends which are unrelated here
2fill in blank
medium

Complete the code to create a new APIRouter instance.

FastAPI
router = [1]()
Drag options to blanks, or click blank then click option'
AAPIRouter
BFastAPI
CDepends
DRequest
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to create FastAPI instance instead of APIRouter
Using Depends or Request which are not constructors
3fill in blank
hard

Fix the error in the route decorator to use the router instead of app.

FastAPI
@[1].get("/items")
async def read_items():
    return [{"item_id": "foo"}]
Drag options to blanks, or click blank then click option'
Aapp
BDepends
Crouter
DFastAPI
Attempts:
3 left
💡 Hint
Common Mistakes
Using @app.get instead of @router.get
Using FastAPI or Depends in decorator
4fill in blank
hard

Fill both blanks to include the router in the main FastAPI app.

FastAPI
from fastapi import FastAPI

app = FastAPI()
app.[1](router, prefix=[2])
Drag options to blanks, or click blank then click option'
Ainclude_router
Badd_route
C"/api"
D"/items"
Attempts:
3 left
💡 Hint
Common Mistakes
Using add_route which is not for routers
Wrong prefix string or missing quotes
5fill in blank
hard

Fill all three blanks to define a router with a GET route and include it in the app with prefix.

FastAPI
from fastapi import FastAPI, [1]

router = [1]()

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

app = FastAPI()
app.[2](router, prefix=[3])
Drag options to blanks, or click blank then click option'
AAPIRouter
Binclude_router
C"/greet"
DFastAPI
Attempts:
3 left
💡 Hint
Common Mistakes
Using FastAPI instead of APIRouter for router
Wrong method name to include router
Missing quotes around prefix