Cookies are missing on a fetch request in a Next.js app
fetch with credentials: 'include' sends no Cookie headerShort answer
In the browser, cookies on a cross-origin fetch need credentials include plus an API that answers with an exact origin and Access-Control-Allow-Credentials true, and a cookie the browser allows cross-site. In a Server Component there is no browser, so you read the cookie and pass it yourself.
Fix it fast
1Find out which cause you have
Decide where the fetch runs: in a Client Component (the browser sends cookies) or in a Server Component or Route Handler (no browser cookies). Then look at the response headers of the failing call.
curl -i -H "Origin: https://app.example.com" https://api.example.com/me2Match what you see, then jump to its fix
- 1
The request goes to another origin (another domain or port) and no Cookie header leaves the browser.credentials is not set, so the default same-origin mode appliesFix - 2
Console shows a CORS error mentioning the wildcard origin or Access-Control-Allow-Credentials.The API does not opt in to credentialed CORSFix - 3
CORS headers are correct but the Cookie header is still empty on a cross-site call.The browser blocks the cookie as a third-party cookieFix - 4
The call is in a Server Component or Route Handler, and the API sees no cookie at all.fetch runs on the Next.js server, not in the browserFix
- 1
What does the error look like?
fetch with credentials: 'include' sends no Cookie headerAccess-Control-Allow-Credentials
Cause 1 of 4
credentials is not set, so the default same-origin mode applies
1Check
fetch defaults to credentials same-origin, which sends cookies only to the same origin.
grep -rn "fetch(" --include="*.ts" --include="*.tsx" src | head2Fix
Add credentials include to the cross-origin call.
const res = await fetch("https://api.example.com/me", {
credentials: "include",
});3Verify
In the browser network tab, the request shows a Cookie header.
# DevTools > Network > the request > Request HeadersConfirmed by developer.mozilla.org
Cause 2 of 4
The API does not opt in to credentialed CORS
1Check
The browser rejects a credentialed response unless it has Access-Control-Allow-Credentials true and an explicit Access-Control-Allow-Origin, never *. Preflight responses need the credentials header too.
curl -i -X OPTIONS -H "Origin: https://app.example.com" -H "Access-Control-Request-Method: GET" https://api.example.com/me2Fix
On the API, answer with the exact origin, credentials true and Vary Origin.
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Credentials: true
Vary: Origin3Verify
Repeat the curl check; the three headers appear, and the browser stops reporting a CORS error.
curl -i -H "Origin: https://app.example.com" https://api.example.com/meConfirmed by developer.mozilla.org
Cause 3 of 4
The browser blocks the cookie as a third-party cookie
1Check
Third-party cookie policies and the cookie's SameSite attribute can stop it being sent even when the server allows credentials. Look at the Set-Cookie header your API sends.
curl -s -D - -o /dev/null https://api.example.com/login | grep -i set-cookie2Fix
Set the SameSite attribute to suit your setup, and where you can, serve the API from the same site as the app so the cookie is first-party.
Set-Cookie: session=abc; Secure; HttpOnly; SameSite=Lax3Verify
Reload the app and check the Cookie header in the network tab.
# DevTools > Application > CookiesConfirmed by developer.mozilla.org
Cause 4 of 4
fetch runs on the Next.js server, not in the browser
1Check
Server-side fetch has no browser cookie jar. The incoming request cookies are readable with cookies() from next/headers, which is async in Next.js 15.
grep -rn "next/headers" --include="*.ts" --include="*.tsx" .2Fix
Read the cookies on the server and send them in a Cookie header.
import { cookies } from "next/headers";
export default async function Page() {
const cookieStore = await cookies();
const res = await fetch("https://api.example.com/me", {
headers: { Cookie: cookieStore.toString() },
});
const me = await res.json();
return <p>{me.name}</p>;
}3Verify
Load the page signed in and check that the API now returns the user.
npm run devConfirmed by nextjs.org
How do I stop it coming back?
- Keep the API and the app on the same site so cookies stay first-party.
- Never answer credentialed requests with Access-Control-Allow-Origin *.
- Do auth-bound fetches on the server and pass cookies explicitly.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
Clerk cannot read its publishable key in a Next.js app
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is undefined
Clerk reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY.
3 causes, each with a fixWeb apps
The shell cannot find create-next-app
command not found: create-next-app
create-next-app is not meant to be installed as a shell command.
2 causes, each with a fixWeb apps
ESLint fails with: Cannot find module 'next/babel'
ESLint stops on the first file with a Babel or parser error instead of listing lint results
next/babel is a Babel preset that ships inside the next package.
3 causes, each with a fixWeb apps
Firebase Auth says the API key is not valid
Firebase: Error (auth/api-key-not-valid.-please-pass-a-valid-api-key.)
The apiKey your app sends is either empty, wrong, or blocked.
4 causes, each with a fixWeb apps
Jest fails with SyntaxError: Unexpected token when testing a Next.js component
Jest failed to parse a file.
Jest only understands plain JavaScript until a transform and a few file mappings tell it more.
3 causes, each with a fix