Overview
The init command sets up Better Auth Studio for self-hosting by creating the necessary configuration files and route handlers for your framework.
Self-hosting is currently in beta. Please report any issues on GitHub.
better-auth-studio init [options]
Parameters
Custom app directory path for Next.js projects.By default, the command auto-detects app or src/app directories. Use this option when your Next.js app directory is in a non-standard location.Examples:# Standard locations (auto-detected)
better-auth-studio init
# Custom app directory
better-auth-studio init --api-dir "app"
better-auth-studio init --api-dir "src/app"
better-auth-studio init --api-dir "custom/app"
This option is only relevant for Next.js projects. For other frameworks, it will be ignored.
Supported Frameworks
The init command automatically detects your framework and generates the appropriate setup:
- Next.js (App Router)
- SvelteKit
- Express (manual setup required)
- Other frameworks (manual setup instructions provided)
What It Does
- Detects your framework - Analyzes project files and dependencies
- Creates
studio.config.ts - Studio configuration file
- Generates route handlers - Framework-specific API routes (for Next.js and SvelteKit)
- Displays setup instructions - Console output with next steps
Generated Files
studio.config.ts
Created in the project root:
import type { StudioConfig } from 'better-auth-studio';
import { auth } from '@/lib/auth'; // Path varies by framework
const config: StudioConfig = {
auth,
basePath: '/api/studio',
metadata: {
title: 'Admin Dashboard',
theme: 'dark',
},
};
export default config;
Next.js Route Handler
Created at app/api/studio/[[...path]]/route.ts (or src/app/api/studio/[[...path]]/route.ts):
import { betterAuthStudio } from 'better-auth-studio/nextjs';
import studioConfig from '@/studio.config';
const handler = betterAuthStudio(studioConfig);
export {
handler as GET,
handler as POST,
handler as PUT,
handler as DELETE,
handler as PATCH,
};
SvelteKit Route Handler
Created at src/routes/api/studio/[...path]/+server.ts:
import { betterAuthStudio } from 'better-auth-studio/svelte-kit';
import studioConfig from '../../../../../studio.config.js';
const handler = betterAuthStudio(studioConfig);
export async function GET(event) {
return handler(event);
}
export async function POST(event) {
return handler(event);
}
export async function PUT(event) {
return handler(event);
}
export async function DELETE(event) {
return handler(event);
}
export async function PATCH(event) {
return handler(event);
}
Examples
Next.js Project (Auto-detect)
The command will automatically detect your app directory:
Console Output:
π Initializing Better Auth Studio...
π Detected framework: nextjs
β
Created config: studio.config.ts
π Auto-detected app directory: src/app
β
Generated route file: src/app/api/studio/[[...path]]/route.ts
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
Next.js Setup Complete! β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£
β β
β π Files created: β
β β’ studio.config.ts β
β β’ src/app/api/studio/[[...path]]/route.ts β
β β
β β οΈ Important: Ensure better-auth-studio is in dependencies β
β (not devDependencies) for production deployments β
β β
β π Start your app: β
β pnpm dev β
β β
β π Dashboard will be at: β
β http://localhost:3000/api/studio β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Next.js with Custom App Directory
Specify a custom app directory location:
better-auth-studio init --api-dir "custom/app"
Console Output:
π Initializing Better Auth Studio...
π Detected framework: nextjs
β
Created config: studio.config.ts
π Using custom API directory: custom/app
β
Generated route file: custom/app/api/studio/[[...path]]/route.ts
SvelteKit Project
Console Output:
π Initializing Better Auth Studio...
π Detected framework: sveltekit
β
Created config: studio.config.ts
β
Generated route file: src/routes/api/studio/[...path]/+server.ts
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
SvelteKit Setup Complete! β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£
β β
β π Files created: β
β β’ studio.config.ts β
β β’ src/routes/api/studio/[...path]/+server.ts β
β β
β β οΈ Important: Ensure better-auth-studio is in dependencies β
β (not devDependencies) for production deployments β
β β
β π Start your app: β
β pnpm dev β
β β
β π Dashboard will be at: β
β http://localhost:5173/api/studio β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Express Project (Manual Setup)
For Express and unknown frameworks, manual setup instructions are provided:
Console Output:
π Initializing Better Auth Studio...
π Detected framework: express
β
Created config: studio.config.ts
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β π Manual Setup Required for Express β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£
β β
β π Created: studio.config.ts β
β β
β Add this to your server file: β
β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£
import { betterAuthStudio } from 'better-auth-studio/express';
import studioConfig from './studio.config';
app.use('/api/studio', betterAuthStudio(studioConfig));
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£
β β
β π Start your app and visit: β
β http://localhost:3000/api/studio β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Framework Detection
The init command detects your framework by checking for:
Next.js
next.config.js, next.config.mjs, or next.config.ts files
next in package.json dependencies
SvelteKit
svelte.config.js or svelte.config.ts files
src/routes directory
src/hooks.server.ts file
@sveltejs/kit in package.json dependencies
Express
- Server files:
src/index.ts, src/app.ts, src/server.ts, app.js, server.js, index.js
express in package.json dependencies
Exit Codes
| Code | Description |
|---|
0 | Initialization completed successfully |
1 | Initialization failed (error during setup) |
Error Conditions
File Already Exists
If studio.config.ts already exists:
β οΈ studio.config.ts already exists, skipping...
The command continues and creates/updates route files.
Route File Already Exists
If the route handler file already exists:
β οΈ Route file already exists: app/api/studio/[[...path]]/route.ts
The existing file is not overwritten.
Unknown Framework
If the framework cannot be detected:
π Detected framework: unknown
Manual setup instructions are provided.
Installation Note
For production self-hosting, install better-auth-studio as a regular dependency, not a devDependency:pnpm add better-auth-studio
The package is required at runtime when self-hosting.
For CLI usage only (not self-hosting), you can install as a dev dependency:
pnpm add -D better-auth-studio
Auth Import Paths
The generated studio.config.ts uses framework-specific import paths:
| Framework | Import Path |
|---|
| Next.js | @/lib/auth |
| SvelteKit | $lib/auth |
| Other | ./src/auth |
Make sure to update the import path in studio.config.ts if your auth file is in a different location.
Next Steps
After running init:
- Review the generated config - Update paths, metadata, and access controls in
studio.config.ts
- Install dependencies - Ensure
better-auth-studio is in your production dependencies
- Start your app - Run your frameworkβs dev command (
pnpm dev, npm run dev, etc.)
- Access the dashboard - Navigate to
http://localhost:PORT/api/studio
- Configure access control - Set up role-based or email-based access restrictions
See the Self-Hosting Guide for detailed configuration options.