
# Secrets

Declare the names your app needs with `[secrets] required = ["STRIPE_KEY"]`, run `agc secret set STRIPE_KEY`, and give the person the link it answers with. They paste the value in the console, and production and rehearsals get it as an environment variable that nobody can read back.

Success: `agc secret ls` lists the name with `in_live_release: true`, and `agc ship` no longer refuses with `ship.secret_missing`.

## Ask your agent

```text
The app needs our Stripe secret key. Set it up so I enter the key myself;
don't ask me to paste it to you.
```

The agent answers with a console link. You paste the value there and save. On a live app, saving starts a release.

## Steps

```toml
[secrets]
required = ["STRIPE_KEY"]
```

```bash
agc secret set STRIPE_KEY
```

```json
{
  "ok": true,
  "product": "agent.cloud",
  "command": "secret set",
  "data": {
    "name": "STRIPE_KEY",
    "set_url": "https://agent.cloud/console/apps/camp-bookings/settings?secret=STRIPE_KEY#secrets",
    "note": "Give this link to the person; they set the value there. Never ask for it in chat or write it to a file."
  },
  "next": [{ "cmd": "agc secret ls", "why": "see when it’s set" }]
}
```

The app reads it like any variable: `process.env.STRIPE_KEY`.

For a key the app makes up, such as an encryption or session-signing key, let Agent Cloud generate it. Nobody sees the value, so no person is needed:

```bash
agc secret set TOKEN_ENC_KEY --generate
```

Running it again rotates the key. Rotating an encryption key makes existing ciphertext unreadable, so only rotate a key the app can afford to lose or re-derive.

| Command | Answers |
|---|---|
| `agc secret ls` | `secrets` (each name with `set_by`, `set_at`, `in_live_release`), `required`, `missing`, `set_url` |
| `agc secret set NAME --generate` | `generated: true`, plus `op` and `version` if it started a release |
| `agc secret rm NAME` | A console link; the person deletes it there |

| Rule | |
|---|---|
| Name | Matches `^[A-Z][A-Z0-9_]{0,63}$`; not `PORT`, anything starting with `AGC_` or `AGENTCLOUD_`, the app's database variable (`DATABASE_URL` by default), or one of its connections' variables |
| Value | 1 to 32,768 characters; a generated one is 64 random bytes, base64 (88 characters) |

A generated key is the 64 bytes Phoenix and Rails want for `SECRET_KEY_BASE`, read as is. An app that needs exactly 32 bytes (an AES-256 key) uses the first 32 of the decoded value. A key generated before keys were 64 bytes stays 32 until you run `--generate` again.

## How it works

**Who sets values.** Only the app's owner and the organization's owners add, replace or delete a value, in the console under the app's Settings, Secrets. On the API that takes values, `PUT` or `DELETE /v1/apps/<app>/secrets/<name>` with an agent's token answers 403 `agent.not_allowed` with the link, whatever else that agent may do.

**A change is a release.** On a live app, any change to a secret starts a release of the live version: the same image with the new environment. Its receipt, event and history name the secret, never the value. It follows the release policy, so under `watch` it waits for a person. If another operation is running, the change goes out with the next release. Before the first release, a secret is only stored.

**Mirrors never get production secrets.** A mirror gets the person's own `.env.mirror`, for test keys: gitignored by `agc init`, never in a snapshot, hidden from the mirror's log. While a `[secrets] required` name isn't in it, `agc up` refuses before starting anything (`mirror.secret_missing`), naming the `NAME=<test value>` line to add. See [.env.mirror and the shell environment](/docs/build/env-mirror-and-shell-environment.md#envmirror-and-the-shell-environment).

**Shipping checks the list.** While a required name isn't set, `agc ship` refuses with `ship.secret_missing` before anything builds. Its `fix` carries the console link. Deleting a secret the live release requires is refused (`secret.required`) until a release that no longer requires it is live.

## Where a secret could leak

Not through anything Agent Cloud runs. The running app itself is the exposure:

| Path | What happens instead |
|---|---|
| The agent's chat | agc never takes a value; control refuses one sent with an agent's credential (`agent.not_allowed`) |
| A file in the repo | Values live on Agent Cloud, sealed, never in a build, snapshot, event, receipt or log line Agent Cloud writes |
| A read command | There isn't one, for anyone |
| The agent's mirror | Production values never reach it; it gets `.env.mirror` instead |

