# Secrets and environment variables

URL: https://stack.logicaltech.it/docs/deploy/environment-variables/

Store secrets encrypted and versioned at the organization, folder, project, environment or service level, reference them from services, and share plain variables across scopes.

Environment variables configure a service without changing its image: database URLs, API keys, feature flags. LogiStack keeps two kinds apart:

- **Secrets** are encrypted, versioned and audited. You define a secret once, at the level that owns it, and every service below that level can reference it. A service gets a secret only when its **Environment** tab references it.
- **Variables** are plain configuration. They cascade to every service in their scope.

Both are applied on the next deployment. Secrets can also live in HashiCorp Vault, Infisical or 1Password and be read at deploy time: see [External secrets](./external-secrets.mdx).

## Where secrets live [#where-secrets-live]

A secret is defined at one of five levels. A service can reference any secret defined on its own chain:

1. **Organization**: available to every service.
2. **Folder** and subfolder: available to every project inside.
3. **Project**: available to every service in the project.
4. **Environment**: available to every service in one environment, like the Production database password.
5. **Service**: available to that service only.

When two levels define the same key, the picker marks the less specific one as shadowed. For example, with `STRIPE_KEY` on the organization and on the Clients folder, services in Clients see the folder's as the first choice.

## Create a secret [#create-a-secret]

<Steps>
<Step>

### Open Secrets & variables [#open-secrets--variables]

- **Organization or folder**: select **Settings** › **Secrets & variables**. Pick the level in **Defined for**: the whole organization or one folder.
- **Project or environment**: open the project's actions menu (**⋯**) and select **Secrets & variables**, then pick the project or one environment.

![Settings › Secrets & variables, listing the organization secrets RESEND_API_KEY and SENTRY_AUTH_TOKEN with their version and usage](/screens/settings-variables.webp)

![The Secrets & variables page of the Storefront project, Production selected, with the secret DATABASE_PASSWORD at version 2 used by 7 services](/screens/project-variables.webp)

</Step>
<Step>

### Add it [#add-it]

On the **Secrets** tab, select **Add secret**. Enter the key, the value and, optionally, a description such as "Transactional email, rotated by ops". Select **Add secret**.

</Step>
<Step>

### Reference it from a service [#reference-it-from-a-service]

Open the service, select **Environment**, then **Add**. Type the name the container should see, select **Secret** and pick the secret from the list. The list groups secrets by where they are defined. The name in the container can differ from the secret's key: `DB_PASSWORD` can point at `PROD_PG_PASSWORD`.

