Path Parameters
Part of: FastAPI Basics
Learn to create dynamic URLs that accept parameters. Build endpoints that can handle user IDs, product names, and other variable data in the URL path.
What You'll Learn
- Understand what path parameters are and why they're useful
- Create endpoints with dynamic URL paths
- Use type hints for automatic validation
- Handle different data types in path parameters
Theory and Concepts
Path Parameters
🎯 What You'll Learn
Path parameters allow you to create dynamic URLs that can accept different values. Instead of creating separate endpoints for each user, product, or item, you can create one flexible endpoint that handles them all!
By the end of this lesson, you'll understand how to:
- Create endpoints with dynamic URL paths
- Use type hints for automatic validation
- Handle different data types in path parameters
- Build truly flexible APIs
🛣️ What are Path Parameters?
Think of path parameters like variables in your URL. Instead of having fixed paths like /user1, /user2, /user3, you can have one dynamic path like /users/{user_id} that works for any user ID.
Real-World Examples
You see path parameters everywhere on the web:
- YouTube: youtube.com/watch?v=VIDEO_ID
- GitHub: github.com/USERNAME/REPOSITORY
- Online stores: store.com/products/PRODUCT_ID
- Social media: twitter.com/USERNAME
🔧 How Path Parameters Work
Basic Syntax
[Code Example]
Key parts:
- {user_id} in the path defines the parameter
- user_id function parameter captures the value
- The names must match exactly!
URL Examples
With the endpoint above, these URLs would work:
- /users/123 → user_id = "123"
- /users/alice → user_id = "alice"
- /users/admin → user_id = "admin"
🎨 Type Hints for Validation
FastAPI can automatically validate and convert path parameters using Python type hints:
String Parameters (Default)
[Code Example]
Integer Parameters
[Code Example]
What happens:
- /items/123 ✅ Works → item_id = 123 (integer)
- /items/abc ❌ Error → Returns 422 validation error
Float Parameters
[Code Example]
💡 Step-by-Step Examples
Example 1: User Profile Endpoint
[Code Example]
Test it:
- Visit /users/john → {"user_id": "john", "message": "Hello user john!"}
Example 2: Product with Integer ID
[Code Example]
Test it:
- Visit /products/42 → {"product_id": 42, "name": "Product 42", "available": true}
- Visit /products/abc → Validation error (not a number)
Example 3: Multiple Path Parameters
[Code Example]
Test it:
- Visit /users/alice/posts/5 → Gets post 5 from user alice
✨ Best Practices
1. Use Descriptive Names
[Code Example]
2. Add Type Hints
[Code Example]
3. Use Docstrings
[Code Example]
🚫 Common Beginner Mistakes
Mistake 1: Mismatched Parameter Names
[Code Example]
Mistake 2: Forgetting Type Hints
[Code Example]
Mistake 3: Wrong Bracket Style
[Code Example]
🎉 What's Next?
Congratulations! You now understand how to create dynamic URLs with path parameters. This is a fundamental skill that you'll use in almost every API you build.
You've learned:
- How to define path parameters with {parameter_name}
- Using type hints for automatic validation
- Handling different data types (string, integer, float)
- Best practices for naming and structure
Coming up next: Query Parameters - another way to make your APIs flexible by accepting optional parameters in the URL!
📖 Additional Resources
- FastAPI Path Parameters Guide
- Python Type Hints Documentation
- HTTP URL Structure Explained
Helpful Hint
Use curly braces {} in your path to define parameters, like '/users/{user_id}'. The parameter name in the path must match your function parameter name!