This guide walks you through installingDocumentation Index
Fetch the complete documentation index at: https://next-safe-env.dev/llms.txt
Use this file to discover all available pages before exploring further.
next-safe-env, writing your first environment schema, importing typed values in your app, and understanding what happens when a variable is missing or invalid. The whole setup takes less than five minutes.
Create src/env.ts
Create a single file that defines your entire environment schema. Split your variables into The available validators are:
All validators are chainable. Common modifiers:
server (never exposed to the browser) and client (safe to use anywhere). Every key in runtimeEnv must reference its corresponding process.env value individually - do not spread the whole process.env object, as bundlers need individual references to inline values statically and keep server vars out of the client bundle.src/env.ts
| Validator | Output type | Notes |
|---|---|---|
str() | string | Raw string, no coercion |
num() | number | Coerces "3000" → 3000 |
bool() | boolean | Coerces "true"/"1"/"yes"/"on" → true |
url() | string | Shorthand for str().url() |
port() | number | Shorthand for num().port() - integer 1–65535 |
Use env in your app
Import the
env object anywhere in your project. Server vars and client vars are all available on the same object, with types fully inferred from your schema.app/api/auth/route.ts
app/components/Header.tsx
What happens on failure
If any variable is missing or invalid, Fix the values in your
next-safe-env refuses to start the app and prints every problem at once - no hunting for the first undefined at runtime..env file or deployment environment and restart. The error lists every failing field so you can fix them all in one pass.next-safe-env validates what is already in process.env. It does not load .env files on its own. Use Next.js built-in .env support or dotenv to load your .env files first, then next-safe-env will validate and type whatever ends up in process.env.Next steps
Next.js guide
Set up server/client splitting,
NEXT_PUBLIC_ prefix enforcement, and the server-only module guard.Node.js guide
Use next-safe-env in plain Node.js servers, APIs, and CLI scripts.

