LogoNEXTDEVKIT Docs

Environment Variables Reference

A comprehensive guide to understanding, configuring, and managing all environment variables in NEXTDEVKIT across different deployment platforms.

This guide provides a complete reference for all environment variables used in NEXTDEVKIT. You'll learn what each variable does, when you need it, and how to obtain the required values.

Understanding Environment Variables

Environment variables are configuration values that change based on where your application runs (development, staging, production). They keep sensitive information like API keys and database credentials out of your code.

Security Principles

Security First

  • Never commit .env files to version control
  • Use different values for development and production
  • Rotate secrets regularly (every 90 days recommended)
  • Use environment-specific OAuth callbacks

File Organization

your-project/
├── .env.local              # Local development (git-ignored)
├── .env.production         # Production values (git-ignored)
└── .env.example            # Template with dummy values (committed)

Progressive Configuration Journey

Let's configure your environment variables step by step, starting with the absolute essentials and progressively adding features.

Level 1: Minimal Setup (5 minutes)

Goal: Get NEXTDEVKIT running locally with core features.

What You'll Need:

  • Authentication secret
  • Database connection
  • Public URL

Generate Authentication Secret

Terminal
openssl rand -base64 32

This generates a random 32-character secret key for encrypting sessions.

Add to .env.local:

.env.local
# Core Authentication
BETTER_AUTH_SECRET=your-generated-secret-here
BETTER_AUTH_URL=http://localhost:3000
NEXT_PUBLIC_APP_URL=http://localhost:3000

Why these are required:

  • BETTER_AUTH_SECRET: Encrypts user sessions and tokens
  • BETTER_AUTH_URL: Base URL for authentication callbacks
  • NEXT_PUBLIC_APP_URL: Public-facing application URL (exposed to browser)

Variables prefixed with NEXT_PUBLIC_ are exposed to the browser. Never put secrets in them!

Configure Database Connection

For PostgreSQL (Recommended for production):

.env.local
# Database
DATABASE_URL=postgresql://username:password@host:5432/database?sslmode=require

Getting your database URL:

  1. Local PostgreSQL:

    postgresql://postgres:password@localhost:5432/nextdevkit
  2. Neon (Free tier available):

    • Visit neon.tech
    • Create project → Copy connection string
    • Use the pooled connection string
  3. Supabase:

    • Visit supabase.com
    • Project Settings → Database → Connection String
    • Use "Connection Pooling" string

Why you need this: Database stores user accounts, subscriptions, and application data.

See detailed database setup: Database Configuration

Run Database Migrations

Terminal
# Generate migration files
pnpm db:generate

# Apply migrations to your database
pnpm db:migrate

What this does: Creates necessary tables for authentication, users, subscriptions, etc.

🎉 You can now run: pnpm dev

Your app is running with authentication and database! But you'll need more configuration to enable all features.


Level 2: Email Integration (10 minutes)

Goal: Enable password resets, email verification, and newsletter.

What You'll Need:

  • Email service provider API key

Choose Email Provider

NEXTDEVKIT supports Resend by default (recommended for ease of use).

Why Resend:

  • Free tier: 3,000 emails/month
  • No credit card required
  • Simple setup
  • Good deliverability

Alternatives:

  • SendGrid
  • AWS SES
  • Postmark

Get Resend API Key

  1. Visit resend.com and sign up
  2. Go to API KeysCreate API Key
  3. Name it (e.g., "NEXTDEVKIT Development")
  4. Copy the key (starts with re_)

Create Audience for Newsletter:

  1. Go to AudiencesCreate Audience
  2. Name it (e.g., "NEXTDEVKIT Newsletter")
  3. Copy the Audience ID

Add Email Configuration

.env.local
# Email Service
RESEND_API_KEY=re_your_api_key_here
RESEND_AUDIENCE_ID=your-audience-id-here

What these enable:

  • Password reset emails
  • Email verification
  • Newsletter subscriptions
  • Contact form submissions

📧 Features now available:

  • Users can reset passwords
  • Newsletter signup works
  • Contact forms send emails

See email templates: Email Configuration


Level 3: Social Authentication (15 minutes)

Goal: Enable "Sign in with Google/GitHub" buttons.

