Types of Path Parameters in FastAPI
To ensure the accuracy of provided values and enforce expected data types in FastAPI, path parameters with specific types can be employed. This practice facilitates validation, guarding against potential issues arising from the use of incorrect parameter types. Here’s an illustrative example of utilizing path parameters with types in FastAPI:
String Path Parameter
In this example, the path parameter item_id
is of type str
Python3
from fastapi import FastAPI app = FastAPI() @app .get( "/items/{item_id}" ) async def read_item(item_id: str ): return { "item_id" : item_id} |
Run the below command
uvicorn main:app --reload
Output
Integer Path Parameter
In this case, the path parameter item_id
is of type int
Python3
from fastapi import FastAPI app = FastAPI() @app .get( "/items/{item_id}" ) async def read_item(item_id: int ): return { "item_id" : item_id} |
Run the below command
uvicorn main:app --reload
Output
Float Path Parameter
The path parameter item_price
is of type float
Python3
from fastapi import FastAPI app = FastAPI() @app .get( "/items/{item_price}" ) async def read_item(item_price: float ): return { "item_price" : item_price} |
Output
Custom Data Type Path Parameter
In this example , a custom data type (Path
) is used for the path parameter item_name
, with additional metadata like a title
Python3
from fastapi import FastAPI, Path app = FastAPI() @app .get( "/items/{item_name}" ) async def read_item(item_name: str = Path(..., title = "The name of the item" )): return { "item_name" : item_name} |
Run the below command :
uvicorn main:app --reload
Output
Example
This Python code uses FastAPI to create a basic web API with two asynchronous endpoints. The first endpoint (“/”) responds to a GET request with a JSON message “Hello World.” The second endpoint (“/gfg/{name}”) responds to a GET request with a JSON message containing the provided name parameter. FastAPI handles routing and generates API documentation based on function signatures. The code runs the web application using the ASGI server, Uvicorn.
Python3
import uvicorn from fastapi import FastAPI app = FastAPI() @app .get( "/" ) async def index(): return { "message" : "Hello World" } @app .get( "/gfg/{name}" ) async def gfg(name): return { "name" : name} |
Now, run the UVICORN server in terminal using below command
uvicorn main:app --reload
Output
Conclusion
In conclusion, FastAPI’s robust support for path parameters provides a powerful mechanism for handling dynamic data in API routes. Path parameters, coupled with type annotations, not only enhance code clarity but also contribute to effective data validation and error prevention. The framework’s automatic documentation generation based on parameter types further aids developers in creating well-documented APIs. Whether dealing with strings, integers, floats, or custom data types, FastAPI’s flexibility in handling path parameters facilitates the development of reliable and efficient web APIs. As a result, leveraging FastAPI’s path parameters proves instrumental in building scalable and maintainable applications.
FastAPI – Path Parameters
In this exploration, we’ll dive into the realm of FastAPI Path Parameters, unraveling their pivotal role in constructing dynamic and versatile APIs. FastAPI stands out as a contemporary web framework celebrated for its speed, tailor-made for crafting APIs in Python 3.7 and beyond. Leveraging standard Python-type hints, FastAPI simplifies the comprehension of a web service’s functionality for developers, automating the generation of comprehensive documentation.