# Variables

Source: https://openpanel.sh/docs/variables

Environment variables for a service, set on the whole project, on one environment or on the service itself, and how they reach its containers.

Variables are the environment variables your service's containers get. You can set one for a single service, or share it with every service of a project or of one environment.

In the console, a service's own variables are on its **Variables** tab, and the ones shared with the whole project or one environment are on the project's **Shared variables** page. The [CLI](/docs/cli), the [API](/docs/api) and [MCP](/docs/ai) do the same.

## Where a variable is set [#where-a-variable-is-set]

| Scope | Who gets it |
|---|---|
| **Project** | Every service in the project. |
| **Environment** | Every service in that environment of the project: `production`, or another a project admin added, such as `staging`. |
| **Service** | That service only. |

When the same name is set in more than one scope, the narrowest wins: the service's own over the environment's, the environment's over the project's.

## Set, list and remove [#set-list-and-remove]

<Tabs items={["Console","CLI","API"]}>
<Tab value="Console">
- **For one service:** open the project, click the service on the canvas, and go to its **Variables** tab. It lists the service's own variables, and under them what the project and its environment share with it.
- **For the whole project or one environment:** open the project's **Shared variables** page. It has a card for the whole project and one for each environment.
- **Add:** click **Add variable**, give a **Name** and a **Value**, tick **Secret** to keep the value sealed, and click **Add variable**.
- **Change or remove:** use **Edit** or **Delete** on the variable's row. A secret's value can only be replaced, never shown.

Changes take effect at the service's next deploy.
</Tab>

<Tab value="CLI">
```sh
# For every service in the project
openpanel env set LOG_LEVEL=info --project shop

# For one service; several at once
openpanel env set DATABASE_URL=postgres://db:5432/shop CACHE_TTL=60 --project shop --service api

# What is set on the project, or on one service
openpanel env --project shop
openpanel env --project shop --service api

# Remove one
openpanel env unset CACHE_TTL --project shop --service api
```

For an environment, use `--environment <environment id>`; `openpanel projects get shop` shows the ids.
</Tab>

<Tab value="API">
```sh
# Set (or replace) a variable; send serviceId or environmentId to narrow it
curl -X PUT "https://openpanel.sh/api/v1/projects/$PROJECT_ID/variables/DATABASE_URL" \
  -H "Authorization: Bearer $OPENPANEL_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{ "value": "postgres://db:5432/shop", "serviceId": "'"$SERVICE_ID"'" }'

# What is set in one scope: the project, or with ?serviceId= or ?environmentId=
curl "https://openpanel.sh/api/v1/projects/$PROJECT_ID/variables?serviceId=$SERVICE_ID" \
  -H "Authorization: Bearer $OPENPANEL_TOKEN"

# Remove one, by the id the list gives
curl -X DELETE "https://openpanel.sh/api/v1/variables/$VARIABLE_ID" \
  -H "Authorization: Bearer $OPENPANEL_TOKEN"
```
</Tab>
</Tabs>

Setting a name that is already set in the same scope replaces it.

## Changes take effect at the next deploy [#changes-take-effect-at-the-next-deploy]

Setting or removing a variable changes nothing that runs. The next [deploy](/docs/deploy) gives the service its variables as they are then, and its [deployment](/docs/concepts/deployments) keeps that copy, so a rollback runs with the variables of the day you roll back.

## What a service gets [#what-a-service-gets]

To see every variable a service will get at its next deploy, with the scope each comes from:

<Tabs items={["CLI","API"]}>
<Tab value="CLI">
```sh
openpanel env get DATABASE_URL --project shop --service api
```

`env get` with `--service` reads one name as the service gets it. To write them all to a file, see [.env files](/docs/variables/env-files).
</Tab>

<Tab value="API">
```sh
curl "https://openpanel.sh/api/v1/services/$SERVICE_ID/env" \
  -H "Authorization: Bearer $OPENPANEL_TOKEN"
```

```json
{
  "data": [
    { "name": "DATABASE_URL", "value": "postgres://db:5432/shop", "secret": false, "scope": "service", "variableId": "…" },
    { "name": "LOG_LEVEL", "value": "info", "secret": false, "scope": "project", "variableId": "…" },
    { "name": "STRIPE_KEY", "value": null, "secret": true, "scope": "service", "variableId": "…" }
  ]
}
```
</Tab>
</Tabs>

Values are shown as you typed them; [references](/docs/variables/references) are worked out at the deploy.

## Names and values [#names-and-values]

- **Names** are letters, digits and underscores, do not start with a digit, and are at most 256 characters. Case matters.
- **Values** are text up to 64 KB. Line breaks are kept.

## Who can do what [#who-can-do-what]

| | Can view | Can deploy and above |
|---|:-:|:-:|
| List variables and read plain values | ✓ | ✓ |
| Set, replace and remove variables | – | ✓ |
| Read a secret's value | – | – |

Anyone who can see a project can read its plain values, so keep tokens and passwords as [secrets](/docs/variables/secrets). Access is per project: see [Per-project access](/docs/members/per-project-access). Every change is in the [audit log](/docs/monitoring/audit-log), by name, never by value.

<Cards>
<Card href="/docs/variables/secrets" title="Secrets" description="Values that are sealed and never shown again." />

<Card href="/docs/variables/references" title="References" description="Build a value from a shared variable or another service's." />

<Card href="/docs/variables/env-files" title=".env files" description="Write a service's variables to a .env file." />
</Cards>