Welcome to Day 5 of the FastAPI Zero to Hero series!
Today we combine the three types of parameters you'll encounter in any real-world backend project:
- β Path Parameters
- β Query Parameters
- β Body Parameters
π Scenario: Update a User's Profile with Notifications
Weβll build an API where:
- The user is identified via
Path - Notification preference is passed via
Query - New profile data is passed in the
Request Body
π§± Install or Activate Your FastAPI Environment
If you haven't yet:
pip install fastapi uvicorn
π§ͺ Step-by-Step Code
π File: test.py
from fastapi import FastAPI, Path, Query, Body
from pydantic import BaseModel
from typing import Optional
app = FastAPI()
# Request Body Model
class UserUpdateRequest(BaseModel):
name: str
email: str
age: Optional[int] = None
# Response Model
class UserResponse(BaseModel):
id: int
name: str
notify: bool
@app.put("/user/{user_id}", response_model=UserResponse)
def update_user(
user_id: int = Path(..., description="ID of the user to update"),
notify: bool = Query(False, description="Whether to notify the user"),
user: UserUpdateRequest = Body(...)
):
# Simulate logic: update user in DB, send notification, etc.
print(f"Updating user {user_id}: {user.dict()}")
if notify:
print(f"Sending notification to {user.email}...")
return UserResponse(
id=user_id,
name=user.name,
notify=notify
)
π Test Your Endpoint
β 1. Run the Server
Make sure your test.py file is saved, then run it using Uvicorn:
uvicorn test:app --host 0.0.0.0 --reload --port 9002
π§ͺ 2. Use Swagger UI (Recommended for Beginners)
FastAPI comes with auto-generated interactive API documentation via Swagger UI.
π§ Steps to Test:
- Open your browser.
- Navigate to: http://localhost:9002/docs
- Locate the
PUT /user/{user_id}endpoint. - Click on it β then click "Try it out".
- Fill in the fields:
-
user_id:123 -
notify:true - JSON Body:
{
"name": "Utkarsh",
"email": "utkarsh@example.com",
"age": 30
}
- Click Execute.
β You will see:
- The full request URL
- The response status
- The returned JSON result
It's a great way to explore and test your API without writing any client code!
Output:
π§ What You Just Did
Here's a breakdown of how different FastAPI components worked together in your endpoint:
| Part | What it Does |
|---|---|
| Path | Identifies the user by user_id
|
| Query | Controls logic flow (e.g., whether to notify) |
| Body | Sends structured profile update data (JSON) |
| Response | Filters output using response_model to limit what gets returned |
π§° Bonus: Add Validation to Your Parameters
You can enhance your endpoint by adding validation rules to the Path and Query parameters:
user_id: int = Path(..., gt=0, description="User ID must be positive"),
notify: bool = Query(False, description="Notify user after update"),
-
gt=0ensures theuser_idis a positive integer. -
descriptionimproves the auto-generated API documentation by adding helpful context to parameters.
π§ͺ Explore Your API at /docs
Run your server if itβs not running already:
uvicorn test:app --host 0.0.0.0 --reload --port 9002
Then, open your browser and navigate to:
π Swagger UI: http://localhost:9002/docs
Youβll see an auto-generated Swagger UI with:
- β All your defined endpoints
- π§© Parameter descriptions
- π¦ Data models for request/response
- π§ͺ Interactive testing interface
π‘ This is one of the best features of FastAPI β instant, interactive API docs powered by your Python code and type hints!
π§© Wrap-Up
In real-world applications, API routes almost always require a mix of:
- β Path parameters for routing (e.g., user ID)
- β
Query parameters for optional behavior (e.g.,
notify=true) - β Body data for resource creation or updates (e.g., profile info)
And now, you know how to combine and validate all three like a pro. πͺ
π Credits
Huge thanks to the FastAPI Official Documentation by SebastiΓ‘n RamΓrez (@tiangolo) β the best place to learn and explore everything about FastAPI.
π¨βπ» About Me
Hey there! Iβm Utkarsh Rastogi, an AWS Community Builder and passionate cloud-native enthusiast who loves building scalable backend systems and sharing knowledge with the community.
π Connect with me: Utkarsh Rastogi
π¬ Share Your Thoughts β I'd Love Your Feedback!
If you enjoyed today's post or learned something new, I'd truly appreciate it if you leave a comment or share your thoughts π
Your feedback, questions, or even a quick βπ₯ Loved this!β keeps me motivated to continue this journey and share more in the upcoming #FastAPIDaily posts.
β
What did you find most helpful?
β
Anything you'd like explained in the next part?
β
Suggestions for improvement? Iβm all ears! π
Letβs grow and learn together β one FastAPI day at a time π


Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.