Skip to main content
Query parameters are the key-value pairs that appear after the ? in a URL. FastAPI makes them easy to declare and validate.

Basic Query Parameters

When you declare function parameters that aren’t part of the path, they’re automatically interpreted as query parameters:
The query parameters:
  • skip: defaults to 0
  • limit: defaults to 10
Example requests:
  • http://localhost:8000/items/ → skip=0, limit=10
  • http://localhost:8000/items/?skip=20 → skip=20, limit=10
  • http://localhost:8000/items/?skip=0&limit=20 → skip=0, limit=20

Optional Query Parameters

Use None as the default value to make query parameters optional:
The | None type annotation (or Optional[str] in older Python) tells FastAPI this parameter is optional.

Required Query Parameters

To make a query parameter required, declare it without a default value:
Here, needy is required. Requests without it will get a validation error.

Using Query() for Advanced Validation

The Query() function provides additional validation and documentation options:

String Validations

1

Length Constraints

2

Regex Pattern

3

Multiple Values

Required Query Parameters with Query()

You can make a query parameter required while still using Query() for validation:
When using Query(), the parameter is required unless you provide default=None or another default value.

Query Parameter Lists

You can receive multiple values for the same query parameter:
Example request:
Response:

Alias Parameters

Use aliases when you need parameter names that aren’t valid Python identifiers:
Now the URL parameter is item-query instead of q:

Deprecating Parameters

You can mark parameters as deprecated:
Deprecated parameters will be marked as such in the OpenAPI documentation.

Available Query() Parameters

Validation

  • min_length, max_length: String length constraints
  • pattern: Regex pattern for string validation
  • gt, ge, lt, le: Numeric comparisons

Documentation

  • title: Short title for docs
  • description: Detailed description
  • examples: Example values
  • deprecated: Mark as deprecated

Behavior

  • alias: Use a different name in the URL
  • include_in_schema: Hide from OpenAPI docs

Combining Path and Query Parameters

FastAPI automatically knows which parameters are path parameters (they’re in the URL path) and which are query parameters (everything else).