Basic Path Parameters
Declare path parameters using curly braces{} in the path, and they will be passed to your function:
item_id is automatically:
- Extracted from the URL
- Converted to the declared type (
int) - Validated (returns 422 error if not an integer)
- Passed to your function
Type Conversion and Validation
FastAPI automatically validates and converts path parameters based on their type annotations:Order Matters
When you have paths that could match multiple patterns, order matters. More specific paths should come first:Enum Path Parameters
You can use Python Enums to restrict path parameter values:The enum values will be available in the OpenAPI docs, and only those values will be accepted.
Using Path() for Metadata and Validation
ThePath() function allows you to add metadata and validation constraints:
Numeric Validations
You can add numeric constraints to path parameters:Available Validation Parameters
1
Numeric Constraints
gt: Greater thange: Greater than or equallt: Less thanle: Less than or equal
2
String Constraints
min_length: Minimum string lengthmax_length: Maximum string lengthpattern: Regex pattern to match
3
Documentation
title: Title for documentationdescription: Description for documentationexamples: Example values
Key Points
- Path parameters are always required (they’re part of the URL)
- They’re automatically validated based on type annotations
- You can use Enums to restrict to specific values
- Use
Path()to add metadata and validation constraints - Order your routes from most specific to most generic
Related Topics
- Query Parameters - Learn about optional URL parameters
- Request Body - Handle complex data with Pydantic models
- Extra Data Types - Use UUID, datetime, and more