Skip to main content
headers is an async function that lets you read the HTTP incoming request headers from a Server Component.
app/page.tsx
import { headers } from 'next/headers'

export default async function Page() {
  const headersList = await headers()
  const userAgent = headersList.get('user-agent')
}

Parameters

headers takes no parameters.

Returns

headers returns a read-only Web Headers object.
get(name)
string | null
Returns the string value of the named header, or null if not present.
has(name)
boolean
Returns true if the header exists.
keys()
IterableIterator<string>
Returns an iterator over all header names.
values()
IterableIterator<string>
Returns an iterator over all header values.
entries()
IterableIterator<[string, string]>
Returns an iterator over all [name, value] pairs.
forEach(callback)
void
Executes a callback for each key/value pair.

Good to know

  • headers is async and returns a promise. Use async/await or React’s use.
  • In Next.js 14 and earlier, headers was synchronous. Synchronous access still works in Next.js 15 for backwards compatibility, but is deprecated.
  • The returned Headers object is read-only — you cannot set or delete headers.
  • headers is a Request-time API. Using it opts the route into dynamic rendering.

Examples

Forwarding the Authorization header

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

export default async function Page() {
  const authorization = (await headers()).get('authorization')
  const res = await fetch('https://api.example.com/user', {
    headers: { authorization }, // forward the authorization header
  })
  const user = await res.json()

  return <h1>{user.name}</h1>
}

Reading multiple headers

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

export default async function Page() {
  const headersList = await headers()

  return (
    <ul>
      {[...headersList.entries()].map(([name, value]) => (
        <li key={name}>
          {name}: {value}
        </li>
      ))}
    </ul>
  )
}

Version history

VersionChanges
v15.0.0-RCheaders is now async. A codemod is available.
v13.0.0headers introduced.

Build docs developers (and LLMs) love