headers

The headers function allows you to read the HTTP incoming request headers from a Server Component ↗.

headers()

This API extends the Web Headers API ↗. It is read-only, meaning you cannot set / delete the outgoing request headers.

app/page.tsx

import { headers } from 'next/headers'
 
export default function Page() {
  const headersList = headers()
  const referer = headersList.get('referer')
 
  return <div>Referer: {referer}</div>
}

Good to know:

API Reference

const headersList = headers()

Parameters

headers does not take any parameters.

Returns

headers returns a read-onlyWeb Headers ↗ object.

Examples

Usage with Data Fetching

headers() can be used in combination with Suspense for Data Fetching ↗.

app/page.js

import { headers } from 'next/headers'
 
async function getUser() {
  const headersInstance = headers()
  const authorization = headersInstance.get('authorization')
  // Forward the authorization header
  const res = await fetch('...', {
    headers: { authorization },
  })
  return res.json()
}
 
export default async function UserPage() {
  const user = await getUser()
  return <h1>{user.name}</h1>
}

Version History

Version Changes
v13.0.0 headers introduced.
Last updated on