Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Next.js 16+ · Function

cookies

cookies is an asynchronous Next.js function for reading incoming request cookies in Server Components and reading or writing outgoing cookies in Server Functions or Route Handlers. Await it to access the cookie store.

Since
v15.0.0-RC

How it works

Import cookies from next/headers and await it to obtain a cookie store. The store reads incoming cookies in Server Components and can read or modify outgoing cookies in Server Functions or Route Handlers. Use get, getAll, has, set, delete, and toString as appropriate.

Syntax

const cookieStore = await cookies()

Parameters

NameTypeDescription
namestringCookie name used by get, getAll, has, or delete.
valuestringValue for the cookie being written.
optionsobjectCookie attributes such as path, secure, httpOnly, sameSite, maxAge, or expires.

Returns

A promise that resolves to a cookie store supporting get, getAll, has, set, delete, and toString.

Key points

  • Use cookies to read request cookies in Server Components.
  • Use Server Functions or Route Handlers when you need to modify outgoing cookies.
  • getAll(name) returns every matching cookie; omit the name to retrieve all cookies.
  • has(name) checks whether a named cookie exists.
  • set(name, value, options) writes a cookie with configurable options.
  • delete(name) removes a named cookie.

Common mistakes

  • Because cookies is asynchronous, call it with await or React's use function.
  • Set or delete cookies only in a Server Function or Route Handler, before streaming begins.

Example: Read a cookie in a Server Component

import { cookies } from 'next/headers'

export default async function Page() {
  const cookieStore = await cookies()
  const theme = cookieStore.get('theme')?.value ?? 'light'

  return <p>Theme: {theme}</p>
}

This Server Component awaits the cookie store and reads the value of the theme cookie, falling back to light.

When to use it

Read request cookies

Read preferences, feature flags, or session identifiers during server rendering.

Set cookies

Persist authentication or user-preference state from a Server Function.

Delete cookies

Remove a session or preference cookie from a Server Function or Route Handler.

  • next/headers
  • Server Components
  • Server Functions
  • Route Handlers
  • use

Source: Functions: cookies | Next.js. Checked 2026-10-15.