# Env

Declare your settings and secrets once, then read them as typed values in browser and server code.

Hosts: Node, Docker, Cloudflare, Vercel, Netlify, Deno.

:hero-figure{name="env"}

:::framework{name="Nuxt"}
```ts [nuxt.config.ts]
import { env } from 'vite-hub/env'

export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: {
    host: 'node',
    env: {
      // Safe to show in the browser
      public: {
        appName: env({ default: 'Acme', mode: 'build' }),
      },
      // Only server code can read these
      server: {
        github: {
          token: env({
            secret: true,
            source: env.source('GITHUB_TOKEN'),
          }),
        },
        dryRun: env.boolean({ default: true }),
      },
    },
  },
})
```
:::
:::framework{name="Nitro"}
```ts [vite.config.ts]
import { nitro } from 'nitro/vite'
import { defineConfig } from 'vite'
import { vitehub } from 'vite-hub'
import { env } from 'vite-hub/env'

export default defineConfig({
  plugins: [vitehub({
    host: 'node',
    env: {
      // Safe to show in the browser
      public: {
        appName: env({ default: 'Acme', mode: 'build' }),
      },
      // Only server code can read these
      server: {
        github: {
          token: env({
            secret: true,
            source: env.source('GITHUB_TOKEN'),
          }),
        },
        dryRun: env.boolean({ default: true }),
      },
    },
  }), nitro()],
})
```
:::
:::framework{name="Vite"}
```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { vitehub } from 'vite-hub'
import { env } from 'vite-hub/env'

export default defineConfig({
  plugins: [vitehub({
    host: 'node',
    env: {
      // Safe to show in the browser
      public: {
        appName: env({ default: 'Acme', mode: 'build' }),
      },
      // Only server code can read these
      server: {
        github: {
          token: env({
            secret: true,
            source: env.source('GITHUB_TOKEN'),
          }),
        },
        dryRun: env.boolean({ default: true }),
      },
    },
  })],
})
```
:::

```ts [src/app.ts]
import { usePublicEnv } from '#vitehub/env/public'

// Public values work in browser code
const { appName } = usePublicEnv()
document.title = appName
```

```ts [server/api/issues.get.ts]
import { defineEventHandler } from 'h3'
import { useServerEnv } from 'vite-hub/env'

export default defineEventHandler(async () => {
  const { github } = useServerEnv()

  // A secret stays hidden until you unseal it
  const token = github.token.unseal()
  const response = await fetch(
    'https://api.github.com/issues',
    { headers: { authorization: `Bearer ${token}` } },
  )
  return response.json()
})
```

```ts [server/api/publish.post.ts]
import { defineEventHandler } from 'h3'
import { useServerEnv } from 'vite-hub/env'

export default defineEventHandler(() => {
  // dryRun is a boolean, not the text "true"
  const { dryRun } = useServerEnv()
  if (dryRun) return { published: false }

  return { published: true }
})
```

### [Browser or server](https://vitehub.dev/docs/env/configure#env-config-sections)

Public values can appear in the browser. Server values stay in server code.

### [Typed values](https://vitehub.dev/docs/env/configure#typed-values)

Read host text as a boolean, a number, or one of a list of choices. A wrong value fails with a clear error.

### [Secrets stay hidden](https://vitehub.dev/docs/env/server-api#use-it-at-runtime)

A secret prints as hidden text until you call `unseal()`.

### [Clear variable names](https://vitehub.dev/docs/env/configure#variable-names)

`env.server.github.token` reads `VITEHUB_GITHUB_TOKEN` first, then the name you give in `env.source()`.

### [Load secrets from your own store](https://vitehub.dev/docs/env/server-api#read-external-env-storage)

Read values from an outside secret store. Each `loadServerEnv()` call reads them again.

### [Change a secret without a redeploy](https://vitehub.dev/docs/env/bridge#replace-a-credential)

An admin replaces a stored value while the app runs.

Use Env to declare public, build-time, server runtime, and secret values with typed accessors. Keep secret values out of source and client bundles.

Import `useServerEnv()` and `usePublicEnv()` from `vite-hub/env` in server code, and `usePublicEnv()` from `#vitehub/env/public` in browser code. Provider-backed Env values are read through the declared provider and do not become public automatically.

For a token that the client carries but must not read or change, such as an upload ticket, a signed link, or OAuth state, use `seal()` and `unseal()` from `vite-hub/env`. Do not write your own AES-GCM or HMAC code.

Prove missing, valid, and invalid values in the same host mode you will deploy. Inspect declaration metadata, never secret contents.

<!-- vitehub-docs:section-links -->
## In this section

- [Overview](https://vitehub.dev/raw/docs/env.md) (this page)
- [Get started](https://vitehub.dev/raw/docs/env/get-started.md)
- [Configure](https://vitehub.dev/raw/docs/env/configure.md)
- [Server API](https://vitehub.dev/raw/docs/env/server-api.md)
- [Limits and errors](https://vitehub.dev/raw/docs/env/limits-and-errors.md)
- [Env Bridge](https://vitehub.dev/raw/docs/env/bridge.md)
- [App secret](https://vitehub.dev/raw/docs/env/app-secret.md)
- [Sign and seal values](https://vitehub.dev/raw/docs/env/seal.md)
- [Service tokens](https://vitehub.dev/raw/docs/env/service-tokens.md)
- [Verify webhooks](https://vitehub.dev/raw/docs/env/webhooks.md)

Next: [ENV_R0001](https://vitehub.dev/raw/docs/env/diagnostics/env_r0001.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
