Install the package
Add Better Auth to your project:
If you’re using a separate client and server setup, install Better Auth in both parts of your project.
Set environment variables
Create a Base URL
.env file in the root of your project and add the following:Secret keyA secret value used for encryption and hashing. It must be at least 32 characters and generated with high entropy. You can run openssl rand -base64 32 to generate one..env
Need to rotate your secret later? Use
BETTER_AUTH_SECRETS (plural) to roll over to a new secret without invalidating existing sessions. See the secrets option for details..env
Create your auth instance
Create a file named
auth.ts in one of the following locations:- Project root
lib/folderutils/folder
src/, app/, or server/ (e.g. src/lib/auth.ts).Import Better Auth and export your auth instance. The variable must be named auth or use a default export.auth.ts
Configure your database
Better Auth requires a database to store user data. You can connect directly or use an ORM adapter.Direct database connectionORM adapters
You can also run Better Auth in stateless mode by omitting the database option. See Stateless Session Management for details. Note that most plugins require a database.
- SQLite
- PostgreSQL
- MySQL
auth.ts
- Drizzle
- Prisma
- MongoDB
auth.ts
If your database is not listed above, see other supported databases or check the full list of ORM adapters.
Create database tables
Better Auth includes a CLI to manage the schema required by the library.
-
Generate — creates an ORM schema or SQL migration file:
-
Migrate — creates the required tables directly in the database (available only for the built-in Kysely adapter):
If you prefer to create the schema manually, see the core schema in the database documentation.
Configure authentication methods
Configure the authentication methods you want to support. Better Auth has built-in support for email/password and social providers.
auth.ts
You can add more authentication methods — including passkeys, magic links, and username — through plugins.
Mount the route handler
Set up a route handler on your server to handle auth API requests. By default, Better Auth handles requests at
/api/auth/*.Better Auth supports any backend framework that uses standard
Request and Response objects, and provides helper functions for popular frameworks.- Next.js (App Router)
- Next.js (Pages Router)
- Nuxt
- SvelteKit
- Hono
- Express
- Expo
app/api/auth/[...all]/route.ts
Create the client instance
The client SDK lets you interact with the auth server from your frontend. Import You can also export specific methods directly:
createAuthClient from the package for your framework.If your auth server runs on a different domain than your client, pass the full base URL. If they share the same domain, you can omit
baseURL.- React
- Vue
- Svelte
- Solid
- Vanilla
lib/auth-client.ts
lib/auth-client.ts
Done
You’re ready to use Better Auth in your application. Continue to basic usage to learn how to sign users in, manage sessions, and more.