API key field

Enter and manage an API key for an AI provider. Empty, it is a password field with a reveal toggle; with a key saved, it shows the key the way your server masked it ("sk-…a1b2") with Replace and Remove. ds::provider-keys is a panel of them, one per provider.

API key field

Needs Alpine and the package's script for the reveal toggle, Remove's confirmation and Test's test event. See getting started. Without them it is an ordinary form: the key posts, Test posts intent=test, and Remove stays hidden behind its first press, which fails safe.

Security

The component never receives, renders or stores a key.

  • It takes hint, a masked form your server makes ("sk-…a1b2"), never the key. A value is refused with an exception.
  • The input is always empty on the page. Its reveal toggle shows only what the reader is typing now, and hides it again when the form submits.
  • autocomplete="off", spellcheck="false", autocapitalize="off" and autocorrect="off" are set, so the browser neither offers to save it nor sends it to a spelling service. The password managers' opt-out attributes (data-1p-ignore, data-lpignore, data-bwignore) are written too, as a courtesy: none of them is a contract, so do not rely on them.
  • Nothing is written to storage or logged by the script.

Store keys encrypted (Laravel's encrypted cast), make the hint on the server, and check a key only on the server.

Use it

<x-ds::api-key-field
    provider="OpenAI"
    name="openai_key"
    :hint="$team->openai_key ? Str::mask($team->openai_key, '…', 3, -4) : null"
    :status="$team->openai_key_status"
    prefix="sk-"
    help="Create one under API keys in your OpenAI dashboard."
    :test="true"
    :action="route('settings.keys.update')"
/>
public function update(Request $request)
{
    $team = $request->user()->team;

    match ($request->input('intent')) {
        'remove' => $team->update(['openai_key' => null]),
        'test' => $team->update(['openai_key_status' => OpenAiKey::check($request->input('openai_key') ?: $team->openai_key)]),
        default => $request->filled('openai_key') && $team->update(['openai_key' => $request->input('openai_key'), 'openai_key_status' => 'set']),
    };

    return back();
}

Several providers in one panel and one form: each field posts keys[id] and each button intent[id].

Provider keys

<x-ds::provider-keys :action="route('settings.keys.update')" :providers="[
    ['id' => 'openai', 'provider' => 'OpenAI', 'hint' => $hints['openai'], 'status' => 'valid', 'prefix' => 'sk-', 'test' => true],
    ['id' => 'anthropic', 'provider' => 'Anthropic', 'prefix' => 'sk-ant-'],
]" />

In Livewire, cancel test and check the key yourself:

<x-ds::api-key-field provider="Anthropic" wire:model="key" :status="$status" :test="true" x-on:test.prevent="$wire.testKey()" />

Props: api-key-field

PropTypeDefaultWhat it does
providerstring—The provider's name, used in the label.
labelstring:provider API key
namestringapi_keyThe new key's field name.
hintstring—The saved key, masked by your server. Given, the field shows it with Replace and Remove.
statusunset set valid invalid checkingset with a hint, else unsetWhat your server knows of the key, as a mark and words. checking, valid and invalid are drawn as the agent components' running, done and failed.
messagestringper statusThe status's words, replacing "The key works", "The key was rejected"…
helpstring—Guidance under the field.
errorstring—A validation message; opens the field for replacing.
prefixstring—"sk-": a hint ("Keys start with sk-.") and the placeholder. Never a check.
logostring—An image URL. The logo slot takes markup.
testboolfalseA Test button: dispatches test (cancelable), then posts intent=test. The test slot replaces it.
intentstringintentThe name Test, Remove and Save post under.
actionstring—A URL: the field is its own form, with a Save button.
disabledboolfalse
testLabel, replaceLabel, removeLabel, removeQuestion, saveLabel, revealLabel, prefixTextstringEnglish

Listeners on the tag (x-on:test, @test) go on the field's root, where its events bubble to; anything else (wire:model, x-model, data attributes) lands on the input, and id names it.

Props: provider-keys

PropTypeDefaultWhat it does
providersarray[]Rows: id, provider, and any of the field's hint, status, message, help, error, prefix, logo, test.
actionstring—One form for the panel, with a Save keys button.
titlestringAPI keys
descriptionstring—

Accessibility

  • A labelled password field; the status, the error and the help describe it, and an invalid key marks it aria-invalid.
  • The status line is a polite status, in the page from the start: a check the server finishes is read once.
  • Reveal is a toggle button named "Show key" that says pressed or not.
  • Enter in the replacement field saves; it never presses Remove, which asks before it acts.

Don't

  • Don't pass the key, or a hint that is most of it. Four characters at the end is plenty to recognise it.
  • Don't validate the format here. prefix is a hint: providers change formats, and a check that blocks a valid key is worse than none.
  • Don't keep a key in a public Livewire property after saving it. Public properties travel to the browser in the snapshot; clear it once saved.

From the package repo: edit this page on GitHub.

Was this page helpful?