![The Environment tab of the web service: DB_PASSWORD references the Production secret DATABASE_PASSWORD, STRIPE_SECRET_KEY a project secret, SESSION_SECRET the service's own secret, and PORT is plain text](/screens/service-environment.webp)

</Step>
<Step>

### Save [#save]

Select **Save and redeploy** to apply at once, or **Save** to apply on the next deployment.

</Step>
</Steps>

## Type a secret directly on a service [#type-a-secret-directly-on-a-service]

On a service's **Environment** tab you can also select **Text**, type the value and turn on **Secret**. The toggle turns on by itself when the name looks sensitive (`PASSWORD`, `TOKEN`, `KEY`, `SECRET`, `CREDENTIAL`, `PRIVATE`). When you save, LogiStack stores the value as a secret of that service and references it, so every secret is managed in one place. **Review before saving** lists which keys become secrets and which stay plain variables.

Removing the variable also removes that service secret, unless something else references it.

To add many at once, select **Paste .env**. The preview shows which keys will become secrets.

```bash title=".env"
STRIPE_SECRET_KEY=sk_live_51H...
PUBLIC_URL="https://shop.example.com"
LOG_LEVEL=warn
```

## Use a secret inside a value [#use-a-secret-inside-a-value]

A plain variable can embed a secret with `${{ secrets.KEY }}`. LogiStack replaces it at deploy time with the value of the most specific secret named `KEY` on the service's chain, so the connection string stays readable while the password stays a secret:

```bash title="Service variable"
DATABASE_URL=postgres://app:${{ secrets.PG_PASSWORD }}@db:5432/app
```

The variable is stored as typed, with the expression, and never holds the value. Once resolved, it is treated as a secret: masked in logs, and passed to a build as a BuildKit secret when marked **Build**. A name that no secret on the chain defines fails the deployment with `DATABASE_URL uses ${{ secrets.PG_PASSWORD }}, but no secret of that name is defined on this service's chain`. A value with an expression is never turned into a service secret, even with **Secret** on.

## Mount a secret as a file [#mount-a-secret-as-a-file]

Some images read credentials from a file, through variables like `POSTGRES_PASSWORD_FILE`. On a service's **Environment** tab, turn on **File** on a row that references a secret. On the next deployment the value is mounted read-only as a Swarm secret at `/run/secrets/<KEY>`, and the container gets `<KEY>_FILE=/run/secrets/<KEY>` instead of `<KEY>`. For example, `DB_PASSWORD` with **File** on gives the container the file `/run/secrets/DB_PASSWORD` and `DB_PASSWORD_FILE=/run/secrets/DB_PASSWORD`.

The value is not in the service's environment, so `docker service inspect` does not show it. A file is not passed to builds.

## See, change and roll back a secret [#see-change-and-roll-back-a-secret]

On the **Secrets** tab:

- The eye button shows the value. It needs the read secrets permission on the secret's level, and every reveal is recorded in the [audit log](../team/audit-log.mdx) as `secret.reveal`. The value hides again after 20 seconds.
- **Edit value** saves a new version. Services that reference the secret show the change in [Review changes](./deployments.mdx#review-changes-before-deploying) until they redeploy.
- **History** lists every version with its author. **Roll back** copies an earlier value as a new version.
- **Usages** lists the services that reference the secret. **Delete** is unavailable while it is in use.

Values never appear in variable lists, the API's variable endpoints, manifests, the audit log or deployment logs. Build and deployment logs replace any secret value, and its base64 form, with `***`.

## How secrets are encrypted [#how-secrets-are-encrypted]

Each organization has its own data key. Every secret version is encrypted with AES-256-GCM under that key and bound to its organization, secret and version number, so a value copied into another row does not decrypt. Data keys are themselves encrypted with a key derived from the instance master key, which never leaves the LogiStack containers.

An organization admin can select **Rotate encryption key** on **Settings** › **Secrets & variables**. LogiStack creates a new data key and re-encrypts every secret version. Values, versions and references don't change.

## Rotate the master key [#rotate-the-master-key]

The master key encrypts integration credentials, files and variables directly, and wraps every organization's data key. Rotate it if it may have leaked. LogiStack re-encrypts everything to the new key while it runs: during the rotation it reads values with either key.

<Steps>
<Step>

### Create the new key as a Docker secret [#create-the-new-key-as-a-docker-secret]

On a manager node, save the new key and create the secret from it:

```bash
openssl rand -base64 32 | tr -d '\n' > /root/logistack-master-key.new
docker secret create logistack-master-key-next /root/logistack-master-key.new
```

</Step>
<Step>

### Give it to the API and the worker, with the old key as the previous one [#give-it-to-the-api-and-the-worker-with-the-old-key-as-the-previous-one]

```bash
for svc in logistack_logistack-api logistack_logistack-worker; do
  docker service update \
    --secret-add source=logistack-master-key-next,target=master_key_next \
    --env-add LOGISTACK_MASTER_KEY_FILE=/run/secrets/master_key_next \
    --env-add LOGISTACK_MASTER_KEY_PREVIOUS_FILE=/run/secrets/master_key \
    "$svc"
done
```

</Step>
<Step>

### Wait for the worker to finish [#wait-for-the-worker-to-finish]

When the worker starts, it re-encrypts every stored value and re-wraps every data key, then logs `master key rotation finished: LOGISTACK_MASTER_KEY_PREVIOUS can be removed`. Rerunning is safe: values already on the new key are skipped.

```bash
docker service logs logistack_logistack-worker 2>&1 | grep "master key rotation"
```

</Step>
<Step>

### Make the new key the permanent one [#make-the-new-key-the-permanent-one]

The installer and in-app updates mount the secret named `logistack-master-key`, so replace it with the new value. Docker secrets cannot change in place, so each service lets go of it first:

```bash
for svc in logistack_logistack-api logistack_logistack-worker; do
  docker service update --secret-rm logistack-master-key \
    --env-rm LOGISTACK_MASTER_KEY_PREVIOUS_FILE "$svc"
done
docker secret rm logistack-master-key
docker secret create logistack-master-key /root/logistack-master-key.new
for svc in logistack_logistack-api logistack_logistack-worker; do
  docker service update \
    --secret-add source=logistack-master-key,target=master_key \
    --env-rm LOGISTACK_MASTER_KEY_FILE \
    --secret-rm logistack-master-key-next "$svc"
done
docker secret rm logistack-master-key-next
```

Then put the new key in `/etc/logistack/credentials` and delete `/root/logistack-master-key.new`.

</Step>
</Steps>

<Callout type="warn">
  Don't start an in-app update until the last step is done, and keep the new key in `/etc/logistack/credentials`. Without it, a restored database cannot be decrypted.
</Callout>

Caddy's certificate storage key (with `LS_CADDY_STORAGE_ENCRYPTION`) is pinned before the rotation, so stored certificates stay readable.

## Plain variables [#plain-variables]

The **Variables** tab holds plain variables at four scopes. When the same key is set at more than one, the most specific wins: **Organization**, **Project**, **Environment**, then **Service**.

For example, with `LOG_LEVEL=info` at the organization and `LOG_LEVEL=warn` on Production, every Production service gets `warn` and every other service gets `info`.

On a service, the **Inherited** panel lists the plain variables it gets from the organization, project and environment. A crossed-out row is overridden by a more specific one. **Override for this service** copies the key and value into **Service variables** for you to edit.

Mark a variable **Build** to also pass it to the build. Plain build variables become Dockerfile build arguments; secrets become BuildKit secrets. See [Build types](./build-types.mdx#build-time-variables-and-secrets).

## With the API [#with-the-api]

The same operations are available over the API and the MCP server (`secret_create`, `secret_reveal`, and so on).

```bash title="Create a Production database password"
curl -fsS -X POST "https://stack.example.com/api/secrets" \
  -H "x-api-key: $LOGISTACK_API_KEY" \
  -H "content-type: application/json" \
  -d '{
    "scope": "environment",
    "scopeId": "env_storefront_production",
    "key": "PG_PASSWORD",
    "value": "s3cret-2026"
  }'
```

`scope` is `organization`, `folder`, `project`, `environment` or `service`. To reference it, send `{"key": "DB_PASSWORD", "secretId": "sec_...", "isSecret": true, "isBuildTime": false}` in `PUT /api/services/{id}/env-vars`; add `"asFile": true` to mount it as a file. `POST /api/secrets/{id}/reveal` returns the value.

Secret variables created the older way, through `POST /api/env-vars` or a [project manifest](../automation/project-manifests.mdx) with `secret: true`, are converted into secrets on the next deployment. Every service that received one keeps receiving it through a reference.

## Variables set on every deployment [#variables-set-on-every-deployment]

LogiStack sets these on every container. They are read-only, and names starting with `LOGISTACK_` are reserved for them.

| Variable | Value |
|---|---|
| `LOGISTACK_DEPLOYMENT_ID` | The deployment that started this container |
| `LOGISTACK_COMMIT_SHA` | Full hash of the deployed commit. Git sources only |
| `LOGISTACK_COMMIT_MESSAGE` | First line of the commit message, up to 200 characters. Git sources only |
| `LOGISTACK_BRANCH` | Branch the commit was built from. Git sources only |
| `LOGISTACK_ENVIRONMENT` | Name of the environment |
| `LOGISTACK_PROJECT` | Name of the project |
| `LOGISTACK_SERVICE` | Name of the service |
| `LOGISTACK_URL` | Primary domain with its scheme, such as `https://app.example.com`. Unset without a domain |
| `LOGISTACK_FQDN` | Every hostname the service answers on, comma-separated. Unset without a domain |
| `LOGISTACK_PREVIEW_URL` | Pull request previews only: the preview's main URL. See [Preview environments](./preview-environments.mdx#reach-one-service-from-another) |
| `LOGISTACK_PREVIEW_URL_<SERVICE>` | Pull request previews only: the URL of each exposed service of the preview, such as `LOGISTACK_PREVIEW_URL_API` |

A Dockerfile build gets the ones it declares with `ARG`:

```dockerfile
ARG LOGISTACK_COMMIT_SHA
ENV SENTRY_RELEASE=$LOGISTACK_COMMIT_SHA
```

## Check that it worked [#check-that-it-worked]

Open the **Terminal** tab and print the variable:

```bash
printenv LOG_LEVEL
```

If it shows the old value, the service hasn't been redeployed since the change. Select **Deploy**.

## Common problems [#common-problems]

- **"Secret not available"**: the service references a secret that is no longer on its chain, for example because its project moved to another folder. Select **Fix** and pick another secret.
- **A secret can't be deleted**: it is in use. Open **Usages** and remove the references first.
- **A secret is missing from the picker**: it is defined on another branch, or you lack the read permission on its level.
- **"Defined twice"**: the same name appears on two rows. Keep one.
- **A reserved name is refused**: names starting with `LOGISTACK_` belong to the deployment context variables. Rename yours.
- **A build can't see a variable**: check its **Build** box and declare it with `ARG` in the Dockerfile.
