When you visit T3 env website, it has this hero title “Framework agnostic validation for type-safe environment variables.” with this description — “Never build your apps with invalid environment variables again. Validate and transform your environment with the full power of Zod.”
You can use T3 env to apply type-safe validations on your environment variables so you don’t end up deploying an application with invalid env variables. but how do you apply these validations? let’s find out.
T3 env usage is simple, you would first have to define your schema as shown below:
// src/env.mjsimport { createEnv } from "@t3-oss/env-nextjs";import { z } from "zod";export const env = createEnv({ /* * Serverside Environment variables, not available on the client. * Will throw if you access these variables on the client. */ server: { DATABASE_URL: z.string().url(), OPEN_AI_API_KEY: z.string().min(1), }, /* * Environment variables available on the client (and server). * * 💡 You'll get type errors if these are not prefixed with NEXT_PUBLIC_. */ client: { NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY: z.string().min(1), }, /* * Due to how Next.js bundles environment variables on Edge and Client, * we need to manually destructure them to make sure all are included in bundle. * * 💡 You'll get type errors if not all variables from `server` & `client` are included here. */ runtimeEnv: { DATABASE_URL: process.env.DATABASE_URL, OPEN_AI_API_KEY: process.env.OPEN_AI_API_KEY, NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY: process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY, },});
Github docs for T3 env provides Next.js based example. Pay extra attention to the comments provided in the above example. Since Next.js is a full-stack framework, you have env variables for server and client and you need to be careful to not expose your server side enviroment variables to
client.
T3 env requires you to define your server env types in server object in schema.
/** Serverside Environment variables, not available on the client.* Will throw if you access these variables on the client.*/server: { DATABASE_URL: z.string().url(), OPEN_AI_API_KEY: z.string().min(1),},
Similarly, define the types for the client side environment variables
/* * Environment variables available on the client (and server). * * 💡 You'll get type errors if these are not prefixed with NEXT_PUBLIC_.*/client: { NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY: z.string().min(1),},
So far, we only defined the variables and their types on the client and server side. Next step is to define runtimeEnv.
/** Due to how Next.js bundles environment variables on Edge and Client,* we need to manually destructure them to make sure all are included in bundle.** 💡 You'll get type errors if not all variables from `server` & `client` are included here.*/runtimeEnv: { DATABASE_URL: process.env.DATABASE_URL, OPEN_AI_API_KEY: process.env.OPEN_AI_API_KEY, NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY: process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY,},
Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.