Skip to main content
Path parameters are parts of the URL path that are captured as function parameters. FastAPI makes it easy to declare them with automatic type conversion and validation.

Basic Path Parameters

Declare path parameters using curly braces {} in the path, and they will be passed to your function:
The path parameter 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:
If you put the generic /users/{user_id} route first, it would match /users/me and try to use “me” as the user_id parameter.

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

The Path() 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 than
  • ge: Greater than or equal
  • lt: Less than
  • le: Less than or equal
2

String Constraints

  • min_length: Minimum string length
  • max_length: Maximum string length
  • pattern: Regex pattern to match
3

Documentation

  • title: Title for documentation
  • description: Description for documentation
  • examples: 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