You can define Cookie parameters the same way you define Query and Path parameters.

Import Cookie

example:

from typing import Union

from fastapi import Cookie, FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(ads_id: Union[str, None] = Cookie(default=None)):
    return {"ads_id": ads_id}

First import Cookie:

=== "Python 3.6 and above"

from fastapi import Cookie, FastAPI

=== "Python 3.10 and above"

from fastapi import Cookie, FastAPI

Declare Cookie parameters

Then declare the cookie parameters using the same structure as with Path and Query.

The first value is the default value, you can pass all the extra validation or annotation parameters:

=== "Python 3.6 and above"

async def read_items(ads_id: Union[str, None] = Cookie(default=None)):

=== "Python 3.10 and above"

async def read_items(ads_id: str | None = Cookie(default=None)):

!!! note "Technical Details" Cookie is a "sister" class of Path and Query. It also inherits from the same common Param class.

But remember that when you import `Query`, `Path`, `Cookie` and others from `fastapi`, those are actually functions that return special classes.

!!! info To declare cookies, you need to use Cookie, because otherwise the parameters would be interpreted as query parameters.

keywords: pythonFastAPI