Why add this:

  • Improves conversion (easier signup)
  • Reduces password fatigue
  • Better user experience

Configure GitHub OAuth

1. Create GitHub OAuth App:

2. Fill in details:

Application name: NEXTDEVKIT (Development)
Homepage URL: http://localhost:3000
Authorization callback URL: http://localhost:3000/api/auth/callback/github

3. Get credentials:

  • Click Generate a new client secret
  • Copy Client ID and Client Secret

4. Add to environment:

.env.local
# GitHub OAuth
GITHUB_CLIENT_ID=Ov23xxxxxxxxxxxxx
GITHUB_CLIENT_SECRET=your-github-client-secret

Create separate OAuth apps for development, staging, and production with different callback URLs!

Configure Google OAuth

1. Create Google Cloud Project:

2. Enable Google+ API:

  • Go to APIs & ServicesLibrary
  • Search "Google+ API" → Enable

3. Create OAuth Credentials:

  • Go to APIs & ServicesCredentials
  • Click Create CredentialsOAuth client ID
  • Application type: Web application

4. Configure OAuth consent screen:

  • User Type: External
  • Add required information (app name, support email)

5. Add authorized origins and redirect URIs:

Authorized JavaScript origins:
http://localhost:3000

Authorized redirect URIs:
http://localhost:3000/api/auth/callback/google

6. Get credentials:

.env.local
# Google OAuth
GOOGLE_CLIENT_ID=your-client-id.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=GOCSPX-your-client-secret

🔐 Features now available:

  • Social login buttons appear
  • One-click sign up with Google/GitHub
  • Automatic profile picture from OAuth provider

See authentication setup: Authentication


Level 4: Payment Integration (20 minutes)

Goal: Enable subscription billing and one-time purchases.

What You'll Need:

  • Stripe account (or Creem)
  • Product and price IDs

Choose Payment Provider

Stripe (Recommended):

  • Industry standard
  • Excellent documentation
  • Supports most countries
  • Advanced features (trials, metering, etc.)

Creem:

  • Merchant of Record (handles tax compliance)
  • Good for global sales
  • Simpler tax handling

NEXTDEVKIT supports both providers. Choose based on your needs.

Configure Stripe

1. Create Stripe Account:

  • Visit stripe.com
  • Sign up and verify your account

2. Get API Keys:

  • Go to DevelopersAPI keys
  • Copy Secret key (starts with sk_test_ for testing)

3. Create Products:

Create three products in Stripe Dashboard:

Product 1: Pro Monthly

  • Name: "Pro Plan - Monthly"
  • Billing: Recurring
  • Interval: Monthly
  • Price: $9.99/month
  • Trial: 7 days (optional)

Product 2: Pro Yearly

  • Name: "Pro Plan - Yearly"
  • Billing: Recurring
  • Interval: Yearly
  • Price: $99/year
  • Trial: 30 days (optional)

Product 3: Lifetime

  • Name: "Lifetime Access"
  • Billing: One-time
  • Price: $399

4. Copy Price IDs:

Each product has a Price ID (starts with price_). Copy these for your .env.local:

.env.local
# Stripe Configuration
STRIPE_SECRET_KEY=sk_test_your_stripe_secret_key

# Subscription Price IDs
NEXT_PUBLIC_PRICE_ID_PRO_MONTHLY=price_xxxxxxxxxxxxx
NEXT_PUBLIC_PRICE_ID_PRO_YEARLY=price_xxxxxxxxxxxxx
NEXT_PUBLIC_PRICE_ID_LIFETIME=price_xxxxxxxxxxxxx

Configure Webhooks

Webhooks notify your app when payments succeed or fail.

1. Install Stripe CLI (for local testing):

Terminal
# macOS
brew install stripe/stripe-cli/stripe

# Windows
scoop bucket add stripe https://github.com/stripe/scoop-stripe-cli.git
scoop install stripe

# Linux
# Download from https://github.com/stripe/stripe-cli/releases/latest

2. Login to Stripe:

stripe login

3. Forward webhooks to local server:

stripe listen --forward-to localhost:3000/api/webhooks/stripe

This command outputs a webhook secret (starts with whsec_):

.env.local
STRIPE_WEBHOOK_SECRET=whsec_your_local_webhook_secret

