FastAPI – Query Parameters Examples

Here, we will explore commonly used examples of FastAPI to illustrate its functionality. The examples are outlined below.

FastAPI Greeting API

In 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 w3wiki!"
    else:
        return f"Hello, {name} Welcome to w3wiki!"


Here’s how to call the endpoint:

Endpoint Example 1:

http://localhost:8000/hello

Endpoint Example 2:

http://localhost:8000/hello?name=Alice

FastAPI 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

Python3




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=electronics

FastAPI – Query Parameters

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.

Similar Reads

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....

FastAPI – Query Parameters Examples

Here, we will explore commonly used examples of FastAPI to illustrate its functionality. The examples are outlined below....

Best Practices For Using Query Parameters in FastAPI

...

Contact Us