Skip to main content
cookies is an async function that lets you read incoming request cookies in Server Components, and read or write outgoing cookies in Server Functions and Route Handlers.
app/page.tsx
import { cookies } from 'next/headers'

export default async function Page() {
  const cookieStore = await cookies()
  const theme = cookieStore.get('theme')
  return '...'
}

Methods

The object returned by cookies() exposes the following methods:
get(name)
{ name: string; value: string } | undefined
Returns the first cookie with a matching name. Returns undefined if not found.
getAll(name?)
{ name: string; value: string }[]
Returns all cookies with a matching name. If name is omitted, returns all cookies.
has(name)
boolean
Returns true if a cookie with the given name exists.
set(name, value, options?)
void
Sets an outgoing cookie. Only available in Server Functions and Route Handlers.
delete(name)
void
Deletes a cookie by name. Only available in Server Functions and Route Handlers.
toString()
string
Returns a string representation of the cookies.
When calling set(), the following options are supported:
name
string
The cookie name.
value
string
The cookie value.
expires
Date
The exact expiration date.
maxAge
number
Cookie lifetime in seconds.
domain
string
The domain on which the cookie is available.
path
string
default:"/"
Restricts the cookie to a specific path within the domain.
secure
boolean
If true, the cookie is only sent over HTTPS.
httpOnly
boolean
If true, the cookie is inaccessible to client-side JavaScript.
sameSite
'lax' | 'strict' | 'none' | boolean
Controls cross-site request behavior.
priority
'low' | 'medium' | 'high'
The cookie priority hint.
partitioned
boolean
Whether the cookie uses CHIPS partitioning.

Good to know

  • cookies is async and returns a promise. Use async/await or React’s use.
  • In Next.js 14 and earlier, cookies was synchronous. Synchronous access still works in Next.js 15 for backwards compatibility, but is deprecated.
  • cookies is a Request-time API. Using it in a layout or page opts the route into dynamic rendering.
  • delete and set can only be called inside a Server Function or Route Handler — not during Server Component rendering.
  • HTTP does not allow setting cookies after streaming starts, so set must be used in a Server Function or Route Handler.

Examples

app/page.tsx
import { cookies } from 'next/headers'

export default async function Page() {
  const cookieStore = await cookies()
  const theme = cookieStore.get('theme')
  return '...'
}

Getting all cookies

app/page.tsx
import { cookies } from 'next/headers'

export default async function Page() {
  const cookieStore = await cookies()
  return cookieStore.getAll().map((cookie) => (
    <div key={cookie.name}>
      <p>Name: {cookie.name}</p>
      <p>Value: {cookie.value}</p>
    </div>
  ))
}
app/actions.ts
'use server'

import { cookies } from 'next/headers'

export async function create() {
  const cookieStore = await cookies()

  cookieStore.set('name', 'lee')
  // or with options
  cookieStore.set('name', 'lee', { secure: true })
  // or with an object
  cookieStore.set({
    name: 'name',
    value: 'lee',
    httpOnly: true,
    path: '/',
  })
}
app/page.tsx
import { cookies } from 'next/headers'

export default async function Page() {
  const cookieStore = await cookies()
  const hasCookie = cookieStore.has('theme')
  return '...'
}

Deleting cookies

app/actions.ts
'use server'

import { cookies } from 'next/headers'

export async function deleteCookie() {
  const cookieStore = await cookies()
  cookieStore.delete('name')
}

Version history

VersionChanges
v15.0.0-RCcookies is now async. A codemod is available.
v13.0.0cookies introduced.

Build docs developers (and LLMs) love