# Secret

> Secrets: named environment values for sandboxes, agents and apps.

```python
openai = noclick.Secret.from_name("openai", values={"OPENAI_API_KEY": "sk-..."}, create_if_missing=True)
with noclick.Sandbox.create(secrets=[openai]) as sb:
    sb.exec("python summarize.py")   # reads os.environ["OPENAI_API_KEY"]
```

A `Connection` is an account at a service NoClick calls for you, with tokens it
keeps fresh; a `Secret` is raw values for code you run. Values are write-only:
`keys` names them, and nothing reads one back.

## noclick.Secret

```python
class Secret
```

Named values, such as API keys, that the code you run reads from its
environment: a sandbox's commands, an agent's shell, an app. Each key
becomes an environment variable of that name. Values are write-only.

| Attribute | Type | Description |
| --- | --- | --- |
| `id` | `str` |  |
| `name` | `str` |  |
| `keys` | `list[str]` | The names it holds, in order; never their values. |
| `user_id` | `str | None` |  |
| `created_at` | `Any` |  |
| `updated_at` | `Any` | When its values last changed. |
| `used_by` | `list[dict[str, Any]]` | Who reads it: `kind` (`agent`, a running `sandbox` or a `project`), `id`, `name` (None for a sandbox) and, for an app, the `binding` name. |
| `console_url` | `str | None` | This secret in the developer console (None off the hosted API). |

### Secret.create

```python
noclick.Secret.create(
    name, values=None, *, allow_existing=False, user=..., client=None
) -> Secret
```

Create a secret called `name` (1-64 letters, digits and `. _ -`),
unique to its user (a taken name raises `noclick.Conflict` with
`existing_id`, or with `allow_existing=True` returns the secret already there, its
values untouched), holding `values`: up to 100 keys, each an environment
variable name (letters, digits and `_`, not starting with a digit; not
`PATH`, `HOME` or another the process sets, nor one starting `LD_`,
`NC_` or `NOCLICK_`), each value a non-empty string of up to 32,768 characters.
Without `values` it starts empty, for someone to fill in from the console.

Parameters:

- `name` (`str`, required)
- `values` (`Mapping[str, str] | None`, default `None`)
- `allow_existing` (`bool`, default `False`)
- `user` (`Any`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: `Secret` The new secret; its `keys` name what it holds.

Usage:

Store a key from your environment:

```python
import os

shipping = noclick.Secret.create("shipping", {"SHIPPING_API_KEY": os.environ["SHIPPING_API_KEY"]})
print(shipping.keys)   # ['SHIPPING_API_KEY']
```

Store it the first time, and keep whatever someone pasted in the console after:

```python
import os

shipping = noclick.Secret.create(
    "shipping", {"SHIPPING_API_KEY": os.environ["SHIPPING_API_KEY"]}, allow_existing=True,
)
```

### Secret.from_name

```python
noclick.Secret.from_name(
    name, *, values=None, create_if_missing=False, user=..., client=None
) -> Secret
```

A lazy reference to the secret called `name`. Acting for a user, that
user's own secret is found first, then the account's. Given `values`, it gets the
current (or given) user's own and sets the keys in `values` on every run (NoClick
compares them, so unchanged values change nothing). Keys it doesn't name are kept: remove
one with `update`. `create_if_missing=True` creates it, holding `values`, when
there's none; without it, a missing secret raises `noclick.NotFound`.

Parameters:

