Environment Variables
Indie Starter uses @t3-oss/env-nextjs↗ along with zod↗ under the hood for validating environment variables at runtime and buildtime by providing a simple logic in src/env.js
.
env.js
If you want to add a new environment variable, you must add a validator for it in src/env.js
, and then add the KV-pair in .env.local
The boilerplate is generated by T3 and uses the createEnv
function to create the schema validate both client and server-side environment variables.
Using Environment Variables
When you want to use your environment variables, you can import them from the created env.js
and use them as you would normally do. If you import this on the client and try accessing a server-side environment variable, you will get a runtime error.
Adding Environment Variables
To ensure your build never completes without the environment variables the project needs, you will need to add new environment variables in two locations:
📄 .env.local
: Enter your environment variable like you would normally do in a .env.local
file, i.e. KEY=VALUE
📄 env.js: Add the appropriate validation logic for the environment variables by defining a Zod schema inside createEnv for each one, e.g. KEY: z.string()
. Besides that, make sure to destruct them in the runtimeEnv option, e.g.: KEY: process.env.KEY
Type Coercion
All variables you add to .env will be imported as strings, even if their value is intended to represent a different type. If you want to use your environment variables as a different type at runtime, you can use Zod’s coerce to convert the string to the type you want. It will throw if the coercion fails.
Add the variables to your .env.local
:
Then, validate them in env.js
: