Skip to content

Response Cookies

You can create (set) Cookies in your response.

To do that, you can create a response as described in Return a Response directly.

Then set Cookies in it, and then return it:

from fastapi import FastAPI
from starlette.responses import JSONResponse

app = FastAPI()"/cookie/")
def create_cookie():
    content = {"message": "Come to the dark side, we have cookies"}
    response = JSONResponse(content=content)
    response.set_cookie(key="fakesession", value="fake-cookie-session-value")
    return response

More info

To see all the available parameters and options, check the documentation in Starlette.