0
0
FastAPIframework~20 mins

Router prefix and tags in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Router Prefix and Tags Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the full path of the endpoint?
Given this FastAPI router setup, what is the full URL path to access the read_items endpoint?

from fastapi import APIRouter

router = APIRouter(prefix="/items", tags=["items"])

@router.get("/")
async def read_items():
    return {"msg": "List of items"}
FastAPI
from fastapi import APIRouter

router = APIRouter(prefix="/items", tags=["items"])

@router.get("/")
async def read_items():
    return {"msg": "List of items"}
A/items/read_items
B/read_items/
C/items/
D/
Attempts:
2 left
💡 Hint
Remember the prefix adds before the route path.
state_output
intermediate
2:00remaining
What tags appear in the OpenAPI docs?
Consider this FastAPI router code. Which tags will appear in the OpenAPI documentation for the read_users endpoint?

from fastapi import APIRouter

router = APIRouter(prefix="/users", tags=["users"])

@router.get("/", tags=["custom"])
async def read_users():
    return {"msg": "List of users"}
FastAPI
from fastapi import APIRouter

router = APIRouter(prefix="/users", tags=["users"])

@router.get("/", tags=["custom"])
async def read_users():
    return {"msg": "List of users"}
A["custom"]
B["users"]
C["users", "custom"]
D[]
Attempts:
2 left
💡 Hint
Endpoint tags override router tags if provided.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in router prefix usage
Which option contains a syntax error when defining a FastAPI router with a prefix and tags?

Code snippet:
from fastapi import APIRouter

router = APIRouter(prefix=PREFIX, tags=TAG_LIST)

Assume PREFIX and TAG_LIST are variables.
FastAPI
from fastapi import APIRouter

router = APIRouter(prefix=PREFIX, tags=TAG_LIST)
A
PREFIX = "/api"
TAG_LIST = ["api"]
B
PREFIX = "/api"
TAG_LIST = {"api"}
C
PREFIX = "/api"
TAG_LIST = ("api",)
D
PREFIX = "/api"
TAG_LIST = "api"
Attempts:
2 left
💡 Hint
Tags must be a list of strings, not a single string.
🔧 Debug
advanced
2:00remaining
Why does the endpoint not appear under the expected tag?
You have this FastAPI router:

from fastapi import APIRouter

router = APIRouter(prefix="/products", tags=["products"])

@router.get("/list", tags=[])
async def list_products():
    return {"msg": "Products list"}

But in the OpenAPI docs, the endpoint appears under the default tag instead of "products". Why?
FastAPI
from fastapi import APIRouter

router = APIRouter(prefix="/products", tags=["products"])

@router.get("/list", tags=[])
async def list_products():
    return {"msg": "Products list"}
AThe endpoint has its own tags parameter set to an empty list, overriding router tags.
BThe router tags parameter must be a string, not a list.
CThe router prefix is incorrect, so tags are ignored.
DThe router tags are ignored if the endpoint path starts with a slash.
Attempts:
2 left
💡 Hint
Check if the endpoint has tags set explicitly.
🧠 Conceptual
expert
3:00remaining
How does router prefix affect nested routers?
Consider two routers in FastAPI:

from fastapi import APIRouter

parent_router = APIRouter(prefix="/parent", tags=["parent"])
child_router = APIRouter(prefix="/child", tags=["child"])

@child_router.get("/endpoint")
async def child_endpoint():
    return {"msg": "Child endpoint"}

parent_router.include_router(child_router)

What is the full path and tags shown in OpenAPI docs for child_endpoint?
FastAPI
from fastapi import APIRouter

parent_router = APIRouter(prefix="/parent", tags=["parent"])
child_router = APIRouter(prefix="/child", tags=["child"])

@child_router.get("/endpoint")
async def child_endpoint():
    return {"msg": "Child endpoint"}

parent_router.include_router(child_router)
APath: /parent/child/endpoint, Tags: ["parent", "child"]
BPath: /parent/child/endpoint, Tags: ["child"]
CPath: /child/endpoint, Tags: ["child"]
DPath: /parent/endpoint, Tags: ["parent"]
Attempts:
2 left
💡 Hint
Prefixes combine when routers are nested; tags do not merge automatically.