# Read your first environment value

Env gives your app its settings, such as its name, URLs, and API keys. Declare each value once in your config. Then read it in your code with the correct type.

In this tutorial, you declare an app name, `Acme`, and return it from a server route.

> [!NOTE] Before you start
> Start with the app from [Installation](https://vitehub.dev/docs/getting-started/installation) and run every command from the app root. You do not need an account or an API key.

::tutorial-step{title="Declare the value"}
## Declare the value

Add an `env` block to your config, under `vitehub({ env })` in Vite or `vitehub.env` in Nuxt. Keep your existing options.

### Nuxt

```ts [nuxt.config.ts]
import { env } from 'vite-hub/env'

export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: {
    env: {
      public: {
        appName: env({ default: 'Acme', mode: 'build' }),
      },
    },
  },
})
```

### 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({
    env: {
      public: {
        appName: env({ default: 'Acme', mode: 'build' }),
      },
    },
  }), nitro()],
})
```

### 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({
    env: {
      public: {
        appName: env({ default: 'Acme', mode: 'build' }),
      },
    },
  })],
})
```

- `public` holds values that browser code can read. Any visitor can see them, so never put a password or an API key here. Put secrets in `env.server`, and read them only in server code.
- `default: 'Acme'` is the value when no environment variable sets it. So this tutorial needs no environment variable.
- `mode: 'build'` reads the value once, when you build the app. The value is then part of the build output.

::

::tutorial-step{title="Read the value"}
## Read the value

Create `server/api/app-name.get.ts` to handle `GET /api/app-name`. It reads `appName` and returns it as JSON:

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

export default defineEventHandler(() => {
  const { appName } = usePublicEnv()
  return { appName }
})
```

`usePublicEnv()` returns the typed Env values generated from your config. Run this route through the dev server or the built server output. Outside a ViteHub build, it throws `ENV_NOT_GENERATED`.

::

::tutorial-step{title="Run and verify"}
## Run and verify

Start or restart the app after the config change:

```bash [commands/start]
pnpm dev
```

Keep it running. Open a second terminal and call the route:

```bash [commands/request]
curl http://localhost:3000/api/app-name
```

The route returns:

```json [output/response.json]
{"appName":"Acme"}
```

Your code read the default value that you declared in your config.

::

## Next steps

::card-group
:::card{title="Configure" icon="i-lucide-sliders-horizontal" to="/docs/env/configure"}
Add server values, secrets, and typed values, and read them from environment variables.
:::

:::card{title="Server API" icon="i-lucide-code" to="/docs/env/server-api"}
Read Server Env and secrets in your server code.
:::
::

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

- [Overview](https://vitehub.dev/raw/docs/env.md)
- [Get started](https://vitehub.dev/raw/docs/env/get-started.md) (this page)
- [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)
- [Schema file](https://vitehub.dev/raw/docs/env/env-spec.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: [Configure](https://vitehub.dev/raw/docs/env/configure.md)

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