AI Workshop: learn to build apps with AI →
Hono: Manage cookies

Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.


Hono provides utilities to help you handle cookies.

Basic helpers are getCookie(), setCookie(), and deleteCookie():

import { getCookie, setCookie, deleteCookie } from 'hono/cookie'
setCookie('username', 'joe')

You can set all the cookie options by passing an object as the third parameter:

setCookie('username', 'Flavio', { 
  domain: 'thevalleyofcode.com', 
  path: '/administrator', 
  expires: new Date(Date.now() + 900000), 
  httpOnly: true 
})

The most useful parameters you can set are:

ValueDescription
domainThe cookie domain name
expiresSet the cookie expiration date. If missing, or 0, the cookie is a session cookie
httpOnlySet the cookie to be accessible only by the web server. See HttpOnly
maxAgeSet the expiry time relative to the current time, expressed in milliseconds
pathThe cookie path. Set to ’/’ to apply to the whole site
secureMarks the cookie HTTPS only
signedSet the cookie to be signed
sameSiteValue of SameSite

Read a cookie:

const cookie = getCookie('username')

Clear a cookie:

deleteCookie('username')

Then we have two more functions to work with signed cookies:

import { getSignedCookie, setSignedCookie } from 'hono/cookie'

They work like the others but add a signature so you can detect if the client tampered with the cookie.

Lessons in this unit:

0: Introduction
1: Your first Hono app
2: The Request object
3: Send a response to the client
4: ▶︎ Manage cookies
5: Work with HTTP headers
6: Handling redirects
7: Routing
8: JSX templates
9: Middleware
10: Hono on Node.js