[GH-ISSUE #3588] Failed to getSession using x-api-key #9655

Closed
opened 2026-04-13 05:15:52 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @cschroeter on GitHub (Jul 24, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/3588

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

// api/foo/route.ts (Nextjs)
import { headers } from 'next/headers'
import { auth } from '~/lib/auth'
import { authClient } from '~/lib/auth-client'

export const GET = async () => {
  // Works
  const serverSession = await auth.api.getSession({
    headers: await headers(),
  })

  // Does not work
  const clientSession = await authClient.getSession({
    fetchOptions: {
      headers: await headers(),
    },
  })

  console.log('Server Session:', serverSession)
  console.log('Client Session:', clientSession)

  return Response.json({ ok: true })
}

Current vs. Expected behavior

I can not get a session with a given x-api-key using the import { authClient } from '~/lib/auth-client' instead I get this error:

[Error: No response is returned from route handler '/Users/christian/my-app/src/app/api/auth/[...all]/route.ts'. Ensure you return a `Response` or a `NextResponse` in all branches of your handler.]
 GET /api/auth/get-session 500 in 366ms
Client Session: {
  data: null,
  error: { status: 500, statusText: 'Internal Server Error' }
}

Expected behaviour is to return the session for that given x-api-keyheader.

Using cookies return the correct session both cases

What version of Better Auth are you using?

latest

Provide environment information

Does not apply

Which area(s) are affected? (Select all that apply)

Client

Auth config (if applicable)

import { betterAuth } from "better-auth"
import { nextCookies } from 'better-auth/next-js'
import { apiKey } from 'better-auth/plugins'

export const auth = betterAuth({
  plugins: [apiKey(), nextCookies()],
});

Additional context

No response

Originally created by @cschroeter on GitHub (Jul 24, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/3588 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce ```tsx // api/foo/route.ts (Nextjs) import { headers } from 'next/headers' import { auth } from '~/lib/auth' import { authClient } from '~/lib/auth-client' export const GET = async () => { // Works const serverSession = await auth.api.getSession({ headers: await headers(), }) // Does not work const clientSession = await authClient.getSession({ fetchOptions: { headers: await headers(), }, }) console.log('Server Session:', serverSession) console.log('Client Session:', clientSession) return Response.json({ ok: true }) } ``` ### Current vs. Expected behavior I can not get a session with a given `x-api-key` using the `import { authClient } from '~/lib/auth-client'` instead I get this error: ```text [Error: No response is returned from route handler '/Users/christian/my-app/src/app/api/auth/[...all]/route.ts'. Ensure you return a `Response` or a `NextResponse` in all branches of your handler.] GET /api/auth/get-session 500 in 366ms Client Session: { data: null, error: { status: 500, statusText: 'Internal Server Error' } } ``` Expected behaviour is to return the session for that given `x-api-key`header. > Using cookies return the correct session both cases ### What version of Better Auth are you using? latest ### Provide environment information ```bash Does not apply ``` ### Which area(s) are affected? (Select all that apply) Client ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" import { nextCookies } from 'better-auth/next-js' import { apiKey } from 'better-auth/plugins' export const auth = betterAuth({ plugins: [apiKey(), nextCookies()], }); ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-13 05:15:52 -05:00
Author
Owner

@ping-maxwell commented on GitHub (Jul 26, 2025):

It looks like you're using authClient in a nextjs API route, which we don't support.
The authClient in this case should be used on the client/front-end, and auth.apis are intended for backend use (such as API routes).

<!-- gh-comment-id:3121497623 --> @ping-maxwell commented on GitHub (Jul 26, 2025): It looks like you're using authClient in a nextjs API route, which we don't support. The authClient in this case should be used on the client/front-end, and auth.apis are intended for backend use (such as API routes).
Author
Owner

@himself65 commented on GitHub (Sep 19, 2025):

It looks like you're using authClient in a nextjs API route, which we don't support. The authClient in this case should be used on the client/front-end, and auth.apis are intended for backend use (such as API routes).

No, this is a wrong assumption. The client should work well on the server-side rendering. I'm re-open this

<!-- gh-comment-id:3313468299 --> @himself65 commented on GitHub (Sep 19, 2025): > It looks like you're using authClient in a nextjs API route, which we don't support. The authClient in this case should be used on the client/front-end, and auth.apis are intended for backend use (such as API routes). No, this is a wrong assumption. The client should work well on the server-side rendering. I'm re-open this
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9655