Last Updated : 23 Jul, 2025
In this article, we will learn about FastAPI Query Parameters, what are query parameters, what they do in FastAPI, and how to use them. Additionally, we will see examples of query parameters for best practices.
What is FastAPI - Query Parameters?Query parameters stand as a formidable asset in bolstering the flexibility and usability of an API. Their utility lies in the ability to filter, sort, paginate, and search data seamlessly, all without the need to create dedicated endpoints for each specific task.
In the FastAPI framework, these optional parameters, aptly named query parameters, can be transmitted to the API endpoint directly through the URL. The mechanism involves including individual parameters after a question mark (?), with each parameter being demarcated by an ampersand (&). This approach not only streamlines the process of interacting with the API but also empowers developers to dynamically tailor their queries, making the API more versatile and user-friendly. There are two main types of query parameters in FastAPI:
Here's how query parameters can be used in FastAPI:
FastAPI - Query Parameters ExamplesHere, we will explore commonly used examples of FastAPI to illustrate its functionality. The examples are outlined below.
FastAPI Greeting APIIn this example , code defines a FastAPI application with a single endpoint `/hello` that accepts an optional query parameter `name` and returns a greeting message. The `hello` function handles the logic, generating a personalized greeting if a name is provided, otherwise a default welcome message. If the name argument is given the endpoint will return a greeting message with the user's name. Otherwise, the endpoint will return a generic default message.
Python3
from fastapi import FastAPI
app = FastAPI()
@app.get("/hello")
async def hello(name: str = None):
"""Say hello to the user.
Args:
name: The user's name.
Returns:
A greeting message.
"""
if name is None:
return "Hello, Welcome to GeeksForGeeks!"
else:
return f"Hello, {name} Welcome to GeeksForGeeks!"
Here's how to call the endpoint:
Endpoint Example 1:
http://localhost:8000/hello
Endpoint Example 2:
http://localhost:8000/hello?name=AliceFastAPI Product Search Endpoint
In this example FastAPI code defines a simple web application with a single endpoint /products
. The endpoint supports a query parameter search_query
for filtering products. The function get_products
retrieves a list of products and filters them based on the provided search query, returning the result as a JSON response
from fastapi import FastAPI
app = FastAPI()
@app.get("/products")
async def get_products(search_query: str = None):
"""Get a list of products.
Args:
search_query: A search term.
Returns:
A list of product objects matching the search query.
"""
products = ["shoes", "electronics", "clothing", "running shoes", "mobile phones", "sports shoes"]
# Filter products based on search_query if provided
if search_query:
products = [product for product in products if search_query in product]
return {"products": products}
This will return a list of all products, whether they contain the search term or not.
http://localhost:8000/products
Use the search_query parameter to filter results by endpoint. For example, use the following URL to filter results to include only products that contain the search term "electronics":
http://localhost:8000/products?search_query=electronicsBest Practices For Using Query Parameters in FastAPI
When working with query parameters in FastAPI, adopting best practices is essential to ensure the clarity, reliability, and security of your API. Here are some key guidelines to enhance your use of query parameters:
In conclusion, FastAPI's support for query parameters enhances the development of robust and flexible APIs. The ability to seamlessly integrate parameters into API routes provides a convenient way to handle client inputs. Whether filtering data, setting defaults, or handling optional parameters, FastAPI's query parameters simplify the development process. Leveraging automatic validation, documentation generation, and support for complex types, FastAPI empowers developers to create efficient and self-documenting APIs with ease. The versatility and performance of FastAPI, coupled with its intuitive handling of query parameters, make it a powerful framework for building modern web APIs.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4