# Read your first environment value

Env gives your app its settings, such as its name, URLs, and API keys. You declare each value once in your config. Then you 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
> Use the app from [Installation](https://vitehub.dev/docs/getting-started/installation). Run the commands from the root of the app. You need no account and no API key.

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

Add an `env` block to your config. In Vite, it goes in `vitehub({ env })`. In Nuxt, it goes in `vitehub.env`. Keep the other options of your app.

### Nuxt

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

export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: {
    host: 'node',
    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({
    host: 'node',
    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({
    host: 'node',
    env: {
      public: {
        appName: env({ default: 'Acme', mode: 'build' }),
      },
    },
  })],
})
```

- `public` holds values that browser code can read. Every 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 you need no environment variable for this tutorial.
- `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`. This file is the route `GET /api/app-name`. It reads `appName` and returns it:

```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()` reads the Env values generated from your config, so `appName` has a type. Run this route through the dev server or 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 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)
- [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)
