
FastAPI
Learn the fundamentals of complex paths in FastAPI.
Complex paths use multiple path parameters to identify nested resources. They follow REST principles for hierarchical resource relationships.
In this section, you'll understand:
from fastapi import FastAPI
app = FastAPI()
# Single parameter (simple)
@app.get("/users/{user_id}")
async def get_user(user_id: int):
return {"user_id": user_id}
# Two parameters (one level of nesting)
@app.get("/users/{user_id}/posts/{post_id}")
async def get_user_post(user_id: int, post_id: int):
return {"user_id": user_id, "post_id": post_id}
# Three parameters (multiple levels)
@app.get("/organizations/{org_id}/teams/{team_id}/members/{member_id}")
async def get_team_member(org_id: int, team_id: int, member_id: int):
return {"org_id": org_id, "team_id": team_id, "member_id": member_id}
# Create resource in nested context
@app.post("/users/{user_id}/posts")
async def create_post(user_id: int, post: PostCreate):
return {"user_id": user_id, "post": post}
# Multiple parameters with different types
@app.get("/projects/{project_slug}/issues/{issue_id}")
async def get_issue(project_slug: str, issue_id: int):
return {"project": project_slug, "issue": issue_id}# Blog platform: organizations > blogs > posts > comments
@app.get("/orgs/{org_id}/blogs/{blog_id}/posts/{post_id}/comments/{comment_id}")
async def get_comment(org_id: int, blog_id: int, post_id: int, comment_id: int):
return {
"org_id": org_id,
"blog_id": blog_id,
"post_id": post_id,
"comment_id": comment_id
}
# E-commerce: stores > categories > products
@app.get("/stores/{store_id}/categories/{category_id}/products/{product_id}")
async def get_product(store_id: int, category_id: int, product_id: int):
return {
"store_id": store_id,
"category_id": category_id,
"product_id": product_id
}Complex paths are essential for:
Ready to explore more? Check out the advanced section for production patterns and edge cases.
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward