Skip to Content
Zod Config

Env adapter

Load configuration from process.env or a custom object.

Loads the configuration from process.env or a custom object. Use regex to keep only the keys you need, which helps avoid conflicts when you combine several adapters. To build nested objects from flat keys, set nestingSeparator.

Install

No extra dependency. The env adapter is synchronous, so it also works with loadConfigSync.

Usage

import {  } from 'zod';
import {  } from 'zod-config';
import {  } from 'zod-config/env-adapter';

const  = .({
  : .().(/^\d+$/),
  : .(),
});

// using default env (process.env)
const  = await ({
  : ,
  : (),
});

// using custom env + filter regex to match only the keys we need
const  = await ({
  : ,
  : ({
    : /^MY_APP_/,
    : {
      : '3000',
      : 'localhost',
      : 'ignored',
    },
  }),
});

// using nesting separator to create nested objects
const  = await ({
  : .({
    : .({
      : .(),
      : .(),
    }),
  }),
  : ({
    : {
      'database.host': 'localhost',
      'database.port': '5432',
    },
    : '.',
  }),
});

Options

OptionTypeDescription
customEnvRecord<string, any>Object to read instead of process.env.
nestingSeparatorstringSeparator used to build nested objects from flat keys, e.g. . or _. Only top-level keys are split.

Also accepts the shared adapter options: regex, silent, keyMatching and transform.

For apps with a browser bundle (Next.js, Vite, Astro, TanStack Start), use type-safe environment variables instead: they separate server and client variables.

On this page