cookies()

Reads and writes HTTP cookies in Server Components, actions, and handlers.

Since Next 13 (App Router); async since Next 15 Spec ↗

Syntax

import { cookies } from 'next/headers'; const store = await cookies()

Returns

Promise<ReadonlyRequestCookies> — Cookie store with get, getAll, set, delete, has.

Examples

import { cookies } from 'next/headers'

export default async function Page() {
  const cookieStore = await cookies()
  const theme = cookieStore.get('theme')?.value ?? 'light'
  return <div data-theme={theme}>Hello</div>
}
'use server'
import { cookies } from 'next/headers'

export async function setTheme(value: string) {
  const cookieStore = await cookies()
  cookieStore.set('theme', value, {
    httpOnly: true,
    secure: true,
    sameSite: 'lax',
    maxAge: 60 * 60 * 24 * 365,
  })
}

Notes

Next 15: `cookies()` is async — always `await` it. Reading cookies opts the route into dynamic rendering. You can only `.set()` / `.delete()` cookies inside a Server Action or Route Handler (not during render of a Server Component). Server-only API.

See also