4. For Production:

  • Go to DevelopersWebhooks in Stripe Dashboard
  • Click Add endpoint
  • URL: https://yourdomain.com/api/webhooks/stripe
  • Select events:
    • checkout.session.completed
    • customer.subscription.created
    • customer.subscription.updated
    • customer.subscription.deleted
    • invoice.payment_succeeded
    • invoice.payment_failed

Important

Use different webhook secrets for development and production environments!

Alternative: Configure Creem

If you prefer Creem as your payment provider:

1. Sign up at creem.io

2. Get API credentials:

.env.local
# Creem Configuration
CREEM_API_KEY=creem_test_your_api_key
CREEM_WEBHOOK_SECRET=whsec_your_webhook_secret

# Product IDs (create products in Creem dashboard)
NEXT_PUBLIC_PRICE_ID_PRO_MONTHLY=prod_your_monthly_id
NEXT_PUBLIC_PRICE_ID_PRO_YEARLY=prod_your_yearly_id
NEXT_PUBLIC_PRICE_ID_LIFETIME=prod_your_lifetime_id

3. Update payment provider in config:

src/config/index.ts
payment: {
  provider: "creem",  // Change from "stripe"
  // ... rest of config
}

💳 Features now available:

  • Pricing page works
  • Users can subscribe
  • Payment webhooks update user subscriptions
  • Billing portal for managing subscriptions

See payment setup: Payment Configuration


Level 5: File Storage (15 minutes)

Goal: Enable file uploads (user avatars, document uploads).

What You'll Need:

  • S3-compatible storage provider (AWS S3, Cloudflare R2, etc.)

Choose Storage Provider

Cloudflare R2 (Recommended for most users):

  • Free tier: 10GB storage, 1M reads/month
  • No egress fees
  • S3-compatible API
  • Fast global CDN

AWS S3:

  • Industry standard
  • Pay-as-you-go pricing
  • Extensive features
  • Global availability

Alternatives:

  • DigitalOcean Spaces
  • Backblaze B2
  • Wasabi

Configure Cloudflare R2

1. Create R2 Bucket:

2. Get API Tokens:

  • Go to R2Manage R2 API Tokens
  • Create API Token with read/write permissions
  • Copy Access Key ID and Secret Access Key

3. Get Account ID:

  • Found in your R2 dashboard URL or settings

4. Add to environment:

.env.local
# Storage Configuration
NEXT_PUBLIC_AVATARS_BUCKET_NAME=nextdevkit-avatars

# Cloudflare R2
STORAGE_REGION=auto
STORAGE_ACCESS_KEY_ID=your-access-key-id
STORAGE_SECRET_ACCESS_KEY=your-secret-access-key
STORAGE_ENDPOINT=https://your-account-id.r2.cloudflarestorage.com

For Cloudflare Workers deployment, add STORAGE_ACCOUNT_ID as well.

Alternative: Configure AWS S3

1. Create S3 Bucket:

  • Go to AWS Console
  • Create bucket in your preferred region

2. Create IAM User:

  • Go to IAMUsersCreate user
  • Attach policy: AmazonS3FullAccess (or create custom policy)
  • Create access key → Copy credentials

3. Add to environment:

.env.local
# Storage Configuration
NEXT_PUBLIC_AVATARS_BUCKET_NAME=nextdevkit-avatars

# AWS S3
STORAGE_REGION=us-east-1
STORAGE_ACCESS_KEY_ID=AKIA...
STORAGE_SECRET_ACCESS_KEY=your-secret-key
# STORAGE_ENDPOINT not needed for AWS S3

📁 Features now available:

  • Users can upload avatars
  • File upload components work
  • Secure signed URLs for private files

See storage setup: Storage Configuration


Level 6: Analytics (10 minutes)

Goal: Track user behavior and site performance.

What You'll Need:

  • Analytics service account (Google Analytics, Umami, or Plausible)

Option 1: Google Analytics

Free, feature-rich, widely used

1. Create GA4 Property:

2. Get Measurement ID:

  • Go to AdminData Streams → Select your stream
  • Copy Measurement ID (starts with G-)

3. Add to environment:

