#7658·pi

Extension API for persisting API-key credentials (auth.json)

Author: arseniy-glCreated Aug 5, 2026Updated Sep 27, 2026

Problem

Extensions can register custom providers via pi.registerProvider() (legacy config form) and users can define providers in models.json, but there is no programmatic way for an extension to persist an API-key credential to auth.json.

What extensions have today:

  • pi.registerProvider(name, config) — apiKey must be a literal, $ENV_VAR reference, or !command in the config itself
  • oauth.login() flow — persists credentials, but only through the interactive /login flow and only for OAuth
  • ctx.modelRegistry.getProviderAuth(id) — read/resolve only
  • getAuthPath() — exposes the file location, but not a write API

So an extension that collects an API key from the user (e.g. a setup wizard) has no first-class way to store it where pi natively looks for credentials.

Use case

I'm building an extension that adds a TUI setup wizard for OpenAI-compatible providers: it asks for provider name, id, baseUrl, and token, then fetches GET /models and registers the provider. The natural place for the token is auth.json (0600, native credential location, picked up by /login availability checks), not a plaintext apiKey literal in models.json.

Current workarounds and their problems

Workaround Problem
Hand-edit auth.json from the extension (read/modify/write JSON) No locking/serialization — races with pi's own writes (cf. discussion #1629 about concurrent auth access); file format is internal and may change
apiKey literal in models.json Secret in plaintext; file is not 0600
$ENV_VAR reference Pushes setup friction onto the user; the wizard can't complete the job

Proposal

Expose the existing credential store on the extension API. pi-ai already has the right abstraction — CredentialStore with a serialized modify(providerId, fn) write path (mutual exclusion per provider id, cross-process where the store supports it) — it just isn't surfaced to extensions.

A minimal surface could be:

typescript
// Narrow option:
await pi.setProviderApiKey(providerId, key);   // persists { type: "api_key", key }
await pi.removeProviderCredential(providerId); // logout-equivalent

// Or expose the store directly:
await pi.credentials.modify(providerId, async (current) => ({ type: "api_key", key }));

Key properties that make this safe to expose:

  • Writes go through the same serialized path pi itself uses (no format duplication, no races)
  • Works for providers registered via registerProvider() legacy config form and providers defined in models.json
  • Lets extensions build complete setup UX (wizards, org onboarding, key rotation) without teaching users to hand-edit config files

The same surface would presumably be useful on the SDK (createAgentSession) side as well.