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

# Secrets, env vars and .env files

> Give sandboxes environment variables and secret files such as .env, for all sandboxes or for one sandbox only.

boat injects two types of secret when a sandbox starts.

| Type | Inside the sandbox |
| - | - |
| Environment variables | Process env vars and shell exports, in `boat exec`, `boat ssh` and every shell |
| Secret files | Files under `/home/user`, at the relative path that you give |

```bash theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
boat env set-var base STRIPE_KEY=sk_live_123
boat new
boat exec <id> 'echo "[$STRIPE_KEY]"'   # prints [sk_live_123]
```

> [!WARNING]
>
> A sandbox gets **no** variables and **no** secret files when one of these is true:
>
> * Its environment is [safe for third parties](https://docs.boat.dev/environments/access.md#safe-for-third-parties). `boat onboard` can turn this on.
> * You created it with `--no-env`.
> * The **Secrets** toggle of the environment is off.
> * It started before you saved the secret. Run `boat env upgrade base`, or read [Versions and upgrades](https://docs.boat.dev/environments/versions.md).
>
> Run `boat env info base` to see the switch and the toggles.

Use secrets for app credentials, API keys, `.env` files, deployment tokens and [private registry logins](https://docs.boat.dev/environments/private-registries.md).

Do not put secrets in these places:

* Prompts.
* URLs.
* CLI arguments that can go into logs.
* Docker build args.
* Committed files.

## Set variables and secret files

Each call changes one item and makes a new environment version. The other items stay.

```bash CLI theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
boat env set-var base STRIPE_KEY=sk_live_123
boat env rm-var base STRIPE_KEY
boat env set-file base backend/.env --from ./local.env
cat ./local.env | boat env set-file base backend/.env
boat env rm-file base backend/.env
```

```bash curl theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
# $BOAT_API_BASE is https://boat.dev/api/v1
ENV_ID=$(curl -sS "$BOAT_API_BASE/environments" -H "Authorization: Bearer $BOAT_API_KEY" \
  | jq -r '.environments[] | select(.name == "base") | .id')

curl -sS -X PUT "$BOAT_API_BASE/environments/$ENV_ID/vars/STRIPE_KEY" \
  -H "Authorization: Bearer $BOAT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"value":"sk_live_123"}'

curl -sS -X PUT "$BOAT_API_BASE/environments/$ENV_ID/secret-files" \
  -H "Authorization: Bearer $BOAT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"path":"backend/.env","contents":"DATABASE_URL=postgres://...\n"}'

curl -sS -X DELETE "$BOAT_API_BASE/environments/$ENV_ID/vars/STRIPE_KEY" \
  -H "Authorization: Bearer $BOAT_API_KEY"
curl -sS -X DELETE "$BOAT_API_BASE/environments/$ENV_ID/secret-files?path=backend%2F.env" \
  -H "Authorization: Bearer $BOAT_API_KEY"
```

```ts TypeScript theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
const { environments } = await sandbox.environments();
const envId = environments.find((e) => e.name === "base")!.id;

await sandbox.setEnvironmentVar({ environmentId: envId, key: "STRIPE_KEY", value: "sk_live_123" });
await sandbox.setEnvironmentSecretFile({
  environmentId: envId,
  path: "backend/.env",
  contents: "DATABASE_URL=postgres://...\n",
});
await sandbox.deleteEnvironmentVar({ environmentId: envId, key: "STRIPE_KEY" });
await sandbox.deleteEnvironmentSecretFile({ environmentId: envId, path: "backend/.env" });
```

```python Python theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
from boat_sdk.models.set_environment_var_request import SetEnvironmentVarRequest
from boat_sdk.models.set_environment_secret_file_request import SetEnvironmentSecretFileRequest

env_id = next(e.id for e in sandbox.environments().environments if e.name == "base")

sandbox.set_environment_var(env_id, "STRIPE_KEY", SetEnvironmentVarRequest(value="sk_live_123"))
sandbox.set_environment_secret_file(env_id, SetEnvironmentSecretFileRequest(
    path="backend/.env",
    contents="DATABASE_URL=postgres://...\n",
))
sandbox.delete_environment_var(env_id, "STRIPE_KEY")
sandbox.delete_environment_secret_file(environment_id=env_id, path="backend/.env")
```

> [!NOTE]
>
> The older `POST /secrets` call (`updateSecrets` in the SDKs) changes only the default environment. It **replaces** all variables and secret files. Use the per-environment calls above instead.

## .env files

A secret file is the way to put a `.env` file in each sandbox. The path is relative to `/home/user`. There is no repository picker. To put a file inside a clone, start the path with the repository folder name.

This path:

```text theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
web-app/backend/.env
```

writes to:

```text theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
/home/user/web-app/backend/.env
```

boat skips absolute paths and paths that go outside `/home/user`.

## Variables for one sandbox

The variables of an environment apply to every sandbox that uses it. To give one sandbox its own values, pass `env` when you create it.

boat merges the per-sandbox values over the environment's values. If the two have the same key, the per-sandbox value wins. Per-sandbox values also reach a sandbox made with `--no-env`.

```bash CLI theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
boat new --env DATABASE_URL=postgres://... --env FEATURE_FLAG=1
```

```bash curl theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
curl -sS -X POST "$BOAT_API_BASE/sandboxes" \
  -H "Authorization: Bearer $BOAT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"ttlSeconds":3600,"env":{"DATABASE_URL":"postgres://...","FEATURE_FLAG":"1"}}'
```

```ts TypeScript theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
await sandbox.create({
  ttlSeconds: 3600,
  env: { DATABASE_URL: "postgres://...", FEATURE_FLAG: "1" },
});
```

```python Python theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
from boat_sdk.models.create_sandbox_request import CreateSandboxRequest

sandbox.create(create_sandbox_request=CreateSandboxRequest(
    ttl_seconds=3600,
    env={"DATABASE_URL": "postgres://...", "FEATURE_FLAG": "1"},
))
```

| Rule | Limit |
| - | - |
| Key format | `[A-Za-z_][A-Za-z0-9_]*` |
| Key length | 128 characters maximum |
| Variables per sandbox | 100 maximum |
| Total size per sandbox | 64KB maximum |
| Reserved boat-internal names (`ASCII_TOKEN`, `BOAT_ID`, and similar) | Rejected |

A fork gets the per-sandbox variables of its source sandbox. If the fork passes its own `env`, it does not get them.


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