Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

FastAPI Query Parameter Validation

FastAPI Query Parameter Validation

FastAPI lets you declare additional validation rules and metadata for query parameters, such as string length limits, regex matching, etc. Through Query and Annotated, you can enhance parameter validation without changing the function logic.


Basic validation

The following example adds a maximum length limit q to the query parameter:

Example

from

typing

import

Annotated

from

fastapi

import

FastAPI

,

Query

app

=

FastAPI

(

)

@

app.

get

(

"/items/"

)

async

def

read_items

(

   

Add validation using Annotated + Query

    q: Annotated

[

str

|

None

,

Query

(

max_length

=

50

)

]

=

None

,

)

:

    results

=

{

"items"

:

[

{

"item_id"

:

"Foo"

}

,

{

"item_id"

:

"Bar"

}

]

}

   

if

q:

        results.

update

(

{

"q"

: q

}

)

   

return

results

Code explanation:

Section Description
Annotated[str \| None, ...] Type annotation indicating q can be a string or None
Query(max_length=50) Validation rule,q has a maximum length of 50 characters
= None Default value, making the parameter optional

FastAPI recommends using Annotated to declare validation, rather than putting Query as the default value. Because with Annotated , the function's default value is the real default value, which is more intuitive in Python and better supported by editors and type-checking tools.


Adding more validation

You can add multiple validation rules at the same time:

Example

from

typing

import

Annotated

from

fastapi

import

FastAPI

,

Query

app

=

FastAPI

(

)

@

app.

get

(

"/items/"

)

async

def

read_items

(

   

Restrict minimum length, maximum length, and regex at the same time

    q: Annotated

[

str

|

None

,

Query

(

min_length

=

3

,

max_length

=

50

,

pattern

=

"^fixedquery$"

)

]

=

None

,

)

:

    results

=

{

"items"

:

[

{

"item_id"

:

"Foo"

}

,

{

"item_id"

:

"Bar"

}

]

}

   

if

q:

        results.

update

(

{

"q"

: q

}

)

   

return

results

String validation parameters:

Parameters Type Description
min_length int Minimum length
max_length int Maximum length
pattern str Regular expression match

Regular expressions ^fixedquery$ meaning:


Validation with a default value

You can set both a default value and validation rules for a query parameter:

Example

from

typing

import

Annotated

from

fastapi

import

FastAPI

,

Query

app

=

FastAPI

(

)

@

app.

get

(

"/items/"

)

async

def

read_items

(

   

Default value is "fixedquery", with a minimum length of 3 required

    q: Annotated

[

str

,

Query

(

min_length

=

3

)

]

=

"fixedquery"

,

)

:

    results

=

{

"items"

:

[

{

"item_id"

:

"Foo"

}

,

{

"item_id"

:

"Bar"

}

]

}

   

if

q:

        results.

update

(

{

"q"

: q

}

)

   

return

results

Default values of any type (including non- None values) make the parameter optional. A parameter with no default value and no Query(default=...) is required.


Required parameter

Use Query , if no default value is declared, the parameter is required:

Example

from

typing

import

Annotated

from

fastapi

import

FastAPI

,

Query

app

=

FastAPI

(

)

@

app.

get

(

"/items/"

)

async

def

read_items

(

   

No = None, so q is a required parameter

    q: Annotated

[

str

,

Query

(

min_length

=

3

)

]

,

)

:

    results

=

{

"items"

:

[

{

"item_id"

:

"Foo"

}

,

{

"item_id"

:

"Bar"

}

]

}

    results.

update

(

{

"q"

: q

}

)

   

return

results

Required but can be None

Sometimes you need the client to provide a value, but the value can be None:

Example

from

typing

import

Annotated

from

fastapi

import

FastAPI

,

Query

app

=

FastAPI

(

)

@

app.

get

(

"/items/"

)

async

def

read_items

(

   

The client must provide the q parameter, but the value can be None

    q: Annotated

[

str

|

None

,

Query

(

min_length

=

3

)

]

,

)

:

    results

=

{

"items"

:

[

{

"item_id"

:

"Foo"

}

,

{

"item_id"

:

"Bar"

}

]

}

   

if

q:

        results.

update

(

{

"q"

: q

}

)

   

return

results


Query parameter lists / multiple values

Use Query You can declare a query parameter that accepts multiple values:

Example

from

typing

import

Annotated

from

fastapi

import

FastAPI

,

Query

app

=

FastAPI

(

)

@

app.

get

(

"/items/"

)

async

def

read_items

(

   

q can appear multiple times in the URL, and the values are collected into a list

    q: Annotated

[

list

[

str

]

|

None

,

Query

(

)

]

=

None

,

)

:

    query_items

=

{

"q"

: q

}

   

return

query_items

Access the http://127.0.0.1:8000/items/?q=foo&q=bar, returns:

{"q": ["foo", "bar"]}

To declare a query parameter of type list , you must explicitly use Query(), otherwise FastAPI will interpret it as a request body.


Declaring metadata

Query also supports adding metadata to parameters, which appears in the API docs:

Example

from

typing

import

Annotated

from

fastapi

import

FastAPI

,

Query

app

=

FastAPI

(

)

@

app.

get

(

"/items/"

)

async

def

read_items

(

    q: Annotated

[

str

|

None

,

Query

(

        title

=

"Query string"

,

         

Parameter title

        description

=

"Query string used to filter items"

,

 

Parameter description

        min_length

=

3

,

        max_length

=

50

,

        alias

=

"item-query"

,

   

Alias for the parameter name in the URL

        deprecated

=

True

,

     

Mark as deprecated

   

)

]

=

None

,

)

:

    results

=

{

"items"

:

[

{

"item_id"

:

"Foo"

}

,

{

"item_id"

:

"Bar"

}

]

}

   

if

q:

        results.

update

(

{

"q"

: q

}

)

   

return

results

Metadata parameter descriptions:

Parameters Description Use Cases
alias Alias for the parameter name in the URL Parameter names containing hyphens (such as item-query), when they are not valid Python variable names
title Parameter title Show the parameter title in the docs
description Parameter description Show the detailed description of the parameter in the docs
deprecated Mark as deprecated The parameter can still be used, but it is marked as "deprecated" in the docs
include_in_schema Whether it appears in the API docs Set to False Can hide the parameter

Exclude the parameter from OpenAPI

If a query parameter should not appear in the API docs:

Example

from

typing

import

Annotated

from

fastapi

import

FastAPI

,

Query

app

=

FastAPI

(

)

@

app.

get

(

"/items/"

)

async

def

read_items

(

   

hidden_query does not appear in the API docs but can still be used

    hidden_query: Annotated

[

str

|

None

,

Query

(

include_in_schema

=

False

)

]

=

None

,

)

:

   

if

hidden_query:

       

return

{

"hidden_query"

: hidden_query

}

   

return

{

"items"

:

[

{

"item_id"

:

"Foo"

}

]

}


Summary

Key points of query parameter validation: