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=0
ensures theuser_id
is a positive integer. -
description
improves 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.