📖 Scenario: You are building a simple web API for a bookstore. You want to organize your code so that different parts of the API handle different tasks clearly and neatly.
🎯 Goal: Build a FastAPI app that uses routing to organize endpoints for books and authors separately.
📋 What You'll Learn
Create a FastAPI app instance called app
Create two routers: books_router and authors_router
Add one GET endpoint to books_router at path /books/ that returns a list of book titles
Add one GET endpoint to authors_router at path /authors/ that returns a list of author names
Include both routers in the main app with prefixes /books and /authors respectively
💡 Why This Matters
🌍 Real World
Web APIs often have many endpoints. Routing helps keep code organized and scalable.
💼 Career
Understanding routing is essential for backend developers building APIs with FastAPI or similar frameworks.
Progress0 / 4 steps
1
Create the FastAPI app and routers
Import FastAPI and APIRouter from fastapi. Create a FastAPI app instance called app. Create two routers called books_router and authors_router using APIRouter().
FastAPI
Hint
Use app = FastAPI() to create the app. Use APIRouter() to create routers.
2
Add GET endpoint to books_router
Add a GET endpoint to books_router at path /books/ that returns a list of book titles: ["The Hobbit", "1984", "Pride and Prejudice"]. Use the decorator @books_router.get("/") and define a function called get_books.
FastAPI
Hint
Use @books_router.get("/") to create the endpoint. Return the exact list of book titles.
3
Add GET endpoint to authors_router
Add a GET endpoint to authors_router at path /authors/ that returns a list of author names: ["J.R.R. Tolkien", "George Orwell", "Jane Austen"]. Use the decorator @authors_router.get("/") and define a function called get_authors.
FastAPI
Hint
Use @authors_router.get("/") to create the endpoint. Return the exact list of author names.
4
Include routers in the main app with prefixes
Include books_router in app with prefix /books. Include authors_router in app with prefix /authors. Use app.include_router() for both.
FastAPI
Hint
Use app.include_router() with the correct router and prefix.
Practice
(1/5)
1. Why does FastAPI use routing to organize endpoints in an application?
easy
A. To create user interfaces without HTML
B. To make the app run faster by skipping functions
C. To connect specific URLs to the functions that handle them
D. To store data in a database automatically
Solution
Step 1: Understand routing purpose
Routing links URLs (web addresses) to the code that runs when those URLs are visited.
Step 2: Match routing with function handling
FastAPI uses routing to tell which function should respond to each URL.
Final Answer:
To connect specific URLs to the functions that handle them -> Option C
Quick Check:
Routing = URL to function link [OK]
Hint: Routing links URLs to functions handling requests [OK]
Common Mistakes:
Thinking routing speeds up the app by skipping code
Confusing routing with database storage
Believing routing creates user interfaces
2. Which of the following is the correct way to define a GET route in FastAPI for the URL /items/?
easy
A. @app.get('/items')
def get_items():
return {'items': []}
B. @app.route('/items/')
def get_items():
return {'items': []}
C. @app.get_items('/items/')
def get_items():
return {'items': []}
D. @app.get('/items/')
def get_items():
return {'items': []}
Solution
Step 1: Identify FastAPI route decorator
FastAPI uses @app.get() to define GET routes, not @app.route() or @app.get_items().
Step 2: Check URL syntax
Both /items/ and /items work, but the question asks for /items/ exactly, so the decorator must match.
Final Answer:
@app.get('/items/')
def get_items():
return {'items': []} -> Option D
Quick Check:
Use @app.get() for GET routes [OK]
Hint: Use @app.get() decorator for GET routes [OK]
Common Mistakes:
Using @app.route() which is not FastAPI syntax
Using incorrect decorator names like @app.get_items()
Missing trailing slash when required
3. What will be the output when accessing the URL /hello in this FastAPI app?