# Secrets

> Store API keys and tokens, shown once and masked after.

Page: https://uicontext.com/docs/components/secrets

Group: Settings.

## When to use

Store API keys and tokens, shown once and masked after. Use [Connections](https://uicontext.com/docs/components/connections.md) instead when the service connects with a sign-in.

## Usage

```ts
import { SecretValues, Vault, type VaultItem } from "./ui/uicontext/components/Secret.ts"

const Message = UiSchema.TaggedEnum({
	ReplacedSecret: { id: Schema.String, values: SecretValues },
	RotatedSecret: { id: Schema.String },
	RemovedSecret: { id: Schema.String },
})

export const Secrets = (h: Html.H<typeof Message.Type>, items: ReadonlyArray<VaultItem>) =>
	Vault(h, {
		props: { label: "Secrets", items },
		onEmit: {
			RequestedReplace: ({ id, values }) => Message.ReplacedSecret({ id, values }),
			RequestedRotate: ({ id }) => Message.RotatedSecret({ id }),
			RequestedRemove: ({ id }) => Message.RemovedSecret({ id }),
			RequestedAdd: () => undefined,
		},
	})
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### Rotate, replace and scope

Values are never shown again. Rotate makes a new one, shown once to copy; scopes and expiry say where and how long each applies.

```ts title="src/demo.ts" source="www/src/examples/secrets/demo.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Redacted, Schema } from "effect"
import { SecretField, SecretValues, ShownOnce, Vault, VaultItem } from "./ui/uicontext/components/Secret.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"

const styles = stylex.create({ frame: { inlineSize: "min(100%, 40rem)", display: "flex", flexDirection: "column", gap: 16 } })

const Message = UiSchema.TaggedEnum({
	Rotated: { id: Schema.String },
	Removed: { id: Schema.String },
	Replaced: { id: Schema.String },
	Done: {},
	RequestedAddSecret: {},
	SavedNewSecret: { values: SecretValues },
	CancelledNewSecret: {},
})

const maya = { id: "maya", name: "Maya Chen", kind: "person" } as const
const seed: ReadonlyArray<VaultItem> = [
	{
		id: "billing", name: "BILLING_API_KEY", kind: "API key", scopes: ["Production"], expires: "Expires Jan 14, 2027",
		saved: { at: "2026-10-14T09:12:00Z", actor: maya, hint: "ends in 4f2a" }, usedBy: "Used by 3 workflows", can: ["rotate", "remove"],
		rotateWarning: "The old key keeps working for 24 hours, then the 3 workflows that use it need the new one.",
	},
	{
		id: "webhook", name: "WEBHOOK_SIGNING_SECRET", kind: "Signing secret", scopes: ["Production", "Preview"],
		saved: { at: "2026-11-02T16:40:00Z", actor: maya, hint: "ends in 91c0" }, can: ["replace", "rotate", "remove"],
	},
	{
		id: "legacy", name: "LEGACY_CRM_TOKEN", kind: "Token", scopes: ["Preview"], expires: "Expired Nov 28", expired: true,
		saved: { at: "2026-05-30T10:00:00Z", hint: "ends in 0b17" }, removeWarning: "Nothing uses it any more.", can: ["replace", "remove"],
	},
]

export const ProjectSecrets = Html.component({
	name: "ProjectSecrets",
	model: Schema.Struct({ items: Schema.Array(VaultItem), fresh: Schema.NullOr(Schema.String), adding: Schema.Boolean, added: Schema.Number }),
	message: Message,
}).make({
	init: () => ({ items: seed, fresh: null, adding: false, added: 0 }),
	update: {
		// a real app asks its server for the new value; here it's made up
		Rotated: ($, { id }) =>
			$.evolve({
				fresh: id,
				items: (items) => items.map((i) => (i.id === id ? { ...i, saved: { at: "2026-12-01T10:00:00Z", actor: maya, hint: "ends in 7d3e" } } : i)),
			}),
		Removed: ($, { id }) => $.evolve({ items: (items) => items.filter((i) => i.id !== id) }),
		Replaced: ($, { id }) => $.evolve({ items: (items) => items.map((i) => (i.id === id ? { ...i, saved: { at: "2026-12-01T10:00:00Z", actor: maya } } : i)) }),
		Done: ($) => $.evolve({ fresh: null }),
		RequestedAddSecret: ($) => $.evolve({ adding: true }),
		CancelledNewSecret: ($) => $.evolve({ adding: false }),
		// a real app sends the value to its vault and keeps only a hint; the name is plain text
		SavedNewSecret: ($, { values }) => {
			const name = values["name"] === undefined ? "" : Redacted.value(values["name"]).trim().toUpperCase()
			const value = values["value"] === undefined ? "" : Redacted.value(values["value"])
			return $.evolve({
				adding: false,
				added: (n) => n + 1,
				items: (items) => [
					{
						id: `new-${$.model.added + 1}`,
						name: name === "" ? "UNNAMED_SECRET" : name,
						kind: "API key",
						scopes: ["Production"],
						saved: { at: "2026-12-01T10:00:00Z", actor: maya, hint: `ends in ${value.slice(-4)}` },
						can: ["replace", "rotate", "remove"],
					},
					...items,
				],
			})
		},
	},
	view: (h, model) =>
		h.div.attrs(sx(styles.frame)).children(
			model.adding &&
				SecretField(h, {
					key: `new-${model.added}`,
					props: {
						agentId: "vault-demo-new",
						label: "New secret",
						saved: null,
						inputs: [
							{ name: "name", label: "Name", secret: false, placeholder: "STRIPE_API_KEY", autocomplete: "off" },
							{ name: "value", label: "Value" },
						],
						cancel: { label: "Cancel" },
					},
					onEmit: {
						RequestedSave: ({ values }) => Message.SavedNewSecret({ values }),
						RequestedRemove: () => undefined,
						RequestedCancel: () => Message.CancelledNewSecret(),
					},
				}),
			model.fresh !== null &&
				ShownOnce(h, {
					key: model.fresh,
					props: {
						label: `New ${model.items.find((i) => i.id === model.fresh)?.name ?? "value"}`,
						value: Redacted.make("bk_live_9f2c41aa07d34e1b8c55e0d17d3e"),
						description: "Copy it now and paste it where the old one was. It won’t be shown again.",
						doneLabel: "Done",
					},
					onEmit: { RequestedDone: () => Message.Done() },
				}),
			Vault(h, {
				props: { label: "Secrets", agentId: "vault-demo", items: model.items, addLabel: "Add secret", timeZone: "UTC" },
				onEmit: {
					RequestedAdd: () => Message.RequestedAddSecret(),
					RequestedReplace: ({ id }) => Message.Replaced({ id }),
					RequestedRemove: ({ id }) => Message.Removed({ id }),
					RequestedRotate: ({ id }) => Message.Rotated({ id }),
				},
			}),
		),
})
```

## API reference

### SecretField (component)

**Props**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `agentId` | `string` | — | The field's unique ID for agents, such as "secret-service-key". |
| `label` (required) | `string` | — | The secret's display name, such as "Service API key". |
| `description` | `string` | — | What it's for, or where it goes: "Used by sessions during setup only." |
| `saved` (required) | `SavedSecret \| null` | — | Null while nothing is saved |
| `usedBy` | `string` | — | What uses it: "Used by 2 preflight scripts" |
| `inputs` | `SecretInput[]` | — | The inputs; one masked input named "value" by default |
| `can` | `Can(["edit", "remove"])` | — | What this person may do: "edit" enters or replaces the value, "remove" removes the saved one. Left out, they may edit and not remove. |
| `lockedReason` | `string` | — | Why it can't be changed, when it can't: "Set by your admin" |
| `removeWarning` | `string` | — | Said in the remove confirm: "Sessions that use it fail until it's replaced." |
| `saveLabel` | `string` | `Save securely` | The save button's words; `Save securely` by default |
| `removeLabel` | `string` | `Remove` | The remove button's words; `Remove` by default |
| `note` | `string` | `"` | Said under the input; "Saved straight to the vault. Nobody can view it after you save it." by default |
| `outcome` | `Outcome` | — | The app's answer to the last request: "save" for `RequestedSave`, "remove" for `RequestedRemove`. Pending shows "Saving securely…"; a refusal shows its reason ("The secret was not saved. Try again."). |
| `expires` | `string` | — | A countdown, formatted by the app: "Expires in 4:12" |
| `closed` | `string` | — | The entry is over (expired, cancelled, used); this sentence replaces the form |
| `cancel` | `{ label }` | — | A Cancel that the app hears (stop a sign-in), in place of the local one |
| `timeZone` | `string` | — | The time zone (IANA) the saved date is shown in |
| `locale` | `string` | — | The locale the saved date is formatted in; the browser's by default |

**Events**

| Event | Payload | Description |
| --- | --- | --- |
| `RequestedSave` | `{ values: SecretValues }` | The values typed, by input name; the app sends them to its vault and drops them |
| `RequestedRemove` | `{}` | Remove the saved value, after the person confirmed |
| `RequestedCancel` | `{}` | The app's Cancel (the `cancel` prop) was pressed |

**Agent actions**

| Action | Description |
| --- | --- |
| `ClickedReplace` | Open the field so the person can type a new value. You never see or type the value: tell the person it's ready for them. |
| `ConfirmedRemove` (asks first) | Remove the saved value. Name its label and savedAt as the target. |

### Vault (component)

**Props**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `agentId` | `string` | `vault:<label>` | The id agents know this vault by; `vault:<label>` by default |
| `label` (required) | `string` | — | "Secrets", "Vault", "Environment variables" |
| `items` (required) | `VaultItem[]` | — | The saved secrets, by name, with who saved them and when; never their values |
| `addLabel` | `string` | — | "Add secret"; leave out when this person can't add |
| `emptyTitle` | `string` | `Nothing saved yet` | What an empty vault says; `Nothing saved yet` by default |
| `emptyHint` | `string` | — | A hint under the empty title |
| `note` | `string` | — | Said under the input when replacing a value |
| `timeZone` | `string` | — | The time zone (IANA) saved dates are shown in |
| `locale` | `string` | — | The locale saved dates are formatted in; the browser's by default |

**Events**

| Event | Payload | Description |
| --- | --- | --- |
| `RequestedAdd` | `{}` | The add button was pressed: open the app's form for a new secret |
| `RequestedReplace` | `{ id: string; values: SecretValues }` | A secret's new value, typed by input name; the app sends it to its vault and drops it |
| `RequestedRemove` | `{ id: string }` | Remove a secret, after the person confirmed |
| `RequestedRotate` | `{ id: string }` | Make a new value for a secret, after the person confirmed; show it with `ShownOnce` |

**Agent actions**

| Action | Description |
| --- | --- |
| `ClickedReplace` | Open an item's replace field for the person to type into. You never see or type the value: tell the person it's ready. |
| `ConfirmedRemove` (asks first) | Remove an item by id. Name that item's { id, name, savedAt }, as in the snapshot, as the target. |

### ShownOnce (component)

**Props**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `agentId` | `string` | `shown-once:<label>` | The id agents know this by; `shown-once:<label>` by default |
| `label` (required) | `string` | — | "Personal access token", "Connection token" |
| `value` (required) | `Redacted` | — | The new token, shown this once |
| `description` | `string` | — | What to do with it: "Paste it into your client's settings." |
| `doneLabel` | `string` | — | "Done"; leave out when the app closes it some other way |

**Events**

| Event | Payload | Description |
| --- | --- | --- |
| `RequestedDone` | `{}` | The person pressed Done: close it, and the value is gone |

## Keyboard and accessibility

- The value is a masked input with a Show button (`aria-pressed`); nothing typed is kept after it's sent.
- Saved secrets show their name and who saved them, never their value.
- A value shown once is selected when focused, and copying it says whether it worked.

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/secrets
```

With npm: `npx shadcn@latest add checkfu/uicontext/secrets`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/secrets`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/secrets`.

It writes these files:

- `src/ui/uicontext/components/Button.ts`
- `src/ui/uicontext/components/FormControls.ts`
- `src/ui/uicontext/components/IconStroke.ts`
- `src/ui/uicontext/components/Secret.ts`
- `src/ui/uicontext/components/ShowText.ts`
- `src/ui/uicontext/components/States.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/agent/Agent.ts`
- `src/ui/uicontext/components/button.stylex.ts`
- `src/ui/uicontext/components/domain/Actor.ts`
- `src/ui/uicontext/components/domain/Outcome.ts`
- `src/ui/uicontext/components/domain/Permission.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`
- `src/ui/uicontext/components/variants.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/Secret.ts](https://github.com/checkfu/uicontext/blob/main/src/Secret.ts)

## Used in

- [API keys](https://uicontext.com/blocks/api-keys.md)

## Related

- [Sign-in Form](https://uicontext.com/docs/components/sign-in-form.md) (`sign-in-form`): Sign in, accept an invitation and manage sessions.
- [Settings](https://uicontext.com/docs/components/settings.md) (`settings`): A settings page with sections, fields and a save bar.
- [Env Editor](https://uicontext.com/docs/components/env-editor.md) (`env-editor`): Names and values such as API keys and URLs, one row each: secrets hidden, a pasted .env file split into rows, mistakes marked.
- [Setting List](https://uicontext.com/docs/components/setting-list.md) (`setting-list`): Settings as rows, each with its control.
- [Inherited Settings](https://uicontext.com/docs/components/inherited-settings.md) (`inherited-settings`): Settings that a workspace sets and a team or project can override.
- [Members](https://uicontext.com/docs/components/members.md) (`members`): Workspace members, their roles and pending invitations.
