> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fibre.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Secrets

> Store API keys, tokens and passwords once, encrypted, and give them to sandboxes by name: to the one command that needs a secret, or to every command in a sandbox. Values are never shown again.

Code in a sandbox often needs a secret: a model provider's API key for an agent, a token to clone a private
repository, a password for a test database. You could paste the value into every request, but then it lives in
your code, in your scripts and in your shell history.

Instead, **store the secret in Fibre once**, under a name such as `OPENAI_API_KEY`, and give it to a sandbox **by
name**. Fibre keeps the value encrypted and adds it as an environment variable only where you ask for it:

| You name the secret on | Who gets it |
| - | - |
| A command (`secrets` on a command) | **That command only.** The next command does not see it |
| A sandbox (`secrets` when you create it) | Every command that runs in that sandbox |

A sandbox that names no secret gets none. A stored value is **write-only**: once it is set, Fibre never shows it
again, not in the API, the SDKs or the CLI. You can only replace it or delete it.

## Store a secret

Give it a name in `UPPER_SNAKE_CASE`, which becomes the name of the environment variable, and the value. In the
CLI, `fibre secret set OPENAI_API_KEY` asks for the value without showing it, or reads it from a pipe, so the
value never appears on the command line:

```bash title="CLI" run theme={null}
printf %s "example-key-0123456789" | fibre secret set OPENAI_API_KEY
```

```typescript title="TypeScript" run theme={null}
import { Sandbox, Secret } from "@fibre-dev/sdk";

const secret = await Secret.set("OPENAI_API_KEY", "example-key-0123456789"); // in your code: read it from your own config
console.log(secret.name, secret.version); // OPENAI_API_KEY 1
```

```python title="Python" run theme={null}
from fibre import Sandbox, Secret

secret = Secret.set("OPENAI_API_KEY", "example-key-0123456789")  # in your code: read it from your own config
print(secret.name, secret.version)  # OPENAI_API_KEY 1
```

```bash title="curl" run theme={null}
curl -sS -X POST "$FIBRE_API_URL/v1/secrets" \
  -H "Authorization: Bearer $FIBRE_API_KEY" -H "Content-Type: application/json" \
  -d '{"name": "OPENAI_API_KEY", "value": "example-key-0123456789"}'
```

> [accordion] Response
>
> ```json title="response" expected theme={null}
> {
>   "name": "OPENAI_API_KEY",
>   "version": 1,
>   "created_at": "…",
>   "updated_at": "…"
> }
> ```

The answer has the name, a version and two times. It never has the value.

## Give a secret to one command

This is the safest way to use a secret, and the one to reach for first: **only the command that needs the secret
gets it**, and nothing else that runs in the sandbox before or after can read it.

First, a sandbox to work in. It names no secrets:

```bash title="CLI" run theme={null}
SBX=$(fibre create --lifetime 10m --label docs=secrets --json | jq -r .id)
```

```typescript title="TypeScript" run theme={null}
const sbx = await Sandbox.create({ lifetime: 600, labels: { docs: "secrets" } });
```

```python title="Python" run theme={null}
sbx = Sandbox.create(lifetime=600, labels={"docs": "secrets"})
```

```bash title="curl" run capture=SBX=.id theme={null}
curl -sS -X POST "$FIBRE_API_URL/v1/sandboxes" \
  -H "Authorization: Bearer $FIBRE_API_KEY" -H "Content-Type: application/json" \
  -d '{"lifetime_sec": 600, "labels": {"docs": "secrets"}}'
```

Now run a command that names the secret. Inside the sandbox it is an ordinary environment variable. This program
only reports how long the key is, because output is the one place a secret could leak by accident:

```bash title="CLI" run theme={null}
fibre exec "$SBX" --secret OPENAI_API_KEY -- \
  python3 -c "import os; print('key loaded:', len(os.environ['OPENAI_API_KEY']), 'characters')"
# key loaded: 22 characters

fibre exec "$SBX" -- sh -c 'echo "the next command: ${OPENAI_API_KEY:-no key}"'
# the next command: no key
```

```typescript title="TypeScript" run theme={null}
const check = "import os; print('key loaded:', len(os.environ['OPENAI_API_KEY']), 'characters')";
const withKey = await sbx.run(["python3", "-c", check], { secrets: ["OPENAI_API_KEY"] });
console.log(withKey.stdout); // key loaded: 22 characters

const next = await sbx.run('echo "the next command: ${OPENAI_API_KEY:-no key}"');
console.log(next.stdout); // the next command: no key
```

