📖 Scenario: You are building a simple web API using FastAPI. To keep your code organized, you want to separate different parts of your API into routers. Routers help you group related routes together, like having different rooms for different activities in a house.
🎯 Goal: Build a FastAPI app that includes a router from another module. The router will have one route that returns a greeting message. You will create the router, then include it in the main app.
📋 What You'll Learn
Create a router in a separate module called items.py.
The router should have a GET route at / that returns a JSON message.
In the main app file main.py, create a FastAPI app instance.
Include the router from items.py into the main app with the prefix /items.
💡 Why This Matters
🌍 Real World
Organizing routes into routers helps keep large FastAPI projects clean and maintainable, similar to organizing files in folders.
💼 Career
Knowing how to modularize FastAPI apps with routers is essential for backend developers building scalable APIs.
Progress0 / 4 steps
1
Create a router in items.py
In a file named items.py, import APIRouter from fastapi. Then create a router called router using APIRouter(). Add a GET route at / on router that returns a dictionary with the key message and value "Hello from items router".
FastAPI
Hint
Remember to import APIRouter and create a router instance. Use @router.get("/") to define the route.
2
Create the main FastAPI app in main.py
In main.py, import FastAPI from fastapi. Create an app instance called app using FastAPI().
FastAPI
Hint
Import FastAPI and create an app instance named app.
3
Import the router from items.py
In main.py, import the router from the items module.
FastAPI
Hint
Use from items import router to bring the router into main.py.
4
Include the router in the main app with prefix
In main.py, use app.include_router() to add the imported router to the app. Use the prefix "/items" so the router's routes start with /items.
FastAPI
Hint
Call app.include_router(router, prefix="/items") to add the router under the /items path.
Practice
(1/5)
1. What is the main purpose of using include_router in a FastAPI application?
easy
A. To handle user authentication automatically.
B. To automatically generate HTML templates for the app.
C. To connect the app to a database.
D. To split the app into smaller, manageable parts for better organization.
Solution
Step 1: Understand the role of routers in FastAPI
Routers help organize routes by grouping related endpoints together.
Step 2: Recognize what include_router does
It adds these grouped routes into the main app, keeping code clean and modular.
Final Answer:
To split the app into smaller, manageable parts for better organization. -> Option D
Quick Check:
Router inclusion = app organization [OK]
Hint: Routers help organize routes; include_router adds them to main app [OK]
Common Mistakes:
Thinking include_router generates HTML templates
Confusing routers with database connections
Assuming include_router handles authentication
2. Which of the following is the correct way to include a router named user_router with prefix /users in a FastAPI app named app?
easy
A. app.add_router(user_router, prefix='/users')
B. app.include_router(user_router, prefix='/users')
C. app.include(user_router, prefix='/users')
D. app.router_include(user_router, prefix='/users')
Solution
Step 1: Recall the correct method name for adding routers
The method is include_router, not add_router or others.
Step 2: Check the syntax for prefix argument
The prefix is passed as a keyword argument prefix='/users'.
Final Answer:
app.include_router(user_router, prefix='/users') -> Option B
Quick Check:
Method name is include_router with prefix argument [OK]
Hint: Use include_router with prefix keyword to add routers [OK]
Common Mistakes:
Using wrong method names like add_router
Passing prefix as positional argument
Misspelling include_router
3. Given this code snippet, what will be the URL path to access the get_items endpoint?
Hint: Avoid including the same router twice with same prefix [OK]
Common Mistakes:
Thinking router must be included before endpoints
Believing prefix is mandatory or must be omitted
Confusing variable naming with functionality
5. You have two routers: user_router with prefix /users and item_router with prefix /items. You want to include both in your FastAPI app and add tags for automatic docs grouping. Which code snippet correctly includes both routers with tags?
hard
A. app.include_router(user_router, prefix='/users', tags='users')
app.include_router(item_router, prefix='/items', tags='items')
B. app.include_router(user_router, prefix='/users')
app.include_router(item_router, prefix='/items')
app.tags = ['users', 'items']
C. app.include_router(user_router, prefix='/users', tags=['users'])
app.include_router(item_router, prefix='/items', tags=['items'])
D. app.include_router(user_router, tags=['users'])
app.include_router(item_router, tags=['items'])
Solution
Step 1: Check how to add tags when including routers
Tags are passed as a list to the tags argument in include_router.
Step 2: Verify prefix and tags usage for both routers
Each router is included with its prefix and tags as lists, e.g., tags=['users'].
Final Answer:
app.include_router(user_router, prefix='/users', tags=['users'])
app.include_router(item_router, prefix='/items', tags=['items']) -> Option C
Quick Check:
Tags must be lists in include_router calls [OK]
Hint: Pass tags as list when including routers for docs grouping [OK]
Common Mistakes:
Passing tags as string instead of list
Setting tags on app instead of routers
Omitting tags argument entirely
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.