.env.local
# Google Analytics
NEXT_PUBLIC_GOOGLE_ANALYTICS_ID=G-XXXXXXXXXX

Automatically enabled when this variable is set.

Option 2: Umami Analytics

Privacy-friendly, self-hostable, GDPR compliant

1. Choose hosting:

2. Create website:

  • Add new website in dashboard
  • Copy Website ID and Script URL

3. Add to environment:

.env.local
# Umami Analytics
NEXT_PUBLIC_UMAMI_WEBSITE_ID=your-website-id
NEXT_PUBLIC_UMAMI_SCRIPT_URL=https://your-umami-instance.com/script.js

Option 3: Plausible Analytics

Privacy-focused, simple, no cookies

1. Sign up:

2. Get configuration:

.env.local
# Plausible Analytics
NEXT_PUBLIC_PLAUSIBLE_DOMAIN=yourdomain.com
NEXT_PUBLIC_PLAUSIBLE_SCRIPT_URL=https://plausible.io/js/script.js

📊 Features now available:

  • Page view tracking
  • Event tracking
  • User journey analysis
  • Performance monitoring

See analytics setup: Analytics Configuration


Level 7: AI Integration (5 minutes)

Goal: Enable AI features (chatbots, content generation).

What You'll Need:

  • Google AI API key

Get Google AI API Key

1. Visit Google AI Studio:

2. Create API Key:

  • Click Get API key
  • Create in new or existing project
  • Copy the key (starts with AIza)

3. Add to environment:

.env.local
# AI Configuration
GOOGLE_GENERATIVE_AI_API_KEY=AIzaXXXXXXXXXXXXXXXXXXXXXXXX

Google AI has a generous free tier. Monitor usage in the AI Studio.

🤖 Features now available:

  • AI chat interface
  • Content generation
  • Smart suggestions

See AI setup: AI Integration


Level 8: Credits System (10 minutes)

Goal: Enable usage-based billing with credits.

What You'll Need:

  • Stripe product IDs for credit packages

Create Credit Products in Stripe

Create four credit packages in Stripe Dashboard:

1. Lite Package:

  • Name: "Credits - Lite"
  • Price: $9 one-time
  • Credits: 100

2. Standard Package:

  • Name: "Credits - Standard"
  • Price: $29 one-time
  • Credits: 300

3. Pro Package:

  • Name: "Credits - Pro"
  • Price: $79 one-time
  • Credits: 1,000

4. Max Package:

  • Name: "Credits - Max"
  • Price: $199 one-time
  • Credits: 3,000

Add Credit Price IDs

.env.local
# Credits System
CREDIT_LITE_PRICE_ID=price_xxxxxxxxxxxxx
CREDIT_STANDARD_PRICE_ID=price_xxxxxxxxxxxxx
CREDIT_PRO_PRICE_ID=price_xxxxxxxxxxxxx
CREDIT_MAX_PRICE_ID=price_xxxxxxxxxxxxx

🎯 Features now available:

  • Usage-based billing
  • Credit purchase flow
  • Credit balance tracking

See credits setup: Credits System


Level 9: Scheduled Tasks (5 minutes)

Goal: Secure cron job endpoints.

What You'll Need:

  • Random secret for cron authentication

Generate Cron Secret

Terminal
openssl rand -base64 16

Add to environment:

.env.local
# Cron Job Security
CRON_SECRET=your-generated-secret

How it's used: Cron endpoints check this secret to prevent unauthorized access:

// Example: /api/cron/cleanup
if (request.headers.get('Authorization') !== `Bearer ${process.env.CRON_SECRET}`) {
  return new Response('Unauthorized', { status: 401 });
}

⏰ Features secured:

  • Data cleanup jobs
  • Report generation
  • Scheduled notifications

Platform-Specific Configurations

Different deployment platforms require slightly different environment variable setups.

Next.js / Vercel Deployment

Best for: Traditional hosting, Vercel deployments, Docker containers

All variables from Levels 1-9 work as-is. No special configuration needed.

Deploy to Vercel:

  1. Import project from Git
  2. Add environment variables in SettingsEnvironment Variables
  3. Deploy

See detailed guide: Next.js Deployment


Cloudflare Workers Deployment

Best for: Edge computing, global distribution, cost optimization

