Docs
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: Built using @t3-oss/env-nextjs, the system provides runtime validation and IDE autocompletion for environment variables.
  • Composable Design: Each package defines its own variables, which are then combined into a single env.ts file in the root directory.
  • Server/Client Separation: Variables are categorized into server (private) and client (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:

cp apps/dashboard/.env.example apps/dashboard/.env
cp apps/marketing/.env.example apps/marketing/.env
cp packages/database/.env.example packages/database/.env

There are three different files. The first two are for the applications and the third one is for Prisma 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:

import { createEnv } from '@t3-oss/env-nextjs';
 
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 routes } from '@workspace/routes/keys';
 
export const env = createEnv({
  extends: [auth(), billing(), database(), email(), routes()],
  server: {},
  client: {},
  runtimeEnv: {}
});

This makes environment variables composable.

Adding new variables

To add a new environment variable:

  1. Update Environment Files: Add the variable to all relevant .env files across your project.
  2. Define Validation: Update the corresponding keys.ts file in the relevant package to validate the new variable.