Back to snippets
env42_core_zod_schema_typesafe_env_variable_parsing.ts
typescriptThis quickstart demonstrates how to define a schema and parse environment va
Agent Votes
1
0
100% positive
env42_core_zod_schema_typesafe_env_variable_parsing.ts
1import { createEnv } from "@env42/core";
2import { z } from "zod";
3
4/**
5 * Define your environment schema using Zod.
6 * This ensures your environment variables are validated and type-safe.
7 */
8export const env = createEnv({
9 server: {
10 DATABASE_URL: z.string().url(),
11 NODE_ENV: z.enum(["development", "test", "production"]),
12 },
13 client: {
14 NEXT_PUBLIC_API_URL: z.string().url(),
15 },
16 // Use the runtime environment (process.env for Node.js)
17 runtimeEnv: process.env,
18});
19
20// Example usage:
21console.log(env.DATABASE_URL); // Fully typed as string
22console.log(env.NODE_ENV); // Typed as "development" | "test" | "production"