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
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,PUTandDELETE.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.authis the instance you create withbetterAuth(), usually inlib/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
appfolder) - 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:
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).](https://i0.wp.com/jd-bots.com/wp-content/uploads/2026/11/better-auth-route-handler-nextjs-01-route-ts-code.webp?resize=728%2C284&ssl=1)
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/authand every address below it, such as/api/auth/sign-in/email.
Step 2: Start the App
npm run dev
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:

Then open http://localhost:3000/api/auth/get-session. Nobody is signed in, so Better Auth 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
| Mistake | What happens | Do this instead |
|---|---|---|
Naming the folder [all] or ...all | Only one part of the address matches, or none | Use exactly [...all] |
Putting the file in pages/api in an App Router app | Next.js treats it as a Pages Router route | Use app/api/auth/[...all]/route.ts |
Changing Better Auth’s basePath but not the folders | Requests go to an address with no route | Keep /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
- Next.js integration (opens in a new tab) in the Better Auth documentation
- Mount handler (opens in a new tab) in the Better Auth installation guide
- route.js (opens in a new tab) and dynamic routes (opens in a new tab) in Next.js
Related
- Better Auth Tutorial — Part 2: Set Up Better Auth in Next.js with PostgreSQL, where this route is part of the full setup
- How to Generate a Better Auth Secret
- npx auth migrate vs generate: Which Better Auth Command to Use
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