# Give stations their credentials

Secrets go in the factory's `.env`, and your code reads them. They never go in a station file. Each outside system your stations call gets one small module that reads its variables and builds its client, so the variable names live in one place.

## What's in `.env`

`src/index.js` loads `.env` from the factory root before anything else runs. It holds two kinds of value:

| Kind | Examples | Read by |
|---|---|---|
| The session's own settings | `ORCHESTRATOR_API_KEY`, `ORCHESTRATOR_API_SECRET`, `FACTORY_LOCATION`, `ORCHESTRATOR_URL`, `POLL_INTERVAL_MS` | lib-factory. The list, with defaults, is in the [lib-factory README](https://github.com/assemblyline-sh/lib-factory#env-vars). |
| Secrets for the systems your stations call | `LEDGER_API_KEY`, `LEDGER_API_SECRET` | Your code |

`.env` is in `.gitignore`. List every variable in `.env.example`, without its value, so the next machine knows what to fill in.

## Never put a secret in a station file

- **Not as a value.** A station file is committed to git and pushed to the Orchestrator, which stores it.
- **Not in its log either.** The session prints every station's config to its log on every run, so anything in the config ends up there:

```text
[IN2] Config: {"ledger_url":"https://ledger.example.com","api_key":"sk_live_…"}
```

A station config is sent as it is stored: `{{env.X}}` is not filled in.

Put the URL, account ids and other settings a station needs in its station config; read the secret in code.

## One client module per outside system

Give each system your stations call one module under `src/utils/`. It reads that system's variables, fails with their names when one is missing, and builds the client. Stations import the client and never touch `process.env`:

```js
// src/utils/ledger/client.js
import { ledgerApi } from './api.js';

const REQUIRED = ['LEDGER_API_KEY', 'LEDGER_API_SECRET'];

let shared = null;

export function ledgerClient(env = process.env) {
  const missing = REQUIRED.filter((name) => !env[name]);
  if (missing.length) throw new Error(`ledger_env: missing ${missing.join(', ')} (see .env.example)`);
  return ledgerApi({ api_key: env.LEDGER_API_KEY, api_secret: env.LEDGER_API_SECRET, api_url: env.LEDGER_API_URL });
}

/** The one client the session's stations share. */
export function sharedLedgerClient() {
  shared ??= ledgerClient();
  return shared;
}
```

```js
// in a station
const ledger = sharedLedgerClient();
await ledger.post(entry);
```

- **The client takes its credentials as arguments.** Whether it is a package or your own `api.js`, it doesn't read the environment itself, so a test can hand it fake ones.
- **The error starts with a reason** (`ledger_env:`), so a missing variable is easy to tell apart from other failures. See [Throw errors from a station](/docs/guides/station-errors#start-the-message-with-a-reason).
- **A station that serves several accounts** passes a prefix, and the module reads `ACME_LEDGER_API_KEY` for prefix `ACME`, and likewise for each other account. The names are still built in one place.

The path of a command-line tool is a machine setting, not a secret, and also comes from `.env`: see [Call a command-line tool from a station](/docs/guides/run-command-line-tools).

## Next

- [Run a factory session](/docs/guides/run-a-factory-session): what the session checks in `.env` when it starts.
- [Debug a run](/docs/guides/debug-a-run): where the resolved config shows up.
