Environment variable bugs are some of the hardest to catch — a missingDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/rijvi-mahmud/shaddy/llms.txt
Use this file to discover all available pages before exploring further.
DATABASE_URL or a malformed API endpoint silently breaks at runtime, often only in production. createEnv validates your environment against a Zod schema the moment your module loads, surfacing problems immediately with a clear error message rather than a cryptic runtime failure.
Installation
How It Works
createEnv accepts a Zod schema, the runtime values to validate, and an optional client prefix. On the server, all variables are validated. On the client (wherever window is defined), only variables whose key starts with clientEnvPrefix are validated and returned — server-only secrets are never exposed to the browser.
If validation fails, a descriptive error is logged to the console and an exception is thrown, stopping your app before it can run with invalid configuration.
Signature
Types
Parameters
A Zod schema (typically
z.object({...})) that describes the shape and validation rules for your environment variables.The actual runtime values to validate, e.g.
{ MY_VAR: process.env.MY_VAR }. The type must match the inferred type of schemas.The prefix used to identify client-safe variables. On the client, only keys starting with this prefix are validated and returned. Defaults to
"NEXT_PUBLIC_" for Next.js projects.Examples
Basic usage
Define a schema once in a dedicatedenv.ts file and import env wherever you need a validated value:
Separating server and client variables
Custom client prefix
If you’re not using Next.js, change the prefix to match your framework’s convention:Consuming validated env values
env is fully typed via z.infer<T>, your editor will autocomplete variable names and flag typos at compile time.
Call
createEnv at module load time — not inside a function or component — so validation errors surface immediately when your server starts or when the browser bundle executes, rather than on the first request that happens to use that variable.Benefits
- Type-safe — Zod inference gives you a fully typed
envobject with no manual type declarations. - Secure — client-prefix filtering ensures server secrets never reach the browser.
- Fail-fast — validation errors throw at startup, not at request time.
- Helpful errors — logs the exact keys and Zod error details so you know exactly what to fix.
- Flexible — works in Next.js, Vite, Remix, or any TypeScript project with a configurable
clientEnvPrefix.