General
Codebase
Environment Variables
Learn how environment variables are managed in the monorepo.
The starter kit provides a robust, type-safe system for managing environment variables, ensuring consistency, security, and seamless integration across all parts of your project.
- Type Safety: The system is built using @t3-oss/env-nextjs which provides runtime validation and IDE autocompletion for environment variables.
- Composable Design: Each package defines its own variables, which are then combined into a
env.ts. - Server/Client Separation: Variables are categorized into
server(private) andclient(public) to maintain security and clarity.
Environment variable files
During the setup copy all .env.example to create .env files, which you populate with your values:
Terminal
cp apps/dashboard/.env.example apps/dashboard/.env
cp apps/marketing/.env.example apps/marketing/.env
cp apps/public-api/.env.example apps/public-api/.env
cp packages/database/.env.example packages/database/.envThere are four different files. The first three are for the applications and the third one is for drizzle scripts.
Composable keys system
Each package with secrets includes a keys.ts file, which defines rules for validating. For example the apps/dashboard/.env file looks like the following:
apps/dashboard/.env
import { createEnv } from '@t3-oss/env-nextjs';
import { keys as analytics } from '@workspace/analytics/keys';
import { keys as auth } from '@workspace/auth/keys';
import { keys as billing } from '@workspace/billing/keys';
import { keys as database } from '@workspace/database/keys';
import { keys as email } from '@workspace/email/keys';
import { keys as monitoring } from '@workspace/monitoring/keys';
import { keys as routes } from '@workspace/routes/keys';
export const env = createEnv({
extends: [
analytics(),
auth(),
billing(),
database(),
email(),
monitoring(),
routes()
],
server: {},
client: {},
runtimeEnv: {}
});This makes environment variables composable.
Adding new variables
To add a new environment variable:
- Update Environment Files: Add the variable to all relevant
.envfiles across your project. - Define Validation: Update the corresponding
keys.tsfile in the relevant package to validate the new variable.