0
0
FastAPIframework~15 mins

Basic query parameter declaration in FastAPI - Mini Project: Build & Apply

Choose your learning style9 modes available
Basic query parameter declaration
📖 Scenario: You are building a simple web API using FastAPI. Your API will greet users by their name, which will be provided as a query parameter in the URL.
🎯 Goal: Create a FastAPI app that accepts a query parameter called name and returns a greeting message including that name.
📋 What You'll Learn
Create a FastAPI app instance named app
Define a GET endpoint at path /greet
Declare a query parameter named name of type str in the endpoint function
Return a dictionary with a key message and value "Hello, {name}!" where {name} is the query parameter value
💡 Why This Matters
🌍 Real World
APIs often need to accept user input via query parameters to customize responses, such as filtering data or personalizing messages.
💼 Career
Understanding how to declare and use query parameters in FastAPI is essential for backend developers building web APIs.
Progress0 / 4 steps
1
Create FastAPI app instance
Import FastAPI from fastapi and create an app instance called app.
FastAPI
Need a hint?

Use app = FastAPI() to create the app instance.

2
Define GET endpoint at /greet
Add a GET endpoint to app with the path /greet using the @app.get("/greet") decorator. Define a function called greet with no parameters yet.
FastAPI
Need a hint?

Use @app.get("/greet") above the function greet.

3
Add query parameter name
Modify the greet function to accept a query parameter called name of type str.
FastAPI
Need a hint?

Add name: str inside the parentheses of the greet function.

4
Return greeting message
Return a dictionary with key message and value "Hello, {name}!" using an f-string inside the greet function.
FastAPI
Need a hint?

Use return {"message": f"Hello, {name}!"} to send the greeting.