# Working with environments

How the CLI keeps track of your Solidafy environments, how to add and switch between them, and the guards that stop accidental writes to production.

## Two meanings of "environment"

In Solidafy, an environment is a separate workspace with its own data, pipelines and dashboards (see [Core concepts](/docs/getting-started/concepts#organisations-and-environments)). In a CLI project, an environment is a local name for one of those workspaces plus the credentials to reach it. You pick the local names. A common setup is `sandbox` for building and testing and `production` for what people rely on.

A personal access token belongs to the Solidafy environment you created it in, so each CLI environment needs its own token, created while you were in the matching environment in the portal.

## Where things are stored

```text
solidafy.json                 which environments exist and which one is active
environments/
  sandbox/
    .env                      credentials for sandbox (never committed)
    pipelines/                pipeline YAML for sandbox
    dashboards/
  production/
    .env
    pipelines/
    dashboards/
```

`solidafy.json` sits at the root of the project. Any command run in that folder or below it finds it:

```json
{
  "organization": "my-solidafy-project",
  "apiUrl": "https://api.solidafy.com",
  "activeEnvironment": "sandbox",
  "environments": [
    "sandbox",
    "production"
  ]
}
```

`organization` is the project name you gave `solidafy init`, and `activeEnvironment` is the one every command acts on.

Each environment's credentials live in `environments/<name>/.env`:

```text
# Solidafy sandbox environment
SOLIDAFY_PAT=sk_pat_...
SOLIDAFY_API_KEY=sk_pat_...
SOLIDAFY_API_URL=https://api.solidafy.com
```

`SOLIDAFY_API_KEY` defaults to the same token unless you pass `--api-key`. The `.gitignore` that `solidafy init` writes excludes `environments/*/.env`, so you can commit everything else. Other commands keep their files per environment too: `report pull` writes to `environments/<name>/reports/`, `liveboard pull` to `environments/<name>/liveboards/`, `policies pull` to `environments/<name>/policies/`.

> **Note:** The CLI reads credentials only from these `.env` files. It does not pick them up from environment variables in your shell.

## Adding an environment

```bash
solidafy env add production --pat sk_pat_...
```

This checks the token against the API first and stops if it does not work. On success it creates `environments/production/` with `pipelines/` and `dashboards/` folders, writes the `.env` file, and adds `production` to `solidafy.json`. It does not switch to the new environment.

To replace the token for an existing environment, for example when one expires, run the same `env add` command again with the new token.

When you clone a project, the `.env` files are not there. Run `env add` once per environment you need. Until you do, commands fail with `Environment '<name>' not found`.

| Flag | Description |
| --- | --- |
| `--pat <token>` | Personal access token. Required. |
| `--token <token>` | Same as `--pat`. |
| `--api-key <key>` | API key. Defaults to the token. |
| `--api-url <url>` | API base URL. Defaults to `https://api.solidafy.com`. |

## Switching and checking

```bash
solidafy env list            # every environment; the active one is marked
solidafy env use production  # make production the active environment
solidafy env status          # active environment, API reachable, pipeline and dashboard counts
```

`env use` refuses a name that is not in `solidafy.json`, or one that has no credentials on this machine. Most commands also print the environment they are acting on at the top of their output, so it is easy to spot when you are pointed at the wrong one.

## Write guards

Two guards protect your data from a stray command.

**Queries are read-only by default.** `solidafy dsl` only reads. To run a query that writes a table (one that ends in `.output(...)`), you have to add `-x`:

```bash
solidafy dsl 'source("raw.events").filter(ok == true).output("bronze.events", mode: replace)'      # read-only: writes nothing
solidafy dsl -x 'source("raw.events").filter(ok == true).output("bronze.events", mode: replace)'   # runs as a write job
```

**Writes to production need `--force`.** When the active environment is named `production` or `prod` (in any letter case), a write query also needs `--force`:

```bash
solidafy dsl -x --force '...'
```

The same rule applies to the write tools of the [MCP server](/docs/cli/ai-agents), which take `force: true` instead.

The production check looks at the local name in `solidafy.json`, not at the environment's type in Solidafy. If you call your production environment something else, such as `live`, the guard does not apply, so name it `production` or `prod`. The guard covers write queries only. Pushing pipelines, dashboards, reports and liveboards is not affected by it, so check `env status` before you push. Try a change in your sandbox environment first, then make the same change in the production environment's files and push those.