```python title="Python" run theme={null}
check = "import os; print('key loaded:', len(os.environ['OPENAI_API_KEY']), 'characters')"
with_key = sbx.run(["python3", "-c", check], secrets=["OPENAI_API_KEY"])
print(with_key.stdout)  # key loaded: 22 characters

nxt = sbx.run('echo "the next command: ${OPENAI_API_KEY:-no key}"')
print(nxt.stdout)  # the next command: no key
```

```bash title="curl" run theme={null}
CID=$(curl -sS -X POST "$FIBRE_API_URL/v1/sandboxes/$SBX/commands" \
  -H "Authorization: Bearer $FIBRE_API_KEY" -H "Content-Type: application/json" \
  -d '{"command": ["python3", "-c", "import os; print(\"key loaded:\", len(os.environ[\"OPENAI_API_KEY\"]), \"characters\")"], "secrets": ["OPENAI_API_KEY"]}' \
  | jq -r .command_id)

curl -sS "$FIBRE_API_URL/v1/sandboxes/$SBX/commands/$CID/logs?follow=1" -H "Authorization: Bearer $FIBRE_API_KEY"
```

> [accordion] Response
>
> ```text title="output" expected theme={null}
> key loaded: 22 characters
> ```

In the API, `secrets` works on [Start a command](https://docs.fibre.dev/api-reference/commands/start-a-command.md), alongside `env`. A name
cannot be both in `secrets` and in `env`.

## Give a secret to a whole sandbox

Some programs run for a long time and need a secret the whole time, such as a server you start in the background or
an agent that runs many commands. Name the secret when you **create** the sandbox, and every command in it gets the
value. The sandbox's details list the secrets it uses, by name only:

```bash title="CLI" run theme={null}
AGENT=$(fibre create --secret OPENAI_API_KEY --lifetime 10m --label docs=secrets --json | jq -r .id)
fibre get "$AGENT" --json | jq -c .secrets   # ["OPENAI_API_KEY"]

fibre exec "$AGENT" -- sh -c 'echo "key loaded: ${#OPENAI_API_KEY} characters"'   # key loaded: 22 characters
```

```typescript title="TypeScript" run theme={null}
const agent = await Sandbox.create({ secrets: ["OPENAI_API_KEY"], lifetime: 600, labels: { docs: "secrets" } });
console.log(agent.secrets); // [ 'OPENAI_API_KEY' ]

const inside = await agent.run('echo "key loaded: ${#OPENAI_API_KEY} characters"');
console.log(inside.stdout); // key loaded: 22 characters
```

```python title="Python" run theme={null}
agent = Sandbox.create(secrets=["OPENAI_API_KEY"], lifetime=600, labels={"docs": "secrets"})
print(agent.secrets)  # ['OPENAI_API_KEY']

inside = agent.run('echo "key loaded: ${#OPENAI_API_KEY} characters"')
print(inside.stdout)  # key loaded: 22 characters
```

```bash title="curl" run capture=AGENT=.id theme={null}
curl -sS -X POST "$FIBRE_API_URL/v1/sandboxes" \
  -H "Authorization: Bearer $FIBRE_API_KEY" -H "Content-Type: application/json" \
  -d '{"secrets": ["OPENAI_API_KEY"], "lifetime_sec": 600, "labels": {"docs": "secrets"}}'
```

> [accordion] Response
>
> ```json title="response" expected theme={null}
> {
>   "id": "…",
>   "status": "running",
>   "secrets": ["OPENAI_API_KEY"]
> }
> ```

Fibre stores only the **names** with the sandbox. It reads the values each time the sandbox starts, so they are
never saved with it.

> [!WARNING]
>
> **A snapshot keeps a sandbox's memory, and memory includes what its running programs hold.** If a program in the
> sandbox had a secret when you saved a [snapshot](https://docs.fibre.dev/setup/snapshots.md), copies of that snapshot start with that program
> and its secret. Save snapshots before your programs load secrets, or from a sandbox that never had them, and name
> secrets on the copies instead.

## Change or delete a secret

To change a value, **set it again** with the same name. Its version goes up, and from then on:

* **Commands that name it** get the new value straight away.
* **New sandboxes that name it** start with the new value.
* **A sandbox that got it when it was created** keeps the value it started with. Create a new sandbox, or name the
  secret on its commands, to use the new one.

You can list your secrets at any time. You see names, versions and times, never values:

```bash title="CLI" run theme={null}
printf %s "example-key-9876543210" | fibre secret set OPENAI_API_KEY   # version 2
fibre secret list
```

```typescript title="TypeScript" run theme={null}
await Secret.set("OPENAI_API_KEY", "example-key-9876543210");
for (const s of await Secret.list()) console.log(s.name, s.version); // OPENAI_API_KEY 2
```

```python title="Python" run theme={null}
Secret.set("OPENAI_API_KEY", "example-key-9876543210")
for s in Secret.list():
    print(s.name, s.version)  # OPENAI_API_KEY 2
```

```bash title="curl" run theme={null}
curl -sS -X POST "$FIBRE_API_URL/v1/secrets" -H "Authorization: Bearer $FIBRE_API_KEY" -H "Content-Type: application/json" \
  -d '{"name": "OPENAI_API_KEY", "value": "example-key-9876543210"}' > /dev/null

curl -sS "$FIBRE_API_URL/v1/secrets" -H "Authorization: Bearer $FIBRE_API_KEY"
```

> [accordion] Response
>
> ```json title="response" expected theme={null}
> {
>   "secrets": [
>     {
>       "name": "OPENAI_API_KEY",
>       "version": 2,
>       "created_at": "…",
>       "updated_at": "…"
>     }
>   ]
> }
> ```

When you no longer need a secret, delete it. Commands that name it from then on are refused with
`secret_not_found`, and sandboxes that named it start without it. Here we also clean up the two sandboxes:

```bash title="CLI" run theme={null}
fibre delete "$SBX" "$AGENT"
fibre secret delete OPENAI_API_KEY
```

```typescript title="TypeScript" run theme={null}
await sbx.delete();
await agent.delete();
await (await Secret.get("OPENAI_API_KEY")).delete();
```

```python title="Python" run theme={null}
sbx.delete()
agent.delete()
Secret.get("OPENAI_API_KEY").delete()
```

```bash title="curl" run theme={null}
curl -sS -o /dev/null -X DELETE "$FIBRE_API_URL/v1/sandboxes/$SBX" -H "Authorization: Bearer $FIBRE_API_KEY"
curl -sS -o /dev/null -X DELETE "$FIBRE_API_URL/v1/sandboxes/$AGENT" -H "Authorization: Bearer $FIBRE_API_KEY"
curl -sS -X DELETE "$FIBRE_API_URL/v1/secrets/OPENAI_API_KEY" -H "Authorization: Bearer $FIBRE_API_KEY"
```

> [accordion] Response
>
> ```json title="response" expected theme={null}
> {
>   "name": "OPENAI_API_KEY",
>   "deleted": true
> }
> ```

## Who can use secrets

Naming a secret puts its value into a sandbox, so Fibre treats it like reading the secret:

| Key | Can set, list and delete secrets | Can name secrets on a sandbox or a command |
| - | - | - |
| Your account's key, or a key with the `secrets.*` scope | Yes | Yes |
| A key with `secrets.read` | List only | Yes |
| A key without a `secrets` scope | No | No: `insufficient_scope` |
| **A key for one sandbox** (made with `resource`) | No | **No**: `key_resource_bound` |

The last row matters for agents. An agent working inside a sandbox should hold a
[key for that one sandbox](https://docs.fibre.dev/get-started/api-keys.md#a-key-for-one-sandbox). With it, the agent can run its own commands,
but it **cannot pull any of your secrets** into its sandbox. Give the agent the secrets it needs yourself, when you
create its sandbox or start its commands, from your own code.

## Good practice

* **Name secrets on the command that needs them**, rather than on the whole sandbox, so that nothing else in the
  sandbox can read them.
* **Never print a secret.** Command output is returned to whoever runs the command and may end up in logs, and in a
  model's context when an agent runs it.
* **Remember that code in a sandbox can send what it can read.** For untrusted or model-written code that uses a
  secret, also limit the sandbox's [network](https://docs.fibre.dev/production/network.md) to the services the secret is for, such as
  `api.openai.com`.
* **Use a separate key per purpose**, such as one model key for your agents and another for evals, so you can
  change or revoke one without touching the others.
* **Change secrets from time to time**, and at once if one may have leaked: set the new value, then revoke the old
  one at its provider.

## Limits

| | |
| - | - |
| Name | `UPPER_SNAKE_CASE`: capital letters, digits and `_`, starting with a letter or `_`; up to 255 characters |
| Value | 1 to 65,536 characters of text |
| Names per sandbox or per command | Up to 50 |
| Errors | `secret_not_found` (with the `missing` names), `validation_failed`, `insufficient_scope`, `key_resource_bound` |

See the [API reference](https://docs.fibre.dev/api-reference/secrets/set-a-secret.md) for every field.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.