Key Differences:

  1. Database: Use Cloudflare D1 instead of PostgreSQL

    # Remove DATABASE_URL
    # D1 binding configured in wrangler.toml
  2. Storage: Add Account ID for R2

    STORAGE_ACCOUNT_ID=your-cloudflare-account-id
  3. Additional bindings configured in wrangler.toml:

    [[d1_databases]]
    binding = "DB"
    database_name = "nextdevkit"
    database_id = "your-database-id"

See detailed guide: Cloudflare Workers Deployment


AWS SST Deployment

Best for: Enterprise infrastructure, AWS integration, compliance requirements

Key Differences:

  1. AWS Credentials:

    AWS_ACCESS_KEY_ID=AKIA...
    AWS_SECRET_ACCESS_KEY=your-secret-key
  2. RDS Database: Use AWS RDS PostgreSQL

    DATABASE_URL=postgresql://username:password@your-rds-instance.region.rds.amazonaws.com:5432/database
  3. S3 Storage: No endpoint needed

    STORAGE_REGION=us-east-1
    # STORAGE_ENDPOINT not used
  4. Infrastructure as Code: Environment variables can be defined in sst.config.ts

See detailed guide: SST AWS Deployment


Environment Variables Quick Reference

Required Variables (Minimum Setup)

VariablePurposeExample Value
BETTER_AUTH_SECRETSession encryptionopenssl rand -base64 32
BETTER_AUTH_URLAuth callback basehttp://localhost:3000
NEXT_PUBLIC_APP_URLPublic app URLhttp://localhost:3000
DATABASE_URLDatabase connectionpostgresql://...

Authentication

VariableRequiredPurposeGet From
GITHUB_CLIENT_IDFor GitHub loginOAuth app IDGitHub Settings
GITHUB_CLIENT_SECRETFor GitHub loginOAuth secretGitHub OAuth App
GOOGLE_CLIENT_IDFor Google loginOAuth app IDGoogle Cloud Console
GOOGLE_CLIENT_SECRETFor Google loginOAuth secretGoogle Cloud Console

Email

VariableRequiredPurposeGet From
RESEND_API_KEYFor email sendingAPI authenticationresend.com
RESEND_AUDIENCE_IDFor newsletterMailing list IDResend Dashboard

Payment

VariableRequiredPurposeGet From
STRIPE_SECRET_KEYFor paymentsAPI authenticationStripe Dashboard
STRIPE_WEBHOOK_SECRETFor webhooksWebhook verificationStripe Webhooks settings
NEXT_PUBLIC_PRICE_ID_PRO_MONTHLYSubscriptionProduct price IDStripe Products
NEXT_PUBLIC_PRICE_ID_PRO_YEARLYSubscriptionProduct price IDStripe Products
NEXT_PUBLIC_PRICE_ID_LIFETIMEOne-timeProduct price IDStripe Products
CREEM_API_KEYAlternative paymentAPI authenticationcreem.io
CREEM_WEBHOOK_SECRETAlternative paymentWebhook verificationCreem Dashboard

Storage

VariableRequiredPurposeGet From
NEXT_PUBLIC_AVATARS_BUCKET_NAMEFile uploadsBucket identifierYour storage provider
STORAGE_REGIONFile uploadsStorage regionR2: auto, S3: us-east-1
STORAGE_ACCESS_KEY_IDFile uploadsAPI authenticationR2/S3 API Keys
STORAGE_SECRET_ACCESS_KEYFile uploadsAPI secretR2/S3 API Keys
STORAGE_ENDPOINTR2 onlyR2 endpoint URLCloudflare R2 Dashboard
STORAGE_ACCOUNT_IDR2 WorkersAccount identifierCloudflare Dashboard

Analytics

VariableRequiredPurposeGet From
NEXT_PUBLIC_GOOGLE_ANALYTICS_IDOptionalGA4 trackingGoogle Analytics
NEXT_PUBLIC_UMAMI_WEBSITE_IDOptionalUmami trackingUmami Dashboard
NEXT_PUBLIC_UMAMI_SCRIPT_URLOptionalUmami scriptUmami instance URL
NEXT_PUBLIC_PLAUSIBLE_DOMAINOptionalPlausible trackingYour domain
NEXT_PUBLIC_PLAUSIBLE_SCRIPT_URLOptionalPlausible scripthttps://plausible.io/js/script.js

