# Environment variables

Source: https://docs.apphaven.eu/environment-variables

Your services receive configuration as environment variables, from two
places: the `env` map in [your manifest](https://docs.apphaven.eu/manifest.md), and the **Variables &
Secrets** you manage in the console.

## In the manifest

Set environment variables for a service with its `env` map. Keys and values
are strings:

```yaml
services:
  web:
    type: container
    image: ghcr.io/example/app:1.4.2
    env:
      NODE_ENV: production
      LOG_LEVEL: info
```

Use the manifest for defaults and non-sensitive configuration: it lives in
your repository, versioned with your code.

## Variables & Secrets in the console

For values that should stay out of your repository, open the app's
**Settings** tab in the console and find **Variables & Secrets**. Click **Add
variable** and choose the type:

- **Variable**: a plain value, visible in the console.
- **Secret**: encrypted at rest and write-only. Once saved, the value is
  never shown again, to anyone. See [Security: secrets](https://docs.apphaven.eu/security.md#secrets).

Both kinds are added to your services' environment on every deploy, and they
take precedence over a value with the same name set in the manifest, so the
manifest keeps the defaults and the console holds the real keys.

## Reserved names

Names beginning with `APPHAVEN_` are reserved by the platform, and the console
will not let you create a Variable or Secret with that prefix. AppHaven uses it
to deliver managed values to your services, such as the credentials for a
[managed Postgres database](https://docs.apphaven.eu/services/postgres.md).

## `APPHAVEN_ENVIRONMENT`

Your app often needs to know where it is running: production should send real
email and run scheduled jobs, while a copy of it usually should not. Every
container service gets `APPHAVEN_ENVIRONMENT`, set to one of:

| Value | Where it runs |
| ----- | ------------- |
| `prod` | Your production deployment, on the production branch. |
| `preview` | A [preview deployment](https://docs.apphaven.eu/previews.md) of another branch. |
| `restore` | A [restore preview](https://docs.apphaven.eu/restore.md#restore-previews): a private copy of your app with data restored from production. |

A restore preview holds a copy of your real production data, including your
users' details, so it is the one to be careful with. It runs with your app's
Variables and Secrets, the same keys production uses, so jobs, emails,
payments and webhooks that were waiting in the copied data would run a second
time. Use the variable so that only production does things with real-world
effects, such as sending email, taking payments, calling webhooks, running
scheduled jobs, or cleaning up storage it shares with production:

```js
const isProduction = process.env.APPHAVEN_ENVIRONMENT === "prod";

if (isProduction) {
  startScheduledJobs();
}
```

AppHaven always sets this variable itself: a value with the same name in your
manifest's `env` is replaced by the platform's.

## Connection references

`env` values can reference the connection details of a managed database in
the same manifest, like `${service.db.url}`. The references are resolved when
your app starts, so credentials never appear in your repository. See
[Database: connection references](https://docs.apphaven.eu/services/postgres.md#connection-references).

To pass a literal `${` through to your app, escape it as `$${`.

## Where to go next

- [Database](https://docs.apphaven.eu/services/postgres.md): the managed database and its connection references.
- [Container services](https://docs.apphaven.eu/services/container.md): the full service spec.
- [Getting started](https://docs.apphaven.eu/getting-started.md): deploy your first app.
