rembrembdocs

cookies

Last updated April 23, 2026

cookies is an async function that allows you to read the HTTP incoming request cookies in Server Components, and read/write outgoing request cookies in Server Functions or Route Handlers.

app/page.tsx

JavaScriptTypeScript

import { cookies } from 'next/headers'
 
export default async function Page() {
  const cookieStore = await cookies()
  const theme = cookieStore.get('theme')
  return '...'
}

Reference

Methods

The following methods are available:

MethodReturn TypeDescription
get('name')ObjectAccepts a cookie name and returns an object with the name and value.
getAll()Array of objectsReturns a list of all the cookies with a matching name.
has('name')BooleanAccepts a cookie name and returns a boolean based on if the cookie exists.
set(name, value, options)-Accepts a cookie name, value, and options and sets the outgoing request cookie.
delete(name)-Accepts a cookie name and deletes the cookie.
toString()StringReturns a string representation of the cookies.

Options

When setting a cookie, the following properties from the options object are supported:

OptionTypeDescription
nameStringSpecifies the name of the cookie.
valueStringSpecifies the value to be stored in the cookie.
expiresDateDefines the exact date when the cookie will expire.
maxAgeNumberSets the cookie’s lifespan in seconds.
domainStringSpecifies the domain where the cookie is available.
pathString, default: '/'Limits the cookie's scope to a specific path within the domain.
secureBooleanEnsures the cookie is sent only over HTTPS connections for added security.
httpOnlyBooleanRestricts the cookie to HTTP requests, preventing client-side access.
sameSiteBoolean, 'lax', 'strict', 'none'Controls the cookie's cross-site request behavior.
priorityString ("low", "medium", "high")Specifies the cookie's priority
partitionedBooleanIndicates whether the cookie is partitioned.

The only option with a default value is path.

To learn more about these options, see the MDN docs.

Good to know

Understanding Cookie Behavior in Server Components

When working with cookies in Server Components, it's important to understand that cookies are fundamentally a client-side storage mechanism:

The server can only send instructions (via Set-Cookie headers) to tell the browser to store cookies - the actual storage happens on the client side. This is why cookie operations that modify state (.set, .delete) must be performed in a Server Function or Route Handler where the response headers can be properly set.

Understanding Cookie Behavior in Server Functions

After you set or delete a cookie in a Server Function, Next.js can return both the updated UI and new data in a single server roundtrip when the function is used as a Server Action (e.g., passed to a form's action prop). See Caching and Revalidating.

The UI is not unmounted, but effects that depend on data coming from the server will re-run.

To refresh cached data too, call revalidatePath or revalidateTag inside the function.

Examples

Getting a cookie

You can use the (await cookies()).get('name') method to get a single cookie:

app/page.tsx

JavaScriptTypeScript

import { cookies } from 'next/headers'
 
export default async function Page() {
  const cookieStore = await cookies()
  const theme = cookieStore.get('theme')
  return '...'
}

Getting all cookies

You can use the (await cookies()).getAll() method to get all cookies with a matching name. If name is unspecified, it returns all the available cookies.

app/page.tsx

JavaScriptTypeScript

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>
  ))
}

Setting a cookie

You can use the (await cookies()).set(name, value, options) method in a Server Function or Route Handler to set a cookie. The options object is optional.

app/actions.ts

JavaScriptTypeScript

'use server'
 
import { cookies } from 'next/headers'
 
export async function create(data) {
  const cookieStore = await cookies()
 
  cookieStore.set('name', 'lee')
  // or
  cookieStore.set('name', 'lee', { secure: true })
  // or
  cookieStore.set({
    name: 'name',
    value: 'lee',
    httpOnly: true,
    path: '/',
  })
}

Checking if a cookie exists

You can use the (await cookies()).has(name) method to check if a cookie exists:

app/page.ts

JavaScriptTypeScript

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

Deleting cookies

There are three ways you can delete a cookie.

Using the delete() method:

app/actions.ts

JavaScriptTypeScript

'use server'
 
import { cookies } from 'next/headers'
 
export async function deleteCookie(data) {
  const cookieStore = await cookies()
  cookieStore.delete('name')
}

Setting a new cookie with the same name and an empty value:

app/actions.ts

JavaScriptTypeScript

'use server'
 
import { cookies } from 'next/headers'
 
export async function deleteCookie(data) {
  const cookieStore = await cookies()
  cookieStore.set('name', '')
}

Setting the maxAge to 0 will immediately expire a cookie. maxAge accepts a value in seconds.

app/actions.ts

JavaScriptTypeScript

'use server'
 
import { cookies } from 'next/headers'
 
export async function deleteCookie(data) {
  const cookieStore = await cookies()
  cookieStore.set('name', 'value', { maxAge: 0 })
}

Version History

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

Was this helpful?