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

# Env contract

> Declare the env keys an app needs once, and have a deploy warn or stop before any container changes when one is missing, empty or malformed.

An env contract lists the keys an app must have, and optionally the shape of each value. Before a deploy builds or starts anything, Levelrail checks the app's env against the contract. It reports key names and reasons only, never values.

## What a rule says

* **required**: the key must be set and not empty. An optional key is only checked when it is set.
* **format**: `nonempty`, `url`, `port` (1 to 65535), `int`, `bool` or `enum:a,b,c`.
* **secret**: the key must be stored as a secret. A plain value for it is reported.

## What is reported

| Kind | Meaning |
| - | - |
| `missing` | A required key is not set. |
| `empty` | A required key has an empty value. |
| `malformed` | The value does not match its format. |
| `not_secret_but_sensitive` | The rule says secret, but the key is plain env. |

## Where the contract is checked

The contract is checked before deploy, for deploys started from the dashboard, CLI or API and for git push deploys. Nothing else triggers the gate. Saving env values, applying an env file, restoring an env revision or rolling back does not run it, so a change can be saved that the next deploy then reports. Run `apps env contract check` or `env check` at any time to see the current result.

## Warn or block

The setting `env.contract.mode` decides what a deploy does. The default is `warn`.

* `off`: the contract is not checked before deploy.
* `warn`: the violation is recorded as an app event and the deploy goes on.
* `block`: the deploy fails before any container changes, and the message lists the key names and reasons.

Change it in Settings, or with the CLI settings commands. It applies at once.

If the contract cannot be read during a deploy, `block` mode stops the deploy with a message to try again, and `warn` mode lets it go on. A secret rule counts as met only when a value is stored for that key.

Editing the contract needs the `write:sensitive` ability. A caller without `read:sensitive` can still run a check, but it only gets `missing` and `empty` results. Other kinds are left out, and key names are returned only for keys the caller listed in the request.

## Use it

```bash theme={"dark"}
levelrail-cli apps env contract set web --rule DATABASE_URL=url --rule PORT=port --rule '?LOG_LEVEL=enum:debug,info' --secret DATABASE_URL
levelrail-cli apps env contract show web
levelrail-cli apps env contract check web -o json
```

`check` exits with a non-zero code when a rule is broken, so a CI job can gate on it. The API is `GET` and `PUT /api/v1/apps/{name}/env/contract` and `POST /api/v1/apps/{name}/env/contract/check`. The MCP tool `get_app_env` also returns `contract_violations` with names and kinds.

A contract belongs to one app. If you delete an app and create another with the same name, the new app starts with no contract.

## Check from CI

`env check` compares the keys your repo files declare with the app's env, plus the app's own contract. It sends key names only.

```bash theme={"dark"}
levelrail-cli env check web --file .env.example --compose docker-compose.yml --format github
```

* `--file` reads `KEY=value` lines, `--schema` reads the `properties` of a JSON schema (`required` narrows which must exist), `--compose` reads keys written as `${VAR:?message}`.
* Exit 0 means clean, 1 means a key is missing or broken, 2 means a file could not be read.
* `--format github` prints one error annotation per problem, naming the file that declared the key.

The repository ships a composite action, `actions/env-check`, that installs the CLI and runs this check.

## Check a file offline

`env lint` checks dotenv style files on your machine and sends nothing anywhere. It reports key names, duplicate keys, values the server would refuse, and values that look like credentials, by key and line and never by value.

```bash theme={"dark"}
levelrail-cli env lint .env.example --format github
```

Exit 0 means clean, 1 means a finding was reported, 2 means a file could not be read. The formats are `text`, `json` and `github`.

## Seeded from templates and compose files

When a template or compose file is installed, every environment key whose value uses `${VAR:?message}` is added to the member service's contract as required. Rules you already set are never changed. The mode stays `warn` unless you change it.

## Failure code

A deploy stopped by the contract fails with the code `env_contract_blocked`, which the dashboard and CLI use instead of reading the message text.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.