The running app has the secret because it needs it. Under the default `routine` autonomy, a code-only release that passes its checks goes live without a person. So code the agent wrote could read `process.env.STRIPE_KEY` and send it somewhere. If that matters, use `watch` and review each release. Rehearsals also get production's secrets, so the app's outbound calls with them are real: skip side effects when `AGC_RUN` is `rehearsal`.

## How secrets and credentials are stored

Each value is sealed and stored apart from the app's row, in `app_secrets (app, name, ciphertext, set_by, set_at)`. Only `GET /runner/v1/tasks/:id/env` opens secrets, for a release or rehearsal task, and only for the claim that holds the task. A cancelled op's task gets nothing. `agc secret ls` returns names, setter and time: no value, fingerprint, prefix or length leaves control.

**Sealing.** App secrets, connection credentials and launcher credentials are sealed with AES-256-GCM under `CONNECTIONS_KEY`. That 32-byte key is given to control as 64 hex characters or base64, and kept in the control plane's runtime secrets, outside the database. Each value gets a fresh 12-byte IV, and the stored form is `iv || tag || ciphertext`. Without the key, control refuses to store secrets (`secret.unavailable`, 503) and connections (`connections.not_configured`). The console shows a connection credential only as a fingerprint (for a key pair, Snowflake's own `RSA_PUBLIC_KEY_FP`).

**On the agent's machine.** agc hides the connection and secret values it holds for a mirror in anything it prints. A `NAME=value` line shows `NAME=[set by agent.cloud]`. Any value of 12 characters or more (3 under a credential-like name) is replaced wherever it appears, raw or JSON-escaped.

**Agent Cloud's own tokens** have fixed lifetimes. The first three are 24 random bytes after the prefix, stored as SHA-256 hashes:

| Credential | Lifetime |
|---|---|
| Agent token, `agc_` | Unclaimed: 7 days to be connected. Connected: 90 days after connecting, or 30 days without use, whichever comes first |
| Deploy key, `agcd_` | Chosen at creation, 1 to 90 days; the person is emailed within 7 days of it |
| Task token, `agct_` | 5 to 60 minutes (default 60); at most 20 live per key |
| Email token, `agce_` + payload + HMAC | Not stored, verified by HMAC; bound to one app and environment, and for a copy, to that copy's id |
| Approval id, `apr_` + 128 bits | A plain id: 24 hours, decided once |

Every request reads the credential's row, so revoking one takes effect on the next request. Ending a credential cancels its unadmitted, unapproved releases and deletes every copy issued to it. Ending a deploy key revokes every task token it minted. A sweep expires overdue credentials without waiting for them to call.

## Errors

Every code, its exit and fix: [Error code catalog](/docs/reference/error-code-catalog.md).

```json
{
  "ok": false,
  "product": "agent.cloud",
  "command": "secret set",
  "data": { "name": "STRIPE_KEY", "set_url": "https://agent.cloud/console/apps/camp-bookings/settings?secret=STRIPE_KEY#secrets" },
  "error": {
    "code": "secret.person_only",
    "message": "agc doesn’t take secret values; a person sets them in the console, and nothing was read.",
    "fix": "Give the person this link: https://agent.cloud/console/apps/camp-bookings/settings?secret=STRIPE_KEY#secrets",
    "retryable": false,
    "docs": "agc help secrets"
  },
  "next": [{ "cmd": "agc secret set STRIPE_KEY", "why": "the link for the person" }]
}
```

## Agent notes

- Never ask the person for a value in chat, and never write one to a file, a commit or `.env`, `.env.mirror` included: the person edits that file.
- `--generate` is for keys the app invents. A key from a provider (Stripe, OpenAI) always needs the person and the link.
- `ship.secret_missing` exits 1; its `fix` carries the console link for the person.
- Don't print the app's environment in its logs: the secrets are in it.

## Related

- [.env.mirror and the shell environment](/docs/build/env-mirror-and-shell-environment.md#envmirror-and-the-shell-environment)
- [What agents can and can't do](/docs/concepts/what-agents-can-and-cant-do.md)
- [Managing agents](/docs/owners/managing-agents.md)
- [Deploy keys](/docs/ship/ci-cd-with-deploy-keys.md#replacing-and-revoking-a-key)
- [Connections](/docs/owners/connections.md)

<!-- feedback:start -->
---
Wrong, outdated or unclear? Run `agc feedback docs "build/secrets: <what is wrong>" --body <file>` with the sentence quoted in the file. No code or app data is sent.
<!-- feedback:end -->
