Skip to main content

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.

Usage

better-auth-studio init [options]

Parameters

--api-dir
string
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

  1. Detects your framework - Analyzes project files and dependencies
  2. Creates studio.config.ts - Studio configuration file
  3. Generates route handlers - Framework-specific API routes (for Next.js and SvelteKit)
  4. 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:
better-auth-studio init
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

better-auth-studio init
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:
better-auth-studio init
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

CodeDescription
0Initialization completed successfully
1Initialization 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:
FrameworkImport 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:
  1. Review the generated config - Update paths, metadata, and access controls in studio.config.ts
  2. Install dependencies - Ensure better-auth-studio is in your production dependencies
  3. Start your app - Run your framework’s dev command (pnpm dev, npm run dev, etc.)
  4. Access the dashboard - Navigate to http://localhost:PORT/api/studio
  5. Configure access control - Set up role-based or email-based access restrictions
See the Self-Hosting Guide for detailed configuration options.

Build docs developers (and LLMs) love