Nuxt
The Nuxt package comes preconfigured for Nuxt, and fills the runtimeEnv
option automatically. All you need to do is define your schemas!
Install dependencies
Install the required dependencies:
pnpm add @t3-oss/env-nuxt zod
pnpm add @t3-oss/env-nuxt zod
@t3-oss/env-core
requires a minimum of typescript@4.7.2
.
@t3-oss/env-core
is an ESM only package. Make sure that your tsconfig uses a module resolution that can read package.json#exports
(Bundler
is recommended).
Create your schema
import { createEnv } from "@t3-oss/env-nuxt";
import { z } from "zod";
export const env = createEnv({
server: {
DATABASE_URL: z.string().url(),
OPEN_AI_API_KEY: z.string().min(1),
},
client: {
NUXT_PUBLIC_PUBLISHABLE_KEY: z.string().min(1),
},
});
import { createEnv } from "@t3-oss/env-nuxt";
import { z } from "zod";
export const env = createEnv({
server: {
DATABASE_URL: z.string().url(),
OPEN_AI_API_KEY: z.string().min(1),
},
client: {
NUXT_PUBLIC_PUBLISHABLE_KEY: z.string().min(1),
},
});
While defining both the client and server schemas in a single file provides the best developer experience, it also means that your validation schemas for the server variables will be shipped to the client. If you consider the names of your variables sensitive, you should split your schemas into two files.
import { createEnv } from "@t3-oss/env-nuxt";
import { z } from "zod";
export const env = createEnv({
server: {
DATABASE_URL: z.string().url(),
OPEN_AI_API_KEY: z.string().min(1),
},
runtimeEnv: process.env,
});
import { createEnv } from "@t3-oss/env-nuxt";
import { z } from "zod";
export const env = createEnv({
server: {
DATABASE_URL: z.string().url(),
OPEN_AI_API_KEY: z.string().min(1),
},
runtimeEnv: process.env,
});
import { createEnv } from "@t3-oss/env-nuxt";
import { z } from "zod";
export const env = createEnv({
client: {
PUBLIC_CLERK_PUBLISHABLE_KEY: z.string().min(1),
},
runtimeEnv: process.env,
});
import { createEnv } from "@t3-oss/env-nuxt";
import { z } from "zod";
export const env = createEnv({
client: {
PUBLIC_CLERK_PUBLISHABLE_KEY: z.string().min(1),
},
runtimeEnv: process.env,
});
Validate schema on build (recommended)
We recommend you importing your newly created file in your nuxt.config.ts
. This will make sure your environment variables are validated at build time which will save you a lot of time and headaches down the road.
import "./env";
export default defineNuxtConfig({
// ...
});
import "./env";
export default defineNuxtConfig({
// ...
});
Use your schema
Then, import the env
object in your application and use it, taking advantage of type-safety and auto-completion:
import { env } from "~~/env"; // On server
export default defineEventHandler(() => {
// do fancy ai stuff
const magic = await fetch("...", {
headers: { Authorization: env.OPEN_AI_API_KEY },
});
// ...
});
import { env } from "~~/env"; // On server
export default defineEventHandler(() => {
// do fancy ai stuff
const magic = await fetch("...", {
headers: { Authorization: env.OPEN_AI_API_KEY },
});
// ...
});
<script setup lang="ts">
import { env } from "~~/env"; // On client - same import!
</script>
<template>
<div>Client says {{ env.NUXT_PUBLIC_GREETING }}!</div>
<a href="/api/hello">See what the server has to say!</a>
</template>
<script setup lang="ts">
import { env } from "~~/env"; // On client - same import!
</script>
<template>
<div>Client says {{ env.NUXT_PUBLIC_GREETING }}!</div>
<a href="/api/hello">See what the server has to say!</a>
</template>