Express Adapter
Adapter for integrating Better Auth Studio with Express.js applications.
Import
import { betterAuthStudio } from 'better-auth-studio/express' ;
betterAuthStudio()
Creates an Express Router instance configured to handle Better Auth Studio requests.
Function Signature
function betterAuthStudio ( config : StudioConfig ) : Router
Parameters
Studio configuration object containing authentication and feature settings. Show StudioConfig properties
Your Better Auth instance
Base path for the studio routes. Default: "/api/studio"
Access control configuration for the studio
Customization options for branding and theming
Last-seen tracking configuration
IP geolocation provider configuration
Event tracking and ingestion configuration
Tools configuration with exclude list
Returns
An Express Router instance that handles all HTTP methods (*)
Usage
Basic Setup
import express from 'express' ;
import { betterAuthStudio } from 'better-auth-studio/express' ;
import { auth } from './auth' ;
import { defineStudioConfig } from 'better-auth-studio' ;
const app = express ();
// Required: Parse JSON bodies
app . use ( express . json ());
const studioConfig = defineStudioConfig ({
auth ,
basePath: '/api/studio' ,
metadata: {
title: 'My App Studio' ,
theme: 'dark'
},
events: {
enabled: true ,
tableName: 'auth_events'
}
});
const studioRouter = betterAuthStudio ( studioConfig );
// Mount the router
app . use ( '/api/studio' , studioRouter );
app . listen ( 3000 , () => {
console . log ( 'Server running on http://localhost:3000' );
console . log ( 'Studio available at http://localhost:3000/api/studio' );
});
With TypeScript
import type { Router as ExpressRouter } from 'express' ;
import express , { type Request , type Response , type NextFunction } from 'express' ;
import { betterAuthStudio } from 'better-auth-studio/express' ;
import { auth } from './auth' ;
const app = express ();
app . use ( express . json ());
const studioRouter : ExpressRouter = betterAuthStudio ({
auth ,
basePath: '/api/studio'
});
app . use ( '/api/studio' , studioRouter );
With Middleware
import express from 'express' ;
import cors from 'cors' ;
import helmet from 'helmet' ;
import { betterAuthStudio } from 'better-auth-studio/express' ;
import { auth } from './auth' ;
const app = express ();
// Global middleware
app . use ( cors ());
app . use ( helmet ());
app . use ( express . json ());
// Studio-specific middleware
const studioRouter = betterAuthStudio ({
auth ,
access: {
roles: [ 'admin' ],
allowEmails: [ '[email protected] ' ]
}
});
app . use ( '/api/studio' , studioRouter );
Implementation Details
Request Conversion
The adapter converts Express Request objects to the universal format:
type UniversalRequest = {
url : string ; // req.originalUrl (includes basePath)
method : string ; // req.method
headers : Record < string , string >; // req.headers
body ?: any ; // req.body (requires express.json())
};
Response Handling
The adapter handles responses by:
Setting the HTTP status code
Setting all response headers
Sending the body (supports both Buffer and string)
Error Handling
Errors are passed to Express error-handling middleware via next(error):
router . all ( '*' , async ( req , res , next ) => {
try {
// Handle request
} catch ( error ) {
next ( error ); // Express will handle the error
}
});
Required Middleware
You must use express.json() middleware before mounting the studio router to parse JSON request bodies.
app . use ( express . json ()); // Required!
app . use ( '/api/studio' , betterAuthStudio ( config ));