📖 Scenario: You are building a simple web API for a bookstore. The API needs to get book details by their ID and filter books by their rating.
🎯 Goal: Create a FastAPI app that uses path parameters with specific types and validation to ensure correct input from users.
📋 What You'll Learn
Create a FastAPI app instance named app
Define a path parameter book_id as an integer in the route /books/{book_id}
Add validation to book_id so it must be greater than 0
Define a path parameter rating as a float in the route /books/rating/{rating}
Add validation to rating so it must be between 0.0 and 5.0 inclusive
Return simple JSON responses confirming the received parameters
💡 Why This Matters
🌍 Real World
APIs often need to accept user input in URLs. Validating these inputs ensures the API works correctly and securely.
💼 Career
Understanding path parameter types and validation is essential for backend developers building reliable web services with FastAPI.
Progress0 / 4 steps
1
Create FastAPI app and integer path parameter
Import FastAPI from fastapi and create an app instance called app. Then define a GET route /books/{book_id} with a path parameter book_id of type int. The function should return a dictionary with the key book_id and its value.
FastAPI
Hint
Use app = FastAPI() to create the app. Define a function with book_id: int as parameter and decorate it with @app.get("/books/{book_id}").
2
Add validation for book_id to be greater than 0
Import Path from fastapi. Update the book_id parameter to use Path(..., gt=0) to enforce that book_id must be greater than 0.
FastAPI
Hint
Use Path(..., gt=0) to require book_id to be greater than zero.
3
Add float path parameter with range validation
Define a new GET route /books/rating/{rating} with a path parameter rating of type float. Use Path(..., ge=0.0, le=5.0) to validate that rating is between 0.0 and 5.0 inclusive. The function should return a dictionary with the key rating and its value.
FastAPI
Hint
Define a new async function with rating: float = Path(..., ge=0.0, le=5.0) and decorate it with @app.get("/books/rating/{rating}").
4
Complete FastAPI app with both routes and validation
Ensure the FastAPI app has both routes: /books/{book_id} with book_id as an integer path parameter validated to be greater than 0, and /books/rating/{rating} with rating as a float path parameter validated between 0.0 and 5.0 inclusive. Both routes should return JSON with the received parameter.
FastAPI
Hint
Make sure both routes and their validations are present in the app code.
Practice
(1/5)
1. What is the main purpose of specifying a type for a path parameter in FastAPI?
easy
A. To disable validation for that parameter
B. To make the parameter optional in the URL
C. To automatically generate HTML forms
D. To ensure the parameter value matches the expected data type from the URL
Solution
Step 1: Understand path parameters in FastAPI
Path parameters extract values from the URL and can be typed to ensure correct data types.
Step 2: Role of typing in path parameters
Typing enforces that the value received matches the expected type, preventing errors and invalid data.
Final Answer:
To ensure the parameter value matches the expected data type from the URL -> Option D
Quick Check:
Path parameter typing = type safety [OK]
Hint: Path parameter types check URL values match expected type [OK]
Common Mistakes:
Thinking typing makes parameters optional
Believing typing disables validation
Confusing path parameters with query parameters
2. Which of the following is the correct way to declare an integer path parameter with a minimum value of 1 in FastAPI?
easy
A. def read_item(item_id: int = Path(min=1)):
B. def read_item(item_id: int = Path(minimum=1)):
C. def read_item(item_id: int = Path(ge=1)):
D. def read_item(item_id: int = Path(min_value=1)):
A. Using gt=0 with a string type causes a validation error
B. Missing type annotation for item_id
C. Path() cannot be used with string parameters
D. The route path should not include curly braces
Solution
Step 1: Check parameter type and validation
The parameter item_id is typed as str but uses gt=0 which means greater than 0, a numeric constraint.
Step 2: Understand validation mismatch
Numeric validations like gt apply only to numbers, not strings, causing a validation error.
Final Answer:
Using gt=0 with a string type causes a validation error -> Option A
Quick Check:
Numeric validation on string param = error [OK]
Hint: Numeric validators only work with numeric types [OK]
Common Mistakes:
Applying numeric validation to strings
Forgetting to annotate type
Misunderstanding route path syntax
5. You want to create a FastAPI endpoint that accepts a path parameter product_id which must be an integer between 100 and 999 inclusive. If the value is invalid, FastAPI should reject the request automatically. Which of the following is the best way to declare this parameter?
hard
A. def get_product(product_id: int = Path(min=100, max=999)):
B. def get_product(product_id: int = Path(ge=100, le=999)):
C. def get_product(product_id: int = Path(gt=100, lt=999)):
D. def get_product(product_id: int = Path(minimum=100, maximum=999)):
Solution
Step 1: Recall correct validation keywords in Path()
FastAPI uses ge (greater or equal) and le (less or equal) for inclusive range validation.
Step 2: Analyze each option's keywords
def get_product(product_id: int = Path(ge=100, le=999)): uses ge=100 and le=999 which matches the requirement exactly. The remaining options use invalid keywords or exclusive bounds. def get_product(product_id: int = Path(gt=100, lt=999)): uses exclusive bounds which exclude 100 and 999.
Final Answer:
def get_product(product_id: int = Path(ge=100, le=999)): -> Option B
Quick Check:
Inclusive range validation = ge and le [OK]
Hint: Use ge and le for inclusive min and max in Path() [OK]
Common Mistakes:
Using min/max instead of ge/le
Using gt/lt for inclusive bounds
Using invalid keyword names
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.