- `name` (`str`, required)
- `values` (`Mapping[str, str] | None`, default `None`)
- `create_if_missing` (`bool`, default `False`)
- `user` (`Any`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: `Secret` A secret that loads (or is created) the first time it's used. Nothing is sent yet.

Usage:

Create it from your environment on the first run, and pick up a rotated key on the next:

```python
import os

openai = noclick.Secret.from_name(
    "openai", values={"OPENAI_API_KEY": os.environ["OPENAI_API_KEY"]}, create_if_missing=True,
)
with noclick.Sandbox.create(secrets=[openai]) as sb:
    sb.exec("python summarize.py")   # reads os.environ["OPENAI_API_KEY"]
```

Or declare it empty and paste the values in the console:

```python
erp = noclick.Secret.from_name("acme-erp", create_if_missing=True, user="acme")
print(erp.console_url)
```

### Secret.from_id

```python
noclick.Secret.from_id(secret_id, *, client=None, user=...) -> Secret
```

A lazy reference to a secret by id.

Parameters:

- `secret_id` (`str`, required)
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.
- `user` (`Any`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).

Returns: `Secret` A secret that loads the first time you read a field. Nothing is sent yet.

Usage:

```python
openai = noclick.Secret.from_id("2a7f0c3e-9d1b-4e6a-8c5f-3b0e7d2a9c14")
print(openai.keys)
```

### Secret.get

```python
noclick.Secret.get(secret_id, *, user=..., client=None) -> Secret
```

Load a secret by id now; an unknown id raises `noclick.NotFound`.

Parameters:

- `secret_id` (`str`, required)
- `user` (`Any`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: `Secret` The secret, loaded: its key names, when they last changed, and who uses it. Never its values.

Usage:

```python
openai = noclick.Secret.get("2a7f0c3e-9d1b-4e6a-8c5f-3b0e7d2a9c14")
print(openai.name, openai.keys, openai.updated_at)
```

### Secret.list

```python
noclick.Secret.list(
    *, limit=None, user_filter=None, user=..., client=None
) -> list[Secret]
```

The current user's secrets and the account's (or those `user_filter` covers).

Parameters:

- `limit` (`int | None`, default `None`): Return at most this many.
- `user_filter` (`Any`, default `None`): Which users a read covers: `noclick.ALL_USERS`, `noclick.NO_USER`, a `User`, or your id for one. It filters without acting as that user.
- `user` (`Any`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: `list[Secret]` Every matching secret, newest first, fetched page by page, with key names and never values.

Usage:

```python
for secret in noclick.Secret.list():
    print(secret.name, secret.keys, secret.user_id)   # user_id is None for the account's

everyone = noclick.Secret.list(user_filter=noclick.ALL_USERS)   # every user's, plus your account's
```

### Secret.list_page

```python
noclick.Secret.list_page(
    *, cursor=None, limit=None, user_filter=None, user=..., client=None
) -> Page
```

One page of secrets; pass `next_cursor` back as `cursor`.

Parameters:

- `cursor` (`str | None`, default `None`): The `next_cursor` of the page before; omit it for the first page.
- `limit` (`int | None`, default `None`): Return at most this many.
- `user_filter` (`Any`, default `None`): Which users a read covers: `noclick.ALL_USERS`, `noclick.NO_USER`, a `User`, or your id for one. It filters without acting as that user.
- `user` (`Any`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: `Page` A page whose `data` holds the secrets and whose `next_cursor` is `None` on the last page.

Usage:

```python
page = noclick.Secret.list_page(limit=50)
while True:
    for secret in page.data:
        print(secret.name, secret.keys)
    if page.next_cursor is None:
        break
    page = noclick.Secret.list_page(limit=50, cursor=page.next_cursor)
```

### Secret.update

```python
secret.update(values, *, replace=False) -> Secret
```

Set values. By default they merge: the keys you pass are set, a key
set to `None` is removed, and the rest are kept. `replace=True` makes
the secret hold exactly `values`. New sandboxes see the change at once,
agents from their next turn, and apps at their next deployment.

Parameters:

- `values` (`Mapping[str, str | None]`, required)
- `replace` (`bool`, default `False`)

Returns: `Secret` This secret, with its new `keys` and `updated_at`.

Usage:

Rotate one key and keep the rest:

```python
import os

openai = noclick.Secret.from_name("openai")
openai.update({"OPENAI_API_KEY": os.environ["NEW_OPENAI_API_KEY"]})
```

Remove a key, or replace every value at once:

```python
erp = noclick.Secret.from_name("acme-erp", user="acme")
erp.update({"ERP_TOKEN": None})
erp.update({"ERP_USER": "svc-noclick", "ERP_PASSWORD": "..."}, replace=True)
print(erp.keys)   # ['ERP_PASSWORD', 'ERP_USER']
```

### Secret.versions

```python
secret.versions() -> list[Version]
```

What it held before each change of the last 30 days, newest first: when each was replaced and the
keys it held, never the values.

Returns: `list[Version]` Each earlier version.

Usage:

```python
stripe = noclick.Secret.from_name("stripe")
for version in stripe.versions():
    print(version.replaced_at, version.keys)
```

### Secret.restore_version

```python
secret.restore_version(version) -> Secret
```

Hold `version`'s keys and values again (the values it holds now are kept as a version in turn).
Nothing is shown or sent: the earlier values come back sealed.

Parameters:

- `version` (`Any`, required)

Returns: `Secret` This secret, holding them.

Usage:

Roll back a key that was rotated by mistake:

```python
stripe = noclick.Secret.from_name("stripe")
stripe.restore_version(stripe.versions()[0])
```

### Secret.delete

```python
secret.delete() -> None
```

Delete the secret and its values, for good (its earlier versions go with it).
While an agent lists it or an app binds it (`used_by`), this raises
`noclick.Conflict`; sandboxes already running keep their environment until they end.

Usage:

```python
noclick.Secret.from_name("old-provider").delete()
```

## noclick.Version

```python
class Version
```

A value a secret or setting held before `replaced_at`. `op` says what replaced it: `changed` (a new
value) or `deleted`. A secret's version never shows its value: `keys` names what a `noclick.Secret` held
and `environment` is where an app's secret was set; a setting's shows its `value`.

| Attribute | Type | Description |
| --- | --- | --- |
| `id` | `str` |  |
| `name` | `str` |  |
| `op` | `str` |  |
| `replaced_at` | `Any` |  |
| `environment` | `str | None` |  |
| `keys` | `list[str] | None` |  |
| `value` | `str | None` |  |
| `raw` | `dict[str, Any]` | The response JSON exactly as the API sent it. |
