Bird
Raised Fist0
FastAPIframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using a prefix in a FastAPI router?
easy
A. To add authentication to routes automatically
B. To group routes under a common URL path segment
C. To change the HTTP method of all routes
D. To disable routes temporarily

Solution

  1. Step 1: Understand what prefix does in FastAPI routers

    The prefix adds a common path segment to all routes defined in that router, grouping them logically.
  2. Step 2: Compare options with prefix purpose

    Only To group routes under a common URL path segment correctly describes grouping routes under a shared URL path segment.
  3. Final Answer:

    To group routes under a common URL path segment -> Option B
  4. Quick Check:

    Router prefix = group routes [OK]
Hint: Prefix groups routes by URL path segment [OK]
Common Mistakes:
  • Thinking prefix changes HTTP methods
  • Confusing prefix with authentication
  • Assuming prefix disables routes
2. Which of the following is the correct way to add a prefix and tags to a FastAPI router?
easy
A. router = APIRouter(prefix='/items', tags=['items'])
B. router = APIRouter(tags='/items', prefix=['items'])
C. router = APIRouter(prefix=['items'], tags='/items')
D. router = APIRouter(prefix='/items', tags='/items')

Solution

  1. Step 1: Recall correct parameter types for prefix and tags

    Prefix expects a string starting with '/', tags expects a list of strings.
  2. Step 2: Check each option's syntax

    router = APIRouter(prefix='/items', tags=['items']) uses prefix as string and tags as list correctly; others mix types incorrectly.
  3. Final Answer:

    router = APIRouter(prefix='/items', tags=['items']) -> Option A
  4. Quick Check:

    Prefix string + tags list = correct syntax [OK]
Hint: Prefix is string, tags is list of strings [OK]
Common Mistakes:
  • Using list for prefix instead of string
  • Using string for tags instead of list
  • Mixing order of parameters incorrectly
3. Given this code snippet, what is the full path of the endpoint?
from fastapi import APIRouter

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

@router.get('/profile')
async def get_profile():
    return {'msg': 'User profile'}
medium
A. /profile/users
B. /profile
C. /users
D. /users/profile

Solution

  1. Step 1: Identify router prefix and route path

    The router has prefix '/users' and the route path is '/profile'.
  2. Step 2: Combine prefix and route path

    Full path is prefix + route path = '/users' + '/profile' = '/users/profile'.
  3. Final Answer:

    /users/profile -> Option D
  4. Quick Check:

    Prefix + route path = full endpoint path [OK]
Hint: Full path = prefix + route path [OK]
Common Mistakes:
  • Ignoring prefix and using only route path
  • Swapping prefix and route path order
  • Confusing tags with path
4. What is wrong with this FastAPI router code?
router = APIRouter(prefix=['/items'], tags='items')
medium
A. prefix should be a list, tags should be a string
B. Both prefix and tags should be strings
C. prefix should be a string, tags should be a list of strings
D. No error, this is valid syntax

Solution

  1. Step 1: Check expected types for prefix and tags

    Prefix must be a string starting with '/', tags must be a list of strings.
  2. Step 2: Analyze given code

    Prefix is given as a list, tags as a string, both reversed from expected types.
  3. Final Answer:

    prefix should be a string, tags should be a list of strings -> Option C
  4. Quick Check:

    Prefix = string, tags = list [OK]
Hint: Prefix string, tags list; reverse causes error [OK]
Common Mistakes:
  • Using list for prefix
  • Using string for tags
  • Assuming both can be strings
5. You want to create two routers in FastAPI: one for /products tagged 'products' and another for /orders tagged 'orders'. How do you correctly set prefixes and tags to keep your API organized?
hard
A. router_products = APIRouter(prefix='/products', tags=['products'])\nrouter_orders = APIRouter(prefix='/orders', tags=['orders'])
B. router_products = APIRouter(prefix=['products'], tags='products')\nrouter_orders = APIRouter(prefix=['orders'], tags='orders')
C. router_products = APIRouter(prefix='/products', tags='products')\nrouter_orders = APIRouter(prefix='/orders', tags='orders')
D. router_products = APIRouter(tags=['products'])\nrouter_orders = APIRouter(prefix='/orders')

Solution

  1. Step 1: Recall correct types for prefix and tags

    Prefix must be a string starting with '/', tags must be a list of strings.
  2. Step 2: Evaluate each option for correctness and organization

    router_products = APIRouter(prefix='/products', tags=['products'])\nrouter_orders = APIRouter(prefix='/orders', tags=['orders']) correctly assigns string prefixes and list tags for both routers, organizing API paths and documentation clearly.
  3. Final Answer:

    router_products = APIRouter(prefix='/products', tags=['products']) router_orders = APIRouter(prefix='/orders', tags=['orders']) -> Option A
  4. Quick Check:

    Correct prefix and tags keep API organized [OK]
Hint: Use string prefix and list tags for each router [OK]
Common Mistakes:
  • Using list for prefix or string for tags
  • Omitting prefix or tags on one router
  • Mixing tags and prefix types
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard