Recipes

Learn how to parse different types of environment variables in different runtimes.

envin leverages the full power of all supported schema validators, allowing you to use transforms, default values, and other advanced features to create powerful and flexible validation schemas for your environment variables. Below are several example recipes for common use cases.

All environment variables are strings, so ensure that the first validator is for strings (e.g. for zod is z.string()). This will be enforced at the type level in the future.

Booleans

Coercing booleans from strings is a common use case. Below are two examples of how to accomplish this, though you can implement any coercion logic that suits your needs.

Default primitive coercion should not be used for booleans, since every non-empty string gets coerced to true.

env.config.ts
import {  } from "envin";
import {  } from "zod/v4";

const env = ({
const env: {
    readonly COERCED_BOOLEAN: boolean;
    readonly NATIVE_COERCION: boolean;
    readonly ONLY_BOOLEAN: boolean;
    readonly STRING_BOOLEAN: boolean;
    readonly _schema: FinalSchema<{}, {
        COERCED_BOOLEAN: z.ZodPipe<z.ZodString, z.ZodTransform<...>>;
        NATIVE_COERCION: z.ZodCoercedBoolean<...>;
        ONLY_BOOLEAN: z.ZodPipe<...>;
        STRING_BOOLEAN: z.ZodPipe<...>;
    }, {}, []>;
}
: { : .() // Transform to boolean using preferred coercion logic .( => !== "false" && !== "0"), // Alternatively, use Zod's built-in primitive coercion // https://zod.dev/?id=coercion-for-primitives : ..(), : .() // Only allow "true" or "false" .( => === "true" || === "false") // Transform to boolean .( => === "true"), // Alternatively, use Zod's new syntax for string boolean coercion // https://zod.dev/v4?id=stringbool : .(), }, // ... });

Numbers

Converting numbers from strings is another common use case that requires careful handling.

env.config.ts
import {  } from "envin";
import {  } from "zod";

const env = ({
const env: {
    readonly SOME_NUMBER: number;
    readonly ZOD_NUMBER_COERCION: number;
    readonly _schema: FinalSchema<{}, {
        SOME_NUMBER: z.ZodPipeline<z.ZodEffects<z.ZodString, number, string>, z.ZodNumber>;
        ZOD_NUMBER_COERCION: z.ZodNumber;
    }, {}, []>;
}
: { : .() // Transform to number .( => (, 10)) // Ensure transform worked correctly .(.()), // Alternatively, use Zod's built-in primitive coercion // https://zod.dev/?id=coercion-for-primitives : ..(), }, // ... });

Storybook

Storybook uses its own bundler that is unaware of envin and doesn't call env to validate environment variables. You can use Storybook's built-in support for environment variables to ensure all required variables are available in your Storybook environment:

.storybook/main.ts
import env from "~/env.config";

const config: StorybookConfig = {
  // Other Storybook configuration...
  env: config => ({
    ...config,
    ...env,
  }),
};

export default config;

On this page