AI & Credits

VariableRequiredPurposeGet From
GOOGLE_GENERATIVE_AI_API_KEYFor AI featuresAPI authenticationGoogle AI Studio
CREDIT_LITE_PRICE_IDCredits systemProduct price IDStripe Products
CREDIT_STANDARD_PRICE_IDCredits systemProduct price IDStripe Products
CREDIT_PRO_PRICE_IDCredits systemProduct price IDStripe Products
CREDIT_MAX_PRICE_IDCredits systemProduct price IDStripe Products

Security

VariableRequiredPurposeGet From
CRON_SECRETCron jobsEndpoint protectionopenssl rand -base64 16
AWS_ACCESS_KEY_IDAWS deploymentAWS authenticationAWS IAM
AWS_SECRET_ACCESS_KEYAWS deploymentAWS secretAWS IAM

Troubleshooting

Common Issues

❌ "Missing BETTER_AUTH_SECRET"

# Generate a new secret
openssl rand -base64 32
# Add to .env.local

❌ "Database connection failed"

  • Check DATABASE_URL format
  • Verify database is running
  • Check network connectivity
  • Ensure sslmode=require for cloud databases

❌ "Stripe webhook signature verification failed"

  • Development: Use stripe listen webhook secret
  • Production: Use Stripe Dashboard webhook secret
  • Ensure STRIPE_WEBHOOK_SECRET matches your environment

❌ "OAuth callback URL mismatch"

  • Verify BETTER_AUTH_URL matches OAuth app settings
  • Check callback URL: {BETTER_AUTH_URL}/api/auth/callback/{provider}
  • Use different OAuth apps for dev/staging/production

❌ "Storage upload failed"

  • Verify bucket name and permissions
  • Check STORAGE_ACCESS_KEY_ID and secret
  • For R2: Ensure STORAGE_ENDPOINT is correct
  • For S3: Verify region

Environment-Specific Debugging

Check loaded variables:

// Add to any server component temporarily
console.log('Loaded env:', {
  hasAuthSecret: !!process.env.BETTER_AUTH_SECRET,
  hasDatabaseUrl: !!process.env.DATABASE_URL,
  // Never log actual values!
});

Verify public variables in browser:

// Browser console
console.log('Public URL:', process.env.NEXT_PUBLIC_APP_URL);

Best Practices

Development Workflow

  1. Use .env.local for local development

    • Git-ignored by default
    • Overrides other .env files
  2. Keep .env.example updated

    • Use dummy values
    • Document all required variables
    • Commit to repository
  3. Separate environments

    .env.local          → Development
    .env.production     → Production values

Security Checklist

  • All secrets use strong random values
  • No secrets committed to Git
  • Different OAuth apps per environment
  • Webhook secrets match environment
  • API keys have minimum required permissions
  • Production uses SSL/TLS (https://)
  • Rotate secrets every 90 days

Production Deployment

  1. Verify all required variables are set
  2. Use production API keys (not test keys)
  3. Update OAuth callback URLs
  4. Configure production webhook endpoints
  5. Set BETTER_AUTH_URL to production domain
  6. Enable SSL for database connections

Migration Guide

Moving from Development to Production

Update Authentication URLs

# Development
BETTER_AUTH_URL=http://localhost:3000

# Production
BETTER_AUTH_URL=https://yourdomain.com

Switch to Production Keys

  • Stripe: sk_live_... instead of sk_test_...
  • OAuth apps: Create production apps
  • Database: Use production instance
  • Email: Remove test mode

Update OAuth Callbacks

Register production callback URLs:

https://yourdomain.com/api/auth/callback/github
https://yourdomain.com/api/auth/callback/google

Configure Production Webhooks

  • Stripe: https://yourdomain.com/api/webhooks/stripe
  • Use production webhook secrets

Next Steps

Now that you understand environment variables:

  1. Configure your deployment platform: Deployment Guide
  2. Set up your database: Database Setup
  3. Configure authentication: Authentication Guide
  4. Enable payments: Payment Integration

Questions? Check our Documentation or Contact Support