Cookie Parameters
You can define Cookie parameters the same way you define Query and Path parameters.
Import Cookie
Section titled “Import Cookie”First import Cookie:
from typing import Annotated
from fastapi import Cookie, FastAPI
app = FastAPI()
@app.get("/items/")
async def read_items(ads_id: Annotated[str | None, Cookie()] = None):
return {"ads_id": ads_id}Declare Cookie parameters
Section titled “Declare Cookie parameters”Then declare the cookie parameters using the same structure as with Path and Query.
You can define the default value as well as all the extra validation or annotation parameters:
from typing import Annotated
from fastapi import Cookie, FastAPI
app = FastAPI()
@app.get("/items/")
async def read_items(ads_id: Annotated[str | None, Cookie()] = None):
return {"ads_id": ads_id}Declare cookies with Cookie, using the same common pattern as Query and Path.