Environment Variables
How to configure environment variables in next-forge.
next-forge uses environment variables for configuration. This guide will help you set up the required variables to get started quickly, and optionally configure additional features.
Quick Start (Minimum Setup)
To get next-forge running locally with basic functionality, you only need to configure these required variables:
1. Database (Required)
Add to packages/database/.env
:
DATABASE_URL="postgresql://user:password@localhost:5432/mydb"
For quick local development, we recommend Neon for a free PostgreSQL database. Sign up, create a project, and copy the connection string.
2. Authentication (Required)
Add to apps/app/.env.local
and apps/web/.env.local
:
# Server
CLERK_SECRET_KEY="sk_test_..."
# Client
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY="pk_test_..."
NEXT_PUBLIC_CLERK_SIGN_IN_URL="/sign-in"
NEXT_PUBLIC_CLERK_SIGN_UP_URL="/sign-up"
NEXT_PUBLIC_CLERK_AFTER_SIGN_IN_URL="/"
NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL="/"
Sign up at Clerk and create an application
Go to API Keys in your Clerk dashboard
Copy the Publishable key (starts with pk_
) and Secret key (starts with sk_
)
3. Local URLs (Pre-configured)
These are already set to sensible defaults for local development:
NEXT_PUBLIC_APP_URL="http://localhost:3000"
NEXT_PUBLIC_WEB_URL="http://localhost:3001"
NEXT_PUBLIC_API_URL="http://localhost:3002"
NEXT_PUBLIC_DOCS_URL="http://localhost:3004"
VERCEL_PROJECT_PRODUCTION_URL="http://localhost:3000"
That's it! You can now run npm run dev
and the app will work with basic authentication and database functionality.
Optional Features
The following environment variables enable additional features. You can add them as needed:
Content Management (BaseHub)
Required for the CMS functionality in packages/cms
.
BASEHUB_TOKEN="bshb_..."
Fork the next-forge template on BaseHub
Navigate to Settings → API Tokens
Copy your Read Token (starts with bshb_
)
Email (Resend)
Required for sending transactional emails.
RESEND_TOKEN="re_..."
RESEND_FROM="noreply@yourdomain.com"
Payments (Stripe)
Required for subscription and payment functionality.
STRIPE_SECRET_KEY="sk_test_..."
STRIPE_WEBHOOK_SECRET="whsec_..."
Get your keys from Stripe Dashboard
For webhooks, install the Stripe CLI and run:
stripe listen --forward-to localhost:3000/api/webhooks/stripe
Analytics
Google Analytics
NEXT_PUBLIC_GA_MEASUREMENT_ID="G-..."
PostHog
NEXT_PUBLIC_POSTHOG_KEY="phc_..."
NEXT_PUBLIC_POSTHOG_HOST="https://app.posthog.com"
Observability
Better Stack (Uptime monitoring)
BETTERSTACK_API_KEY="..."
BETTERSTACK_URL="..."
Get your API key from Better Stack
Security
Arcjet (Rate limiting & security)
ARCJET_KEY="ajkey_..."
Real-time Features
Liveblocks (Collaboration)
LIVEBLOCKS_SECRET="sk_..."
Get your secret from Liveblocks
Notifications (Knock)
KNOCK_API_KEY="..."
KNOCK_SECRET_API_KEY="..."
KNOCK_FEED_CHANNEL_ID="..."
NEXT_PUBLIC_KNOCK_API_KEY="..."
NEXT_PUBLIC_KNOCK_FEED_CHANNEL_ID="..."
Feature Flags
FLAGS_SECRET="..."
Generate a random secret string for encrypting feature flag data.
Webhooks (Svix)
SVIX_TOKEN="..."
Clerk Webhooks
CLERK_WEBHOOK_SECRET="whsec_..."
In your Clerk dashboard, go to Webhooks
Add a new endpoint pointing to https://your-domain.com/api/webhooks/clerk
Subscribe to the events you need (typically user.created
, user.updated
, etc.)
Copy the Signing Secret
Environment Variable Files
next-forge uses environment variables across multiple locations:
File | Purpose |
---|---|
apps/app/.env.local | Main application variables |
apps/web/.env.local | Marketing website variables |
apps/api/.env.local | API server variables |
packages/database/.env | Database connection string |
packages/cms/.env.local | CMS configuration |
packages/internationalization/.env.local | i18n configuration |
The setup script automatically creates these files from .env.example
templates. You only need to fill in the values.
Type Safety
Type safety is provided by @t3-oss/env-nextjs, which provides runtime validation and autocompletion for all environment variables. Each package defines its own environment variables in a keys.ts
file with Zod validation schemas.
Validation Rules
Be as specific as possible with validation. For example, if a vendor secret starts with sec_
, validate it as z.string().min(1).startsWith('sec_')
. This makes your intent clearer and helps prevent errors at runtime.
Adding a New Environment Variable
To add a new environment variable:
- Add the variable to the relevant
.env.local
files - Add validation to the
server
orclient
object in the package'skeys.ts
file
Example in packages/my-package/keys.ts
:
import { createEnv } from "@t3-oss/env-nextjs";
import { z } from "zod";
export const keys = createEnv({
server: {
MY_NEW_SECRET: z.string().min(1),
},
client: {
NEXT_PUBLIC_MY_VALUE: z.string().optional(),
},
runtimeEnv: {
MY_NEW_SECRET: process.env.MY_NEW_SECRET,
NEXT_PUBLIC_MY_VALUE: process.env.NEXT_PUBLIC_MY_VALUE,
},
});
Deployment
When deploying to Vercel or other platforms:
- Add all required environment variables to your deployment platform
- Update URL variables (
NEXT_PUBLIC_APP_URL
, etc.) to production values - Some integrations (like Sentry) automatically inject their variables via marketplace integrations
Variables prefixed with VERCEL_
are automatically available in Vercel deployments, such as VERCEL_PROJECT_PRODUCTION_URL
.