Prerequisites
Before you begin, ensure you have:- An Express.js application
- Better Auth configured in your project
better-auth-studioinstalled as a dependency
Installation
Initialize Configuration
Run the init command to create your configuration file:This creates a
studio.config.ts file:studio.config.ts
Add Studio Handler to Express App
Integrate the studio handler into your Express application:
server.ts
The
express.json() middleware is required for the studio to parse request bodies.How It Works
The Express adapter (better-auth-studio/express) provides a betterAuthStudio() function that:
- Returns an Express Router - Creates a router that handles all studio routes
- Converts requests - Transforms Express requests to universal format
- Handles all HTTP methods - Automatically handles GET, POST, PUT, DELETE, PATCH via
router.all("*") - Injects hooks - Automatically injects last-seen tracking and event hooks if configured
- Error handling - Passes errors to Express error handlers via
next(error)
Adapter Implementation
Here’s how the Express adapter works internally:src/adapters/express.ts
Configuration Examples
Basic Setup
Minimal Express configuration:server.ts
With Access Control
Restrict access to admin users:server.ts
studio.config.ts
With Custom Middleware
Add custom authentication or logging middleware:server.ts
Complete Server Example
Full Express server with Better Auth and Studio:server.ts
TypeScript Setup
For TypeScript projects, ensure proper configuration:tsconfig.json
Custom Base Path
Mount the studio at any path:- /admin
- /dashboard/admin
studio.config.ts
server.ts
http://localhost:3000/adminDeployment
Using Node.js
Deploy your Express app with Node.js:Using Docker
Create a Dockerfile:Dockerfile
Using PM2
Run with PM2 for process management:Troubleshooting
JSON Parsing Errors
If you get JSON parsing errors, ensureexpress.json() is used before the studio:
CORS Issues
If accessing from a different origin, enable CORS:Route Conflicts
Ensure the studio is mounted before other catch-all routes:Next Steps
Configuration
Learn about all configuration options
Next.js Setup
Set up with Next.js