.env.example at the root of the project.
Quick Setup
Copy the example file and customize it for your environment:Variable Reference
Database
PostgreSQL connection string used by Drizzle ORM to connect to your database.Scope: Server-side onlyDefault (Dev Container):Format:Used in:
If you’re using the Dev Container setup, the default value works out of the box. For manual PostgreSQL installations, update the host, port, and credentials accordingly.
src/db/index.ts:6- Database connection pooldrizzle.config.ts:8- Drizzle Kit CLI operations
Better Auth
Better Auth requires three environment variables: a secret for signing tokens, and server/client URLs for API endpoints.Secret key used to sign authentication tokens and cookies. Must be a strong, random string.Scope: Server-side onlyDefault:
change-me-to-a-random-secretGenerate a secure secret:The base URL where your Better Auth server is running. Used by server-side authentication logic.Scope: Server-side onlyDefault: Used in:
http://localhost:3000Production example:src/lib/auth.ts:20- Better Auth base URL configuration- OAuth callback URLs reference this as the redirect base
The base URL for Better Auth as seen from the browser. Exposed to client-side JavaScript.Scope: Client-side (exposed to browser)Default: Used in:
http://localhost:3000Production example:In most cases,
BETTER_AUTH_URL and NEXT_PUBLIC_BETTER_AUTH_URL will be identical. They’re separate to support advanced scenarios like internal vs. external routing.src/lib/auth-client.ts:5- Client-side auth SDK configuration
Google OAuth
To enable Google sign-in, you need OAuth 2.0 credentials from Google Cloud Console.OAuth 2.0 Client ID from Google Cloud Console.Scope: Server-side onlyDefault:
your-google-client-idHow to get:- Go to Google Cloud Console
- Create OAuth 2.0 Client ID credentials
- Set authorized redirect URI to:
{BETTER_AUTH_URL}/api/auth/callback/google
src/lib/auth.ts:15- Google OAuth provider configuration
OAuth 2.0 Client Secret from Google Cloud Console.Scope: Server-side onlyDefault:
your-google-client-secretUsed in:src/lib/auth.ts:16- Google OAuth provider configuration
Backend API (Optional)
If you’re integrating with an external backend service, configure these variables. If not set, backend-related features are disabled.The URL of your backend API service as accessed from the Next.js server. Used by the API proxy route.Scope: Server-side only (NOT exposed to browser)Default: Used in:
http://localhost:8080Purpose: The API proxy at /api/[...path] forwards requests to this URL with automatic JWT injection.This variable is commented out by default. Uncomment it if you have an external backend.
src/app/api/[...path]/route.ts:35- API proxy backend target
The URL of your backend API service as accessed from the browser. Used by client-side API clients.Scope: Client-side (exposed to browser)Default: Used in:
http://localhost:8080Purpose: The fetch and Axios API clients use this URL for direct browser-to-backend requests.If using the API proxy route, you may not need this variable. It’s primarily for direct client-to-backend communication.
src/lib/api-client.ts:28- Fetch-based API client base URLsrc/lib/api-client-axios.ts- Axios API client base URL
Environment Variable Scopes
Next.js distinguishes between server-side and client-side environment variables:| Prefix | Scope | Access |
|---|---|---|
| None | Server-side only | Only accessible in Node.js runtime (API routes, server components) |
NEXT_PUBLIC_ | Client-side | Exposed to the browser, included in JavaScript bundle |
Validation
The application will fail at runtime if required environment variables are missing. You’ll see errors like:-
Check all required variables are set:
-
Test database connection:
-
Start the dev server:
Production Considerations
Use environment variable management
Store secrets in your hosting platform’s environment variable system (Vercel, Railway, AWS, etc.) instead of
.env files.Rotate secrets regularly
Change
BETTER_AUTH_SECRET and OAuth credentials periodically, especially if team members leave.Use HTTPS everywhere
Set
BETTER_AUTH_URL and NEXT_PUBLIC_BETTER_AUTH_URL to https:// URLs in production. OAuth providers require HTTPS for redirect URIs.Related Documentation
- Database Configuration - PostgreSQL setup and connection troubleshooting
- Google OAuth Setup - Step-by-step Google Cloud Console configuration