---
title: ".env.example as schema"
description: "Declare runtime config in an annotated .env.example, build without secrets, and check the NUXT_* variables when the server starts."
canonical_url: "https://nuxt-safe-runtime-config.onmax.me/guide/env-schema"
---
# .env.example as schema

> Declare runtime config in an annotated .env.example, build without secrets, and check the NUXT_* variables when the server starts.

<warning>

This schema source is experimental. Its syntax and behavior can change in a minor release.

</warning>

Most apps already commit a `.env.example` that lists every variable. With `envSchema`, that file is the schema. Add a type to each entry and you no longer need a `runtimeConfig` block or a schema library.

Use it when every value comes from the deployment and the build must run without secrets. Stay with [`$schema`](https://nuxt-safe-runtime-config.onmax.me/guide/installation) for transforms, unions, cross-field rules or computed defaults. An app uses one source, not both.

<steps>

### Annotate the file

```dotenv [.env.example]
# Database connection injected when the server starts.
# @type=url @example="postgresql://example.test/database"
NUXT_DATABASE_URL=

# @type=string(minLength=16)
NUXT_API_SECRET=

# @public @type=boolean
NUXT_PUBLIC_REPLAY_ENABLED=false

# @public @type=number(min=0, max=1)
NUXT_PUBLIC_SAMPLE_RATE=0.1

# @public @type=enum(debug, info, warn, error)
NUXT_PUBLIC_LOG_LEVEL=info

# @optional @type=number
NUXT_REQUEST_LIMIT=
```

Every entry is required and private unless you mark it otherwise. `=false` is a real default. `KEY=` has no default, and `@example` is documentation only.

### Point the module at it

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['nuxt-safe-runtime-config'],
  safeRuntimeConfig: {
    envSchema: '.env.example',
  },
})
```

### Read typed values

```ts
const config = useSafeRuntimeConfig()
config.databaseUrl // string
config.public.replayEnabled // boolean
config.public.sampleRate // number
config.public.logLevel // 'debug' | 'info' | 'warn' | 'error'
config.requestLimit // number | undefined
```

The types come from the file during `nuxt prepare`. Your local `.env` is never read for them.

</steps>

<caution>

Read your existing `.env.example` before you turn this on. `NUXT_API_SECRET=changeme` becomes a real default that ships in the build. Move placeholders to `@example`.

</caution>

`envSchema` validates at server start and never at build. `requireAtBuild` and `validateAtRuntime: false` are errors. Keep `onError` at `'throw'` so an invalid deployment refuses to start.

The [env-spec reference](https://nuxt-safe-runtime-config.onmax.me/reference/env-spec) lists every decorator, type and default rule.

## Deploy one build with different values

Build with the file present and no deployment variables. Then start the server with `NUXT_*` variables:

```bash
NUXT_DATABASE_URL=postgresql://db.internal/app \
NUXT_API_SECRET=a-long-production-secret \
NUXT_PUBLIC_REPLAY_ENABLED=true \
node .output/server/index.mjs
```

The same `.output` can start again with other credentials and public settings, without a rebuild.

The file owns every config path it declares. A value at one of those paths in `runtimeConfig`, or one inherited from the build environment, doesn't become a default, so a credential that CI happens to have never ends up in the build. Defaults written in the file are bundled, so leave credentials blank there.

## Public keys

A key reaches the browser only when it starts with `NUXT_PUBLIC_` and is marked `@public`. A `NUXT_` key marked `@public` stays private, because the annotation can't override Nuxt's prefix rule.

## Nested config

Keys become flat camelCase fields, so `NUXT_DATABASE_URL` is `databaseUrl`. To keep an existing nested shape, map keys to paths:

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  safeRuntimeConfig: {
    envSchema: {
      file: '.env.example',
      paths: {
        NUXT_PUBLIC_POSTHOG_REPLAY_SAMPLE_RATE: 'public.posthog.replaySampleRate',
      },
    },
  },
})
```

Each path must convert back to its key the way Nuxt maps `NUXT_*` variables, so `public.posthog.replaySampleRate` works and `public.replayRate` doesn't. A bad path fails with the file and line.

Editing the file during `nuxt dev` restarts Nuxt.

## Arrays and objects

Declare the item and property types, and quote JSON defaults:

```dotenv [.env.example]
# @type=array(enum(read, write))
NUXT_ROLES='["read"]'

# @type=object(enabled=boolean, retries=number(min=0), tags=array(string))
NUXT_SETTINGS='{"enabled":false,"retries":3,"tags":[]}'
```

`config.roles` is `('read' | 'write')[]`, and `config.settings` must have exactly `enabled`, `retries` and `tags`. JSON members are checked as written, so `"false"` inside the JSON stays a string and fails a `boolean` property.

## How values are converted

A string keeps its exact spelling. `NUXT_ACCOUNT_PIN=00123` declared as a string is `'00123'` after startup, even though Nitro 2 alone would parse it as `123`. On platforms that build each request's config themselves, a spelling the platform already parsed can't be recovered.

A value that is present but invalid fails. It never falls back to the default.

`process.env` keeps its `string | undefined` types, and `useRuntimeConfig()` keeps the types Nuxt infers. Only `useSafeRuntimeConfig()` has the declared types.


## Sitemap

See the full [sitemap](https://nuxt-safe-runtime-config.onmax.me/sitemap.md) for all pages.
