# 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.

## Secret

```typescript
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: `keys` names them, and nothing reads one back. (A `Connection` is an
account at a service NoClick calls for you; a `Secret` is raw values for code you run.)

| Attribute | Type | Description |
| --- | --- | --- |
| `id` | `string` |  |
| `name` | `string` |  |
| `keys` | `string[]` | The names it holds; never their values. |
| `userId` | `string | null` |  |
| `createdAt` | `Date | null` |  |
| `updatedAt` | `Date | null` | When its values last changed. |
| `usedBy` | `UsedBy[]` | Who reads it: agents, running sandboxes, and apps (with the `binding` name). |
| `consoleUrl` | `string | null` | This secret in the developer console (null off the hosted API). |

Types:

```typescript
/** An app that binds a database or a bucket, under its `binding` name. */
interface UsedBy {
  kind: string;
  id: string;
  name: string;
  binding: string | null;
}
```

### Secret.create

```typescript
static create(
  name: string,
  options?: SecretCreateOptions & AllowExisting,
): Promise<Secret>
```

Create a secret, unique by name per user (a taken name throws `Conflict` with `existingId`, or with
`allowExisting: true` returns the secret already there, its values untouched).

Parameters:

- `name` (`string`, required)
- `options.allowExisting` (`boolean`): A taken name returns the object already there, as it is: nothing about it changes.
- `options.user` (`UserOption`, default: the current user): `undefined` = the ambient user (`asUser`), `null` = the account itself.
- `options.client` (`NoClick`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.
- `options.values` (`Record<string, string>`): Up to 100 keys, each an environment variable name (not `PATH`, `HOME` or one starting `LD_`, `NC_` or `NOCLICK_`), each value a non-empty string of up to 32,768 characters.

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

Types:

```typescript
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}
```

### Secret.fromName

```typescript
static fromName(
  name: string,
  options?: SecretCreateOptions & { createIfMissing?: boolean },
): Secret
```

A lazy reference to the secret called `name`: the user's own 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.
`createIfMissing` creates it, holding `values`, when there's none; without it, a missing secret throws
`NotFound`.

Parameters:

- `name` (`string`, required)
- `options.values` (`Record<string, string>`): Up to 100 keys, each an environment variable name (not `PATH`, `HOME` or one starting `LD_`, `NC_` or `NOCLICK_`), each value a non-empty string of up to 32,768 characters.
- `options.createIfMissing` (`boolean`)
- `options.user` (`UserOption`, default: the current user): `undefined` = the ambient user (`asUser`), `null` = the account itself.
- `options.client` (`NoClick`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

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

Types:

```typescript
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}
```

### Secret.fromId

```typescript
static fromId(secretId: string, options?: CallOptions): Secret
```

A lazy reference to a secret by id.

Parameters:

- `secretId` (`string`, required)
- `options.client` (`NoClick`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.
- `options.user` (`UserOption`, default: the current user): `undefined` = the ambient user (`asUser`), `null` = the account itself.

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

Types:

```typescript
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}
```

### Secret.get

```typescript
static get(secretId: string, options?: CallOptions): Promise<Secret>
```

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

Parameters:

- `secretId` (`string`, required)
- `options.user` (`UserOption`, default: the current user): `undefined` = the ambient user (`asUser`), `null` = the account itself.
- `options.client` (`NoClick`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

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

Types:

```typescript
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}
```

### Secret.list

```typescript
static list(options?: ReadOptions & { limit?: number }): Promise<Secret[]>
```

The current user's secrets and the account's (or those `userFilter` covers), names only.

Parameters:

- `options.limit` (`number`): Return at most this many.
- `options.userFilter` (`UserFilter`): Which users the read covers (`user_id` query), instead of acting as one (`user`).
- `options.user` (`UserOption`, default: the current user): `undefined` = the ambient user (`asUser`), `null` = the account itself.
- `options.client` (`NoClick`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: Every matching secret, newest first, fetched page by page, with key names and never values.

Types:

```typescript
/**
 * Which users a read covers: `ALL_USERS`, `NO_USER`, a `User` or your id for one. Sent as the
 * `user_id` query parameter, never as the `NoClick-User` header, so it filters without acting as the user.
 */
type UserFilter = typeof ALL_USERS | typeof NO_USER | UserRef;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}

/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;
```

### Secret.listPage

```typescript
static listPage(options?: ReadOptions & PageOptions): Promise<Page<Secret>>
```

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

Parameters:

- `options.cursor` (`string | null`): The `next_cursor` of the page before; omit it for the first page.
- `options.limit` (`number`): Return at most this many.
- `options.userFilter` (`UserFilter`): Which users the read covers (`user_id` query), instead of acting as one (`user`).
- `options.user` (`UserOption`, default: the current user): `undefined` = the ambient user (`asUser`), `null` = the account itself.
- `options.client` (`NoClick`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

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

Types:

```typescript
/**
 * Which users a read covers: `ALL_USERS`, `NO_USER`, a `User` or your id for one. Sent as the
 * `user_id` query parameter, never as the `NoClick-User` header, so it filters without acting as the user.
 */
type UserFilter = typeof ALL_USERS | typeof NO_USER | UserRef;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}

/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;
```

### Secret.update

```typescript
update(
  values: Record<string, string | null>,
  options?: { replace?: boolean },
): Promise<this>
```

Set values. They merge by default: the keys you pass are set, a key set to `null` is removed,
the rest are kept. `replace` makes it hold exactly `values`. New sandboxes see the change at
once, agents from their next turn, apps at their next deployment.

Parameters:

- `values` (`Record<string, string | null>`, required)
- `options.replace` (`boolean`)

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

### Secret.versions

```typescript
versions(): Promise<Version[]>
```

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

Returns: Each earlier version.

### Secret.restoreVersion

```typescript
restoreVersion(version: Version | string): Promise<this>
```

Hold `version`'s keys and values again; they come back sealed, never shown or sent.

Parameters:

- `version` (`Version | string`, required)

Returns: This secret, holding them.

### Secret.delete

```typescript
delete(): Promise<void>
```

Delete the secret for good (its versions go with it); while an agent lists it or an app binds it, this throws `Conflict`.

## Version

```typescript
interface Version
```

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

| Attribute | Type | Description |
| --- | --- | --- |
| `id` | `string` |  |
| `name` | `string` |  |
| `op` | `"changed" | "deleted"` |  |
| `replacedAt` | `Date | null` |  |
| `environment` | `string | null` |  |
| `keys` | `string[] | null` |  |
| `value` | `string | null` |  |
| `raw` | `JsonObject` | The response JSON exactly as the API sent it. |

Types:

```typescript
type JsonObject = { [key: string]: unknown };
```
