{} What Are Path Parameters?
A path parameter is a variable part of a URL.
It is a request parameter placed directly inside the URL path and is used to identify a specific resource. For example: /books/{book_id} , {books_id} → path parameter
📌 Static API Endpoints
A basic (static) endpoint looks like this:
@app.get("/books")
async def read_all_books():
return BOOKSURL:
🌍 127.0.0.1:8000/books 127.0.0.1:8000/books
This is called a static path because /books is fixed and never changes.
📖 Meaning:
👉 “Return data for all books.”
🔀 Dynamic Paths
Sometimes you need data for a single item instead of all items.
This is where path parameters come in.
A path parameter is written inside {}.
Example:
/users/{user_id}It is often called a dynamic parameter (informal term) because the value changes each time.
Examples of actual URLs:
/users/1/users/42/users/100
⚠️ Terminology
Term | Meaning |
|---|---|
Path Parameter | ✅ Official term (FastAPI/OpenAPI) |
Dynamic Parameter | ⚠️ Informal name |
❌ Inefficient Approach
Creating separate endpoints for each item is not practical:
/book1
/book2
/book3This quickly becomes messy and hard to maintain.
✅ Using Path Parameters
A better solution is a single flexible endpoint:
/books/{book_id} Here:
{book_id}is the path parameterIt acts as a placeholder for any value
Examples:
/books/1 → book_id = 1
/books/10 → book_id = 10🧩 Example in FastAPI
from fastapi import FastAPI
app = FastAPI()
@app.get("/books/{dynamic_param}")
async def read_all_book(dynamic_param):
return {"dynamic_param": dynamic_param}If you open:
/books/book_oneResponse:
{
"dynamic_param": "book_one"
}
FastAPI automatically:
Extracts the value from the URL
Converts it to the specified type (
int)Passes it to the function
🌍 Real-World Examples
Use Case | Example URL |
|---|---|
Get one book |
|
Get one user |
|
Get one order |
|
⚠️ Order Matters with Path Parameters
FastAPI checks routes from top to bottom and uses the first matching path.
🔀 Problem Example
@app.get("/books/{dynamic_param}")
async def read_all_book(dynamic_param):
return {"dynamic_param": dynamic_param}
@app.get("/books/mybook")
async def read_all_books():
return {"book_title": "My Favorite Book"}If you visit:
🌍 /books/mybook
FastAPI matches:
/books/{dynamic_param}👉

because "mybook" is treated as a value for dynamic_param.
👉 The specific route /books/mybook will never run.
✅ Correct Order
Place the static route first:
@app.get("/books/mybook")
async def read_all_books():
return {"book_title": "My Favorite Book"}
@app.get("/books/{dynamic_param}")
async def read_all_book(dynamic_param):
return {"dynamic_param": dynamic_param}
📌 Why Static Routes Should Come First
Dynamic routes match almost anything
Static routes match only one exact path
If dynamic comes first, it “captures” the request before the static route gets a chance
👉 Rule: Specific (static) → then Generic (dynamic)
📝 Summary:
This section demonstrates how path parameters make APIs more dynamic, scalable, and efficient.
A path parameter is a variable part of a URL used to identify a specific resource (e.g.,
/books/{book_id}).Static endpoints (e.g.,
/books) return fixed resources and do not change.Dynamic endpoints use
{}to capture values from the URL (e.g.,/books/1,/users/42).Path Parameter is the official term (Dynamic Parameter is informal).
Why Use Path Parameters?
✅ Avoid creating multiple hardcoded routes (
/book1,/book2,/book3)✅ Create one flexible and scalable endpoint (
/books/{book_id})✅ Improve maintainability and code cleanliness
How FastAPI Handles Them
Automatically extracts the value from the URL
Converts it to the specified type (e.g.,
int)Passes it into the function
Important Rule: Order Matters
FastAPI evaluates routes top to bottom
Dynamic routes can match almost anything
Static routes should always be declared before dynamic ones
👉 Rule: Specific (static) routes first, Generic (dynamic) routes second