> ## Documentation Index
> Fetch the complete documentation index at: https://momentic.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Environment variables

> How Momentic reads environment variables and secrets at runtime, in order of precedence.

Momentic resolves environment variables from three places, in order of
precedence:

1. **CLI flags** (e.g. `--env`)
2. **The enclosing shell** when `inheritFromShell: true` is set on an
   environment
3. **`momentic.config.yaml`**, either inline under
   [`envVariables`](/configuration/environments#environments-envvariables) or
   loaded from an [`envFile`](/configuration/environments#environments-envfile)
   / JSON file

See [`environments`](/configuration/environments) for the full schema.

## Secrets

For passwords, API tokens, and other secrets, prefer a `.env` file or shell / CI
environment variables over inlining values in `momentic.config.yaml`. See
[Secrets](/configuration/environments#secrets) for the recommended patterns.

## Reserved variables

These are set automatically at runtime and accessible in any step via
`{{ env.NAME }}`:

| Variable      | Description                         |
| ------------- | ----------------------------------- |
| `BASE_URL`    | Base URL of the current environment |
| `CURRENT_URL` | URL of the page being tested        |
| `ENV_NAME`    | Name of the current environment     |
| `TEST_NAME`   | Name of the test being executed     |

## Accessing in tests

Use the templating syntax anywhere a step accepts a string:

```yaml theme={null}
- type:
    text: "{{ env.USERNAME }}"
    into: Email field
```

Or access `env.VARIABLE` directly inside a **JavaScript** step.

## Setting at runtime

Use `setVariable` inside a **JavaScript** step to write a variable:

```javascript theme={null}
setVariable("OTP_CODE", "123456");
```

Or use the **Save to environment variable** option on any step with a return
value (e.g. **AI extract**, **JavaScript**).
