Better Auth integrates with Express using theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/better-auth/better-auth/llms.txt
Use this file to discover all available pages before exploring further.
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.
- Express v4
- Express v5
server.ts
GET request to /api/auth/ok to confirm Better Auth is running.
CORS configuration
Install thecors package and register the middleware before your routes:
server.ts
Accessing the session in routes
Better Auth expects a web-standardHeaders object. Use the fromNodeHeaders helper from better-auth/node to convert Express’s req.headers into the correct format.
server.ts