? 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:skip: defaults to 0limit: defaults to 10
http://localhost:8000/items/→ skip=0, limit=10http://localhost:8000/items/?skip=20→ skip=20, limit=10http://localhost:8000/items/?skip=0&limit=20→ skip=0, limit=20
Optional Query Parameters
UseNone 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:needy is required. Requests without it will get a validation error.
Using Query() for Advanced Validation
TheQuery() 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 usingQuery() 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:Alias Parameters
Use aliases when you need parameter names that aren’t valid Python identifiers:item-query instead of q:
Deprecating Parameters
You can mark parameters as deprecated:Available Query() Parameters
Validation
min_length,max_length: String length constraintspattern: Regex pattern for string validationgt,ge,lt,le: Numeric comparisons
Documentation
title: Short title for docsdescription: Detailed descriptionexamples: Example valuesdeprecated: Mark as deprecated
Behavior
alias: Use a different name in the URLinclude_in_schema: Hide from OpenAPI docs
Combining Path and Query Parameters
Related Topics
- Path Parameters - Learn about URL path parameters
- Request Body - Handle complex data with Pydantic models
- Header Parameters - Read HTTP headers