A FastAPI application demonstrating how to combine path parameters and query parameters in a single endpoint.
- FastAPI framework with automatic OpenAPI documentation
- Demonstrates combining path and query parameters
- Shows multiple path parameters in a single route
- Includes optional query parameters with default values
- Interactive API documentation at
/docsand/redoc - Python 3.7+ compatibility
- Virtual environment setup
- Python 3.7 or higher
- pip (Python package manager)
-
Clone the repository
git clone https://github.com/Kirankumarvel/fastapi-combined-params-demo.git cd fastapi-combined-params-demo -
Create a virtual environment
python -m venv venv
-
Activate the virtual environment
- Windows:
venv\Scripts\activate
- macOS/Linux:
source venv/bin/activate
- Windows:
-
Install dependencies
pip install -r requirements.txt
The project uses the following main dependencies:
fastapi- The web framework for building APIsuvicorn- ASGI server for running FastAPI applications
To generate requirements.txt:
pip freeze > requirements.txt-
Start the development server
uvicorn main:app --reload --reload-exclude venv
-
Access the application
- API: http://127.0.0.1:8000
- Interactive docs: http://127.0.0.1:8000/docs
- Alternative docs: http://127.0.0.1:8000/redoc
Returns product item details with combined path and query parameters.
Path Parameters:
category(string): Product category (e.g., "electronics", "clothing")item_id(int): Unique identifier for the item
Query Parameters:
urgent(bool, optional): Urgency flag (default: false)discount(float, optional): Discount percentage to apply (default: 0.0)
Examples:
# Basic request with only path parameters
curl "http://127.0.0.1:8000/products/electronics/items/123"
# With optional query parameters
curl "http://127.0.0.1:8000/products/electronics/items/123?urgent=true"
curl "http://127.0.0.1:8000/products/electronics/items/123?discount=15.5"
# With all parameters
curl "http://127.0.0.1:8000/products/electronics/items/123?urgent=true&discount=15.5"Sample Responses:
// Basic response
{
"category": "electronics",
"item_id": 123,
"urgent": false,
"discount_applied": 0.0
}
// With query parameters
{
"category": "electronics",
"item_id": 123,
"urgent": true,
"discount_applied": 15.5
}- Syntax:
{parameter_name}in the URL path - Required: Always mandatory
- Position: Part of the URL structure
- Example:
/products/{category}/items/{item_id}
- Syntax:
?key=value&key2=value2after the URL path - Optional: Can have default values
- Position: After the
?in the URL - Example:
?urgent=true&discount=15.5
- Parameters defined in the path β Path parameters
- Parameters not in the path β Query parameters
- Automatic type validation for both
- Comprehensive documentation in OpenAPI schema
-
Path parameters only:
curl "http://127.0.0.1:8000/products/electronics/items/123" curl "http://127.0.0.1:8000/products/clothing/items/456"
-
With query parameters:
curl "http://127.0.0.1:8000/products/electronics/items/123?urgent=true" curl "http://127.0.0.1:8000/products/electronics/items/123?discount=10.0" curl "http://127.0.0.1:8000/products/electronics/items/123?urgent=true&discount=15.5"
-
Interactive testing: Use the Swagger UI at
/docsto test various combinations
fastapi-combined-params-demo/
βββ main.py # Main application file
βββ requirements.txt # Project dependencies
βββ README.md # Project documentation
βββ venv/ # Virtual environment (gitignored)
from fastapi import FastAPI
app = FastAPI()
@app.get("/products/{category}/items/{item_id}")
async def read_product_item(
category: str, # Path parameter (required)
item_id: int, # Path parameter (required)
urgent: bool = False, # Query parameter (optional)
discount: float = 0.0 # Query parameter (optional)
):
"""
Get product item details with combined path and query parameters.
- category: Product category (path parameter, required)
- item_id: Item identifier (path parameter, required)
- urgent: Urgency flag (query parameter, optional, default: false)
- discount: Discount percentage (query parameter, optional, default: 0.0)
"""
item = {
"category": category,
"item_id": item_id,
"urgent": urgent,
"discount_applied": discount
}
# You would typically fetch this data from a database
return item- Path parameters:
categoryanditem_id(no default values = required) - Query parameters:
urgentanddiscount(with default values = optional) - Automatic type conversion: FastAPI converts string values to appropriate types
- Validation: Invalid types return descriptive error messages
- Path Parameters: For essential, identifying information in URL structure
- Query Parameters: For optional modifications, filtering, and flags
- Type Safety: FastAPI validates both path and query parameters
- Default Values: Make query parameters optional
- API Design: Strategic use of both parameter types for clean API design
-
Type conversion errors
- Ensure correct parameter types (e.g.,
item_idmust be integer)
- Ensure correct parameter types (e.g.,
-
Missing required path parameters
- All path parameters must be provided in the URL
-
URL encoding
- Special characters in path parameters may need URL encoding
-
Boolean parameter values
- Use
true/falseor1/0for boolean parameters
- Use
-
Reload issues
uvicorn main:app --reload --reload-exclude venv
- FastAPI Path Parameters
- FastAPI Query Parameters
- FastAPI Parameter Combinations
- Uvicorn Documentation
- Fork the project
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- FastAPI team for the excellent framework
- Uvicorn team for the ASGI server
- Python community for ongoing support