Skip to main content
0 reactions

How to Add the Better Auth Route Handler in Next.js

A dark indigo card titled The Better Auth Route Handler, labelled Better Auth How-To, with the line One file connects /api/auth to Better Auth in Next.js, beside four stacked boxes joined by arrows: Browser, /api/auth/[...all], Better Auth and PostgreSQL.
How to add the Better Auth route handler in Next.js.

Better Auth answers every sign-in, sign-up and session request at one address, /api/auth. In a Next.js app, a single route file connects that address to Better Auth. This guide adds the Better Auth route handler in Next.js with toNextJsHandler and checks that it answers.

The short answer: create app/api/auth/[...all]/route.ts with export const { POST, GET } = toNextJsHandler(auth);, then open /api/auth/ok in your browser.

Syntax

SyntaxTypeScript
export const { POST, GET } = toNextJsHandler(auth);
  • toNextJsHandler(auth) takes your Better Auth instance and returns one request handler under the names Next.js expects: GET, POST, PATCH, PUT and DELETE.
  • export const { POST, GET } = … exports the two that Better Auth’s documentation uses, so Next.js sends GET and POST requests for this route to Better Auth.
  • auth is the instance you create with betterAuth(), usually in lib/auth.ts.

The reference: Next.js integration (opens in a new tab) in the Better Auth documentation, and route.js (opens in a new tab) in the Next.js documentation.

Prerequisites

  • a Next.js app that uses the App Router (the app folder)
  • Better Auth installed, with its instance in lib/auth.ts (Installation (opens in a new tab) in the Better Auth documentation)

Step 1: Create the Route File

In the app folder, create the folders api, then auth inside it, then [...all] inside that, with the square brackets and the three dots. In the last one, create route.ts:

app/api/auth/[…all]/route.tsTypeScript
import { toNextJsHandler } from "better-auth/next-js";
import { auth } from "@/lib/auth";

export const { POST, GET } = toNextJsHandler(auth);
A code editor window titled app/api/auth/[...all]/route.ts with three lines: import toNextJsHandler from better-auth/next-js, import auth from @/lib/auth, and export const POST, GET equals toNextJsHandler(auth).
The whole route handler: three lines in app/api/auth/[…all]/route.ts.
  • import { toNextJsHandler } from "better-auth/next-js"; loads Better Auth’s helper for Next.js.
  • import { auth } from "@/lib/auth"; loads your Better Auth instance. @/ is the project folder, as set up by create-next-app.
  • [...all] is a catch-all folder name: this one file answers /api/auth and every address below it, such as /api/auth/sign-in/email.

Step 2: Start the App

TerminalBash
npm run dev
OutputPlain text
npm notice run bookshop@0.1.0 dev
npm notice run next dev
▲ Next.js 16.4.6 (Turbopack)
- Local:         http://localhost:3000
- Network:       http://192.168.x.x:3000
- Environments: .env
✓ Ready in 282ms
✓ Running next.config.ts took 80ms
- Cache Components enabled
- Partial Prefetching enabled

Step 3: Test the Route

Open http://localhost:3000/api/auth/ok in your browser. Better Auth answers through your route handler:

A browser showing the reply from localhost:3000/api/auth/ok: the JSON text ok true.
Better Auth’s reply at /api/auth/ok.

Then open http://localhost:3000/api/auth/get-session. Nobody is signed in, so Better Auth answers null:

A browser showing the reply from localhost:3000/api/auth/get-session: the word null.
No session yet: get-session answers null.

Both answers come from Better Auth, so the route handler works. Every sign-in feature you add later uses this same route.

Common Mistakes

MistakeWhat happensDo this instead
Naming the folder [all] or ...allOnly one part of the address matches, or noneUse exactly [...all]
Putting the file in pages/api in an App Router appNext.js treats it as a Pages Router routeUse app/api/auth/[...all]/route.ts
Changing Better Auth’s basePath but not the foldersRequests go to an address with no routeKeep /api/auth, as the documentation recommends

FAQ

Do I need to export PATCH, PUT and DELETE?

Better Auth’s Next.js setup exports GET and POST. toNextJsHandler also returns the other three, all running the same handler, if you ever need them.

What about the Pages Router?

Better Auth’s documentation uses toNodeHandler from better-auth/node there, in pages/api/auth/[...all].ts, with the body parser turned off.

Can I protect pages with this route?

No. This route only answers Better Auth’s own requests. Checking the session on your pages is a separate step, which Part 4 of the series covers.

Conclusion

One file, app/api/auth/[...all]/route.ts, with toNextJsHandler(auth), connects Better Auth to your Next.js app. /api/auth/ok answering {"ok":true} shows it works.

Additional Resources

Better Auth, Next.js and other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by Better Auth, Inc. or Vercel.

0 comments