Expose Better Auth endpoints
Next.js App Router
Better Auth · Function
toNextJsHandler adapts a configured Better Auth instance for Next.js App Router route handlers. Export the returned HTTP methods from the authentication catch-all route.
toNextJsHandler adapts a configured Better Auth instance to Next.js App Router route handlers. Call it with your auth instance, then export the HTTP methods returned by the adapter from the catch-all route.
toNextJsHandler(auth)| Name | Type | Description |
|---|---|---|
auth | Better Auth instance | The configured Better Auth instance whose authentication endpoints should be exposed. |
An object containing Next.js route handlers, including GET and POST handlers for the authentication endpoint.
import { auth } from "@/lib/auth";
import { toNextJsHandler } from "better-auth/next-js";
export const { GET, POST } = toNextJsHandler(auth);Place this in the App Router catch-all route at app/api/auth/[...all]/route.ts.
Next.js App Router
Plugin endpoints requiring several verbs
It converts a Better Auth instance into Next.js App Router handlers. The returned methods are exported from the catch-all route so Next.js can process authentication requests.
Follow-up: How would you expose additional HTTP methods for a plugin such as SCIM? Export those methods from the object returned by the adapter, for example GET, POST, PUT, PATCH, and DELETE.
Import the function from better-auth/next-js, pass the configured auth instance, and export the returned GET and POST handlers from app/api/auth/[...all]/route.ts.
Follow-up: Why is a catch-all route used? It lets the Better Auth endpoint receive requests for the framework's authentication API paths under the configured base path.
The function is intended for the App Router route-handler model. For the Pages Router, the documentation specifies toNodeHandler and requires disabling body parsing in the route configuration.
Follow-up: What configuration detail matters for Pages Router requests? Set bodyParser to false so the Node handler can process the request body itself.
toNodeHandlerBetter Auth instanceNext.js App RouterGETPOST