# Environment variables

> Variables per environment and per branch, encrypted with a KMS key in your own AWS account. Production and preview values are never shown again once saved.

- Category: Deploy
- URL: https://www.forkrail.com/docs/environment-variables

## Three environments

| Environment | Used by | Values readable? |
| --- | --- | --- |
| Production | Production deployments | No: write-only, never shown or printed again |
| Preview | Every other deployment (with optional per-branch values) | No: write-only |
| Development | Your own machine, through `forkrail env pull` or `env run` | Yes, for developers and above |

Changes apply to the next deploy or redeploy, never to deployments that already exist. That’s what [Redeploy](https://www.forkrail.com/docs/deployments-and-rollbacks#promote-rollback-redeploy) is for.

## Add and remove variables

In the dashboard, open the project’s **Environment variables** page. You can paste a whole `.env` file into the key field to fill in many at once. From the CLI, values come from stdin or a prompt and are never printed:

```bash
# Production and previews (the default without --target)
forkrail env add my-app DATABASE_URL

# One environment, from stdin
printf '%s' "$STRIPE_KEY" | forkrail env add my-app STRIPE_SECRET_KEY --target production

# Several at once
forkrail env add my-app API_URL --target preview,development

forkrail env ls my-app                  # names only
forkrail env rm my-app API_URL --target preview
```

`--target` (also `-e` or `--environment`) takes `production`, `preview`, `development`, or several comma-separated.

## Values for one branch

A preview variable can be set for one Git branch, as on Vercel. That branch’s previews get its value instead of the preview value of the same name, and every other preview variable as usual.

```bash
forkrail env add my-app API_URL --git-branch feature/checkout
forkrail env rm my-app API_URL --git-branch feature/checkout
```

The branch is the one a deployment was made from: a push’s branch, or the branch checked out when you ran `forkrail deploy`. Production deployments never get branch values.

## Local development

```bash
forkrail env pull                               # Development values into .env.local
forkrail env pull .env --environment preview    # another file and environment
forkrail env run -- npm run dev                 # run a command with the values, no file written
```

An existing file is merged after you confirm (`--yes` in scripts). A new file is readable only by you, and the CLI warns when git doesn’t ignore it. Production and preview values can’t be read back, so they’re written empty for you to fill in.

## System variables

Every deployment is told about itself, under Vercel’s names, so code moved from Vercel finds what it expects:

| Variable | Value |
| --- | --- |
| `VERCEL` | `1` |
| `VERCEL_ENV`, `VERCEL_TARGET_ENV` | `production` or `preview` |
| `VERCEL_URL` | The deployment’s own host |
| `VERCEL_BRANCH_URL` | The branch URL of a branch preview |
| `VERCEL_PROJECT_PRODUCTION_URL` | The production host |
| `VERCEL_REGION` | Where it runs, as Vercel names it (`iad1`, `fra1`…) |
| `VERCEL_DEPLOYMENT_ID`, `VERCEL_PROJECT_ID`, `VERCEL_PROJECT_NAME` | The deployment’s id and the project’s name |
| `VERCEL_GIT_COMMIT_SHA`, `_REF`, `_MESSAGE`, `_AUTHOR_NAME`, `_REPO_OWNER`, `_REPO_SLUG`, `_PULL_REQUEST_ID` | The commit, for deployments made from Git |
| `FORKRAIL_ENV`, `FORKRAIL_URL`, `FORKRAIL_REGION` (AWS name), `FORKRAIL_DEPLOYMENT_ID`, `FORKRAIL_PROJECT_NAME` | forkrail’s own names for the main ones |

Builds get the same, plus `CI=1` and `VERCEL_GIT_PREVIOUS_SHA`. Node apps run with `NODE_ENV=production`, previews too, as on Vercel. A variable you set yourself wins over a system variable of the same name. `VERCEL_OIDC_TOKEN` isn’t set; give the project an [IAM role](https://www.forkrail.com/docs/functions#iam-role) instead.

## Names and limits

- Names are letters, digits and `_`, not starting with a digit.
- Reserved: `AWS_LAMBDA_*`, `FORKRAIL_*`, `PATH`, `HOME`, `TEMP`, `TZ`, `PYTHONPATH` and a few of Lambda’s own. You may set the AWS SDK’s `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_SESSION_TOKEN` and `AWS_REGION`, as Vercel allows.
- Up to **64 KB per environment**, names and values together, as on Vercel. One value can take all of it.

Lambda’s own environment holds only 4 KB. When a deployment’s variables don’t fit, forkrail seals them in an encrypted (AES-256-GCM) file with a key made for that deployment, and your app reads them as usual. Nothing changes for your code.

## Where values are kept

Each value is an AWS SSM SecureString parameter, encrypted with forkrail’s own KMS key in your account. Only identities you allow to decrypt with that key can read a value: the control function and your administrators. The routers, the dashboard and your apps can’t. Values are masked in build logs. The key costs $1 a month; the parameters are free.

Who can change what: developers change preview and development variables; members and owners change production’s too; viewers see names only. See [Team and roles](https://www.forkrail.com/docs/team-and-sign-in).
