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

FastAPI Query Parameters

FastAPI Query Parameters

Query parameters are the key-value pairs in a URL after ? and separated by & . When a function parameter is neither a path parameter nor a request body, FastAPI automatically interprets it as a query parameter.


Basic Usage

Declaring a query parameter only requires adding a type annotation and default value to the function parameter:

Example

from

fastapi

import

FastAPI

app

=

FastAPI

(

)

skip and limit are query parameters with default values

fake_items_db

=

[

{

"item_name"

:

"Foo"

}

,

{

"item_name"

:

"Bar"

}

,

{

"item_name"

:

"Baz"

}

]

@

app.

get

(

"/items/"

)

async

def

read_item

(

skip:

int

=

0

,

limit:

int

=

10

)

:

   

Simulating paginated queries

   

return

fake_items_db

[

skip : skip + limit

]

Format of query parameters in the URL:

http://127.0.0.1:8000/items/?skip=0&limit=10

Parameter values for different access methods:

URL value of skip value of limit Description
/items/ 0 10 Using a default value
/items/?skip=20 20 10 skip uses the provided value, limit uses the default value
/items/?skip=20&limit=5 20 5 both parameters use the provided values

Optional arguments

Setting the default value to None declares an optional query parameter:

Example

from

fastapi

import

FastAPI

app

=

FastAPI

(

)

@

app.

get

(

"/items/{item_id}"

)

async

def

read_item

(

item_id:

str

,

q:

str

|

None

=

None

)

:

   

item_id is a path parameter (required), q is a query parameter (optional)

   

if

q:

       

return

{

"item_id"

: item_id

,

"q"

: q

}

   

return

{

"item_id"

: item_id

}

Here q: str | None = None indicates q can be a string or None, with a default value of None。

FastAPI determines whether a parameter is required based on its default value, = None rather than its type annotation str | None. Type annotations mainly help editors provide better support.


Query parameter type conversion

FastAPI supports automatically converting query parameters to bool Type:

Example

from

fastapi

import

FastAPI

app

=

FastAPI

(

)

@

app.

get

(

"/items/{item_id}"

)

async

def

read_item

(

item_id:

str

,

short:

bool

=

False

)

:

   

the short parameter is automatically converted to a boolean

   

if

short:

       

return

{

"item_id"

: item_id

}

   

return

{

"item_id"

: item_id

,

"description"

:

"This is a very long description"

}

In the following URLs, the short parameters are all converted to True:

/items/foo?short=1

/items/foo?short=True

/items/foo?short=true

/items/foo?short=on

/items/foo?short=yes

other values are converted to False。


Required query parameters

A query parameter without a default value is required:

Example

from

fastapi

import

FastAPI

app

=

FastAPI

(

)

@

app.

get

(

"/items/{item_id}"

)

async

def

read_item

(

item_id:

str

,

needy:

str

)

:

   

needy has no default value, so it is a required query parameter

   

return

{

"item_id"

: item_id

,

"needy"

: needy

}

If you access /items/foo without providing the needy parameter, FastAPI returns an error like the following:

{

  "detail": [

    {

      "type": "missing",

      "loc": ["query", "needy"],

      "msg": "Field required",

      "input": null

    }

  ]

}


Mixing required, defaulted, and optional parameters

You can mix different types of query parameters in the same function:

Example

from

fastapi

import

FastAPI

app

=

FastAPI

(

)

@

app.

get

(

"/items/{item_id}"

)

async

def

read_item

(

    item_id:

str

,

         

Path parameter (required)

    needy:

str

,

           

Required query parameters

    skip:

int

=

0

,

         

Query parameter with a default value

    limit:

int

|

None

=

None

,

 

Optional query parameter

)

:

    item

=

{

"item_id"

: item_id

,

"needy"

: needy

,

"skip"

: skip

}

   

if

limit:

        item.

update

(

{

"limit"

: limit

}

)

   

return

item

Summary of parameter types:

Parameters Declaration Method Required Description
item_id item_id: str Required Path parameter, appears in the URL path
needy needy: str Required Query parameter, no default value
skip skip: int = 0 Optional Query parameter, with default value 0
limit limit: int \| None = None Optional Query parameter, with default value None

Multiple path and query parameters

FastAPI can recognize multiple path and query parameters at the same time; the order of parameter declarations does not affect recognition:

Example

from

fastapi

import

FastAPI

app

=

FastAPI

(

)

@

app.

get

(

"/users/{user_id}/items/{item_id}"

)

async

def

read_user_item

(

    user_id:

int

,

     

Path parameters

    item_id:

str

,

     

Path parameters

    q:

str

|

None

=

None

,

 

Optional query parameter

    short:

bool

=

False

,

 

Query parameter with a default value

)

:

    item

=

{

"item_id"

: item_id

,

"owner_id"

: user_id

}

   

if

q:

        item.

update

(

{

"q"

: q

}

)

   

if

not

short:

        item.

update

(

{

"description"

:

"This is a very long description"

}

)

   

return

item

FastAPI matches variables in the path by parameter name, so the order of function parameters does not matter.


Summary

Key points of query parameters: