# Secrets and settings

Deployed functions read configuration from environment variables. Some are
set by Spaceboy; the rest are **custom secrets** you set per project.

Development environments take custom secrets without publishing. Secrets
for production need a published project: without one, `secrets set
--environment production` exits 4 with `error.code` `NOT_PUBLISHED`,
`error.details.usageBilling` (price, billing page) and `error.action` the
command that publishes, `spaceboy billing publish --project prj_...` (a
person approves the price). An unpublished project holds up to 25 names.

## Custom secrets from the CLI

```bash
spaceboy environments list --project prj_... --json      # the ids --environment takes
printf %s "$TEST_KEY" | spaceboy secrets set PARTNER_KEY --project prj_... --environment development
printf %s "$LIVE_KEY" | spaceboy secrets set PARTNER_KEY --project prj_... --environment production --deploy
printf %s "$KEY" | spaceboy secrets set RESEND_API_KEY --project prj_...   # every environment
spaceboy secrets list --project prj_... --json
spaceboy secrets doctor --project prj_...
spaceboy secrets unset PARTNER_KEY --project prj_... --environment production
```

- One name can hold a different value per environment: a test key in
  development and a live key in production, under the same name, so app
  code reads one variable.
- `set NAME --environment development` sets the value for every
  development environment of the project: yours, your teammates', and ones
  created later. `--environment production` does the same for production.
  `--environment <id>` sets one environment only, an exception to its
  class. `--environment all` sets one value everywhere. Each environment
  gets its own value first, then its class's, then the `all` value. Other
  environments keep their values.
- Without `--environment`: `all` once the project is published; before
  that, development only, so a test key never reaches production when the
  project goes live.
- The value is read from stdin (a hidden prompt on a terminal; a pasted
  multi-line value is kept whole and finishes with Ctrl-D). Leading and
  trailing whitespace is trimmed, since a pasted trailing space or line
  break breaks most keys; `--no-trim` keeps it. Inner line breaks (a PEM
  key) are always kept. `--value` works but lands in shell history.
- Values arrive exactly as set: JSON, quotes, `#`, `=`, `$`, backslashes
  and multi-line PEM keys need no encoding. A value with a NUL character or
  over 8192 characters is refused, and so is one that would take an
  environment's secrets past 48 KiB in total.
- Values are write-only. `list` shows each environment: set or not, where
  its value comes from, when it changed, and a fingerprint (a keyed hash:
  within a project, equal fingerprints mean equal values). Only owners and
  admins see fingerprints.
- `set` prints what changed on each environment and whether a redeploy is
  needed; `--deploy` redeploys the targets that need it.
- `doctor` lists, per environment, the `secret`-class names your
  `env.manifest.json` declares that are not set there, and which names hold
  the same or different values across environments. It warns when a
  development environment receives the value production uses for a key,
  token or password, and when a value stored before exact delivery should
  be re-set. It exits 1 when something declared is missing.
- Names are `UPPER_SNAKE_CASE`, start with a letter, at most 64 characters.
  Names the platform sets itself are refused, including anything starting
  with `VITE_`, `DB_`, `DATABASE_`, `AUTH_`, `APP_`, `SMTP_`, `STRIPE_`,
  `OPENAI_`, `GOOGLE_`, `AI_`, `STORAGE_`, `JOBS_`, `FIREBASE_`, `GCP_`,
  `NODE_`, and `PORT`, `PATH`, `HOME`, `TZ`, `LANG`.
- Owners and admins only.

In code: `process.env.RESEND_API_KEY`. Declare the variable in
`env.manifest.json` (class `secret`) so local development, the adoption
preflight and `secrets doctor` know about it.

## When a new value takes effect

On the environment's next successful deploy (`spaceboy deploy` or
`spaceboy push`). Running functions keep the old value until then. A deploy
notices that the environment's secrets or settings changed and always
redeploys the functions in that case, even when no backend code changed.

If the platform cannot prepare the environment's secrets and settings for a
deploy (it retries brief failures first), the deploy fails with
`runtime_env_refresh_failed` and the reason, and nothing is shipped: the
running version keeps its previous values. It never ships the old values
while reporting success.

## Settings the platform provides

Set on every deployed environment with the matching capability, and not
overridable: `DATABASE_URL` (`DATABASE`), `AUTH_JWT_SECRET`,
`AUTH_PUBLIC_URL`, `APP_BASE_URL` and mail settings (`AUTH`, `EMAIL`),
storage, jobs, AI-gateway and documents settings for their capabilities,
Stripe and OpenAI keys from the organization's connected integrations
(connect Stripe on the project's Connections page, More → "Stripe, GitHub
and other connections", or with `spaceboy integrations stripe connect`;
a payments app cannot deploy to production without it).
Development deploys of a payments app run a simulated checkout until a
Stripe test key is connected for development:
`printf %s "$STRIPE_TEST_KEY" | spaceboy integrations stripe connect --development`,
then deploy development again. Checkout there is real Stripe test mode (test
cards, no real money). That key takes test-mode keys only and is never used
by production, which still needs its own live key.

## Not available

- **Public (browser) settings.** The web app is built with Vite, which only
  exposes `VITE_*` variables to browser code, and `VITE_*` names are
  reserved. A value the browser needs (a publishable key, a public URL)
  must be committed in the code or served by your API.
- Reading a secret's value back, or setting secrets in bulk from a file.
- Applying a changed secret without a deploy.
