# Variables and references

Every environment has **project variables**, shared by its services, and each service has its own **service variables**. A value can be plain text, or it can **reference** other variables, so a connection string, an address or a key is set once and every service that needs it stays in step.

**The syntax.** A reference is `{{ owner.NAME }}`:

- `owner` is `project` for a project variable, or the **name of a service** in the same environment (names are compared without case). A service may reference its own variables.
- `NAME` is the variable's name: letters, digits and `_`, not starting with a digit, at most 128 characters.
- Spaces just inside the braces are optional: `{{db.PGHOST}}` and `{{ db.PGHOST }}` are the same.

**References inside text.** A value can hold several references, and everything around them is kept as written:

```
DATABASE_URL = postgres://{{ db.PGUSER }}:{{ db.PGPASSWORD }}@{{ db.DISPRA_PRIVATE_HOST }}:5432/app
API_BASE     = {{ api.DISPRA_PUBLIC_URL }}/v1
LOG_LEVEL    = {{ project.LOG_LEVEL }}
```

**Built-in variables.** Every service has these, without setting them. Reference them like any other variable, for example `{{ api.DISPRA_PUBLIC_URL }}`:

- `DISPRA_PRIVATE_HOST`: the service's private hostname, reachable only from the project's other services.
- `DISPRA_PRIVATE_URL`: its private address, `host:port`.
- `DISPRA_PUBLIC_URL`: its best public `https://` address.
- `DISPRA_SERVICE_NAME`: its name.

A built-in wins over a variable of the same name. A built-in a service doesn't have (no public address yet, for example) doesn't resolve: give the service an endpoint first.

**Setting one.** A reference is just part of the value:

```
curl -X PUT https://devapi.dispralabs.com/v1/cloud/services/svc_.../variables/DATABASE_URL \
  -H "Authorization: Bearer dsp_live_YOUR_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{"value": "postgres://{{ db.PGUSER }}:{{ db.PGPASSWORD }}@{{ db.DISPRA_PRIVATE_HOST }}:5432/app"}'
```

With the CLI: `dcloud vars set DATABASE_URL='postgres://{{ db.PGUSER }}@{{ db.DISPRA_PRIVATE_HOST }}:5432/app' --service api`.

A reference that doesn't resolve yet (the service it names isn't created, say) is still saved: the answer lists it in `warnings`, and a deployment refuses it until it resolves. A value that is **exactly one reference**, such as `{{ db.DATABASE_URL }}`, is stored as a link to that variable, so it always follows its source.

**Linking a project variable into a service.** To give a service a project variable under its own name, with the value always following the project's, set the service variable with `kind: shared`:

```
PUT /v1/cloud/services/svc_.../variables/STRIPE_KEY
{"kind": "shared", "ref_name": "STRIPE_KEY"}
```

A project variable that services link to can't be deleted until they unlink it (`409 variable_in_use`). Writing `{{ project.STRIPE_KEY }}` inside a value works too; use `shared` when the whole value is the project's.

**When references resolve.** When a deployment starts: for the build (variables with scope `build` or `both`) and for the running service (`runtime` or `both`). References can chain up to 10 deep; a cycle is refused. A change to a variable reaches a service at its **next deployment**: redeploy the services that reference it (**Create a deployment** with `action: redeploy`).

**When a reference doesn't resolve.** The deployment is refused with `422 variable_reference_unresolved`, and the message names the variable and the reference: an unknown service or variable, a built-in the service doesn't have, a cycle, a chain over 10. The running version keeps serving. Check before you deploy:

- **Validate variables text** checks a block of `NAME=value` lines (the raw editor's format) and reports each reference that doesn't resolve, and why.
- **Get the variables graph** shows every variable of the environment and which ones reference which.

**Values stay secret.** Every value is sealed. Resolution happens inside Dispra Cloud: answers carry names, never resolved values, and revealing a value is its own request (**Reveal a service variable**), which is rate limited and recorded.

**Writing `{{` literally.** `\{{` is a literal `{{` (the backslash is dropped). A `{{ ... }}` whose content isn't `owner.NAME` is left as written, so values that hold other template languages keep working.

**In dispra.yaml.** Values in `variables:` use the same references, and `${{ secret }}` declares a variable whose value you set outside the file:

```yaml
services:
  api:
    variables:
      DATABASE_URL: "postgres://{{ db.PGUSER }}:{{ db.PGPASSWORD }}@{{ db.DISPRA_PRIVATE_HOST }}:5432/app"
      PUBLIC_URL: "{{ api.DISPRA_PUBLIC_URL }}"
      STRIPE_KEY: "${{ secret }}"
variables:
  LOG_LEVEL: info
```
