Skip to main content
Better Auth integrates with Express using the toNodeHandler adapter from better-auth/node. Before you start, make sure you have a Better Auth instance configured. If you haven’t done that yet, check out the installation.
CommonJS (CJS) is not supported. Use ECMAScript Modules (ESM) by setting "type": "module" in your package.json or configuring your tsconfig.json to target ES modules.

Mount the handler

Create a catch-all route for /api/auth/* and pass it to toNodeHandler. Mount any express.json() middleware after the Better Auth handler — placing express.json() before it will cause the client to hang on pending requests.
server.ts
After starting your server, send a GET request to /api/auth/ok to confirm Better Auth is running.

CORS configuration

Install the cors package and register the middleware before your routes:
server.ts

Accessing the session in routes

Better Auth expects a web-standard Headers object. Use the fromNodeHeaders helper from better-auth/node to convert Express’s req.headers into the correct format.
server.ts