📖 Scenario: You are building a simple web API for a book store. You want to create an endpoint that requires users to provide the book_id as a query parameter to get details about a specific book.
🎯 Goal: Create a FastAPI endpoint that requires a book_id query parameter. The endpoint should return a JSON response with the book_id provided.
📋 What You'll Learn
Create a FastAPI app instance named app
Create a GET endpoint at path /book
Require a query parameter named book_id of type int
Return a dictionary with the key book_id and the value from the query parameter
💡 Why This Matters
🌍 Real World
APIs often require clients to send specific information as query parameters to fetch or filter data. This project shows how to enforce required query parameters in FastAPI.
💼 Career
Knowing how to handle required query parameters is essential for backend developers building RESTful APIs with FastAPI, a popular modern Python framework.
Progress0 / 4 steps
1
Create FastAPI app instance
Import FastAPI from fastapi and create an app instance called app.
FastAPI
Hint
Use FastAPI() to create the app instance and assign it to app.
2
Define GET endpoint with required query parameter
Define a GET endpoint at path /book using @app.get("/book"). Add a function named get_book that takes a required query parameter book_id of type int.
FastAPI
Hint
Use function parameter typing to require book_id as an integer query parameter.
3
Return book_id in response
Inside the get_book function, return a dictionary with the key "book_id" and the value from the book_id parameter.
FastAPI
Hint
Return a dictionary with "book_id" as key and the function parameter book_id as value.
4
Add type hint and run the app
Ensure the book_id parameter is typed as int and the app is ready to run. (No extra code needed to run here, just confirm the final code structure.)
FastAPI
Hint
The code is complete and ready to run with uvicorn or similar server.
Practice
(1/5)
1. In FastAPI, how do you make a query parameter required so the client must provide it in the URL?
easy
A. Use Optional type hint
B. Set the parameter default to None
C. Use Query(...) in the function parameter
D. Use Body(...) instead of query
Solution
Step 1: Understand FastAPI query parameters
FastAPI uses function parameters to define query parameters automatically.
Step 2: Use Query(...) to require parameter
Using Query(...) tells FastAPI this parameter is required and must be in the URL.
Final Answer:
Use Query(...) in the function parameter -> Option C
Quick Check:
Required query parameter = Query(...) [OK]
Hint: Use Query(...) to make query parameters required [OK]
Common Mistakes:
Setting default to None makes parameter optional
Using Optional type hint allows missing parameter
Using Body(...) defines body, not query parameter
2. Which of the following is the correct syntax to declare a required query parameter named item_id of type int in FastAPI?
easy
A. def read_item(item_id: int = Query(None)):
B. def read_item(item_id: int = None):
C. def read_item(item_id: int = Body(...)):
D. def read_item(item_id: int = Query(...)):
Solution
Step 1: Identify required query parameter syntax
To make a query parameter required, use Query(...) as the default value.
Step 2: Check each option
def read_item(item_id: int = Query(...)): uses Query(...) correctly, making item_id required. def read_item(item_id: int = Query(None)): uses Query(None) which makes it optional. def read_item(item_id: int = None): uses default None without Query, optional. def read_item(item_id: int = Body(...)): uses Body(...) which makes it a required body field, not query parameter.
Final Answer:
def read_item(item_id: int = Query(...)): -> Option D
Quick Check:
Required query param = Query(...) [OK]
Hint: Use Query(...) as default to require query parameter [OK]
5. You want to create a FastAPI endpoint that requires two query parameters: category (string) and limit (integer). Both must be required. Which code snippet correctly enforces this?
hard
A. async def get_items(category: str = Query(...), limit: int = Query(...)):
B. async def get_items(category: str = None, limit: int = None):
C. async def get_items(category: str, limit: int = Query(None)):
D. async def get_items(category: str = Query(None), limit: int = Query(...)):
Solution
Step 1: Understand required query parameters
Both parameters must use Query(...) to be required.
Step 2: Evaluate each option
async def get_items(category: str = Query(...), limit: int = Query(...)): correctly uses Query(...) for both. async def get_items(category: str = None, limit: int = None) uses None defaults, making them optional. async def get_items(category: str, limit: int = Query(None)) makes only limit optional. async def get_items(category: str = Query(None), limit: int = Query(...)) makes category optional.
Final Answer:
async def get_items(category: str = Query(...), limit: int = Query(...)): -> Option A
Quick Check:
Both required = Query(...) for each [OK]
Hint: Use Query(...) for each required query parameter [OK]
Common Mistakes:
Setting default None makes parameter optional
Mixing required and optional unintentionally
Omitting Query makes parameter a path parameter
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.