Astro.cookies
Astro.cookies provides a typed API to read, set, and delete HTTP cookies.
Syntax
Astro.cookies.set('theme', 'dark') Returns
AstroCookies — A cookie manager with get, set, has, delete, and headers methods.
Examples
---
const theme = Astro.cookies.get('theme')?.value ?? 'light';
Astro.cookies.set('seen', 'true', { path: '/', httpOnly: true });
---
<html data-theme={theme}></html>
Output
Reads the saved theme cookie and sets a server-side seen flag for future visits.
Notes
Setting or deleting cookies only takes effect in on-demand (SSR) rendered routes. cookies.get() returns an object with .value, .json(), and .number() helpers. Use httpOnly and secure for sensitive values.