Set Custom Cookies on a Response
Write cookies with set_cookie and options.
Set Custom Cookies on a Response is a free Flask Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Cookies Beyond Sessions
Sometimes you want your own cookie, separate from the session, to remember a theme or a preference for a visitor. 🍪
You Need a Response
Cookies are written onto a response object. So first build one with make_response instead of returning a plain string.
from flask import make_response
resp = make_response('Hi there')Call set_cookie
Add the cookie with set_cookie, passing a name and a value. Then return that response so the header reaches the browser.
resp.set_cookie('theme', 'dark')
return respRead It Next Time
On the next request the browser sends it back. Read your cookie from request.cookies, which works like a dictionary.
from flask import request
theme = request.cookies.get('theme')Control the Lifetime
Set max_age in seconds to control how long a cookie survives. Leave it out and the cookie dies when the browser closes.
resp.set_cookie('theme', 'dark', max_age=60*60*24)HTTPS Only
The secure flag tells the browser to send the cookie only over HTTPS, keeping it off plain unencrypted connections.
resp.set_cookie('token', 'abc', secure=True)Hide from JavaScript
Turn on httponly so client-side scripts cannot read the cookie. This blocks a common cross-site scripting theft path.
resp.set_cookie('token', 'abc', httponly=True)Limit Cross-Site Sending
The samesite option controls when the cookie travels on cross-site requests. Set it to Lax or Strict to curb CSRF risk.
resp.set_cookie('id', '7', samesite='Lax')Deleting a Cookie
To remove one, call delete_cookie with its name on the response. The browser then drops it on the next visit.
resp.delete_cookie('theme')Values Are Strings
Cookie values are always strings. To store a number or flag, convert it on the way out and parse it on the way back in.
resp.set_cookie('count', str(5))Mind the Size
Browsers cap each cookie near four kilobytes. Keep them tiny and store large data on the server, referenced by an id.
Quick Check
You want a cookie that JavaScript on the page cannot read.
Recap
You learned to build a response, write cookies with set_cookie, read them from request.cookies, and harden them with secure flags. 🛡️
Frequently asked questions
Is the “Set Custom Cookies on a Response” lesson free?
Yes — the full text of “Set Custom Cookies on a Response” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.
What will I learn in “Set Custom Cookies on a Response”?
Write cookies with set_cookie and options. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flask Academy?
No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Set Custom Cookies on a Response” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flask Academy lesson?
Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Set and Read the session Dict
- The SECRET_KEY and Signed Cookies
- Set Custom Cookies on a Response
- Flash Messages Between Requests