• ShipSaaSDocumentation
  • Introduction
  • Getting Started
    • Installation
    • Environment Setup
    • Database Setup
    • Development Guide
  • Database
    • Repository Pattern
    • Drizzle Queries
    • Migration Workflow
    • Drizzle Studio
    • Best Practices
  • File Management
    • File Upload Component
    • Server Actions
    • Storage Configuration
  • Authentication Methods
    • Credentials Authentication
    • Magic Link Authentication
    • Two-Factor Authentication (2FA)
    • Social Authentication
    • API Keys & Bearer Tokens
    • Organizations (Multi-tenant)
  • Authorization & Permissions
    • Roles & Hierarchy
    • Route Protection
    • API Protection
    • CASL Permissions
  • Email & Notifications
    • Resend Setup
    • React Email Templates
    • Notification System
    • Email Workflows
    • Customization & Extension
  • Stripe & Payments
    • Stripe Setup
    • Better Auth Integration
    • Checkout Methods
    • Subscription Management
    • Admin Dashboard
    • Webhook Events
  • Architecture & Development
    • Presentation Layer
    • Service Layer
    • Persistence Layer
    • Helpers Architecture
    • Project Structure
    • Development Workflow
  • Testing
    • General Testing
    • Unit Testing
    • UI Component Testing
    • Repository Testing
    • Service Testing
    • End-to-End Testing
  • Deployment
    • Vercel Deployment
    • GitHub Actions CI/CD
    • Database Backup & Restore
    • Database Migrations
    • Troubleshooting Deployment
  • AI Agents Integration
  • Blog Management
    • Blog Setup
    • MDX Blog
    • Database Blog
  • Async Tasks
    • Setup
    • Email Workflows
    • Payment Processing
    • Maintenance Jobs
  1. Documentation
  2. Getting Started
  3. Environment Setup
Buydocschangelogsdemocommunity

Environment Setup

PreviousInstallationNextDatabase Setup
On this page
Method 1: CLI Tool (Recommended)CLI FeaturesMethod 2: Manual ConfigurationRequired VariablesPayment Configuration (Stripe)Storage Configuration (Supabase)Social Authentication (Optional)Environment TypesDevelopment EnvironmentProduction EnvironmentTest EnvironmentValidation & SecurityTroubleshootingNext Steps

Configure your application environment with either the recommended CLI tool or manual configuration. The CLI tool provides smart defaults and validation, while manual setup offers full control.

Method 1: CLI Tool (Recommended)

🚀 Recommended: Use the interactive CLI for automatic configuration with smart defaults and validation.

1

Run the CLI tool

bash
pnpm init:env

The tool automatically detects all required environment variables and guides you through configuration.

2

Choose environment

Select your target environment:

  • Development: Local development (creates .env.development)
  • Production: Production deployment (creates .env.production)
  • Test: Automated testing (creates .env.test)
CLI Environment Selection
3

Configure services

The tool organizes variables into categories:

  1. Database - PostgreSQL connection
  2. Authentication - Better Auth settings
  3. Email - Resend integration
  4. Storage - Supabase file storage
  5. Stripe - Payment processing
  6. Pages & Features - Enable/disable features
  7. OAuth - Social authentication providers
CLI Stripe Configuration

CLI Features

Smart Defaults:

  • Auto-generates secure secrets
  • Provides development URLs
  • Suggests sensible configuration

Interactive Selection:

CLI Checkout Type Selection

Multi-Select Options:

CLI Pages Configuration

Validation:

  • URL format validation
  • Email format checking
  • Required field enforcement

Method 2: Manual Configuration

⚙️ Manual Setup: For advanced users who prefer full control over configuration.

1

Copy template

bash
cp .env.example .env.local
2

Edit configuration

Open .env.local and configure your services:

bash
code .env.local  # VS Code
nano .env.local  # Terminal editor

Required Variables

Database Configuration:

bash
# PostgreSQL connection (Required)
DATABASE_URL="postgresql://username:password@host:5432/database"
DIRECT_URL="postgresql://username:password@host:5432/database"

Authentication:

bash
# Better Auth secret (Required) - 32+ characters
BETTER_AUTH_SECRET="your-super-secret-key-min-32-characters"
BETTER_AUTH_URL="http://localhost:3000"

Email Service:

bash
# Resend API key (Required for email)
RESEND_API_KEY="re_xxxxxxxxxxxx"
EMAIL_FROM="noreply@yourdomain.com"

Payment Configuration (Stripe)

bash
# Stripe keys (Required for payments)
STRIPE_PUBLISHABLE_KEY="pk_test_..."
STRIPE_SECRET_KEY="sk_test_..."
STRIPE_WEBHOOK_SECRET="whsec_..."

Get Stripe keys:

  1. Create account at stripe.com
  2. Go to Developers → API keys
  3. Copy Publishable and Secret keys
  4. Setup webhook endpoint for real-time updates

Storage Configuration (Supabase)

bash
# File storage (Optional)
SUPABASE_URL="https://xxx.supabase.co"
SUPABASE_ANON_KEY="your-anon-key"
SUPABASE_BUCKET="your-bucket-name"
STORAGE_TYPE="supabase"

Social Authentication (Optional)

Google OAuth:

bash
GOOGLE_CLIENT_ID="your-google-client-id"
GOOGLE_CLIENT_SECRET="your-google-client-secret"

GitHub OAuth:

bash
GITHUB_CLIENT_ID="your-github-client-id"
GITHUB_CLIENT_SECRET="your-github-client-secret"

Environment Types

Development Environment

File: .env.development or .env.local

bash
# Development-specific settings
BETTER_AUTH_URL="http://localhost:3000"
NEXT_PUBLIC_APP_URL="http://localhost:3000"
LOG_LEVEL="debug"
NODE_ENV="development"

Production Environment

File: .env.production

bash
# Production settings with HTTPS
BETTER_AUTH_URL="https://yourdomain.com"
NEXT_PUBLIC_APP_URL="https://yourdomain.com"
LOG_LEVEL="info"
NODE_ENV="production"

Production Checklist:

  • ✅ Use HTTPS URLs
  • ✅ Use production Stripe keys
  • ✅ Use production database
  • ✅ Set proper CORS origins
  • ✅ Use strong secrets

Test Environment

File: .env.test

bash
# Test database and mock services
DATABASE_URL="postgresql://localhost:5432/test_db"
BETTER_AUTH_SECRET="test-secret-32-characters-long"
STRIPE_SECRET_KEY="sk_test_mock_key"
LOG_LEVEL="silent"
NODE_ENV="test"

Validation & Security

🔒 Security: Never commit .env.* files to Git. They contain sensitive credentials.

Environment Validation: The app automatically validates environment variables on startup using Zod schemas:

typescript
// Variables are validated against these schemas
serverSchema: z.object({
  DATABASE_URL: z.string().url(),
  BETTER_AUTH_SECRET: z.string().min(32),
  STRIPE_SECRET_KEY: z.string().startsWith('sk_'),
  // ... more validation rules
})

Best Practices:

  • Use different secrets for each environment
  • Rotate secrets regularly
  • Use test keys in development
  • Validate URLs are properly formatted
  • Keep .env.example updated for team members

Troubleshooting

Common Issues:

bash
# Invalid database URL
Error: Invalid DATABASE_URL format
Solution: Ensure URL follows postgresql://user:pass@host:port/db

# Missing required variables
Error: BETTER_AUTH_SECRET is required
Solution: Generate with: openssl rand -base64 32

# Webhook validation failed
Error: Stripe webhook signature invalid
Solution: Check STRIPE_WEBHOOK_SECRET matches your webhook endpoint

CLI Tool Issues:

bash
# CLI won't start
pnpm install && pnpm init:env

# Variables not detected
# Check env-schemas.ts for proper exports
# Restart CLI after schema changes

Next Steps

✅ Environment configured! Continue with database setup to complete your development environment.

After configuring your environment:

  1. Database Setup - Initialize your database
  2. Authentication - Configure auth providers
  3. Stripe Integration - Setup payment processing

💡 Pro tip: Use the CLI tool's multi-environment support to easily manage development, staging, and production configurations.