jlcodes99/cockpit-tools · error

SUB2API_API_KEY_MISSING

SUB2API_API_KEY_MISSING

Error message

SUB2API_API_KEY_MISSING

What it means

buildSub2apiApiKeyCredentials throws SUB2API_API_KEY_MISSING when building sub2api credentials for an API-key account whose openai_api_key is absent or blank. A sub2api API-key account entry cannot exist without an api_key.

Source

Thrown at src/utils/codexExportFormats.ts:355

  }

  const planType = resolvePlanType(account);
  if (planType) {
    credentials.plan_type = planType;
  }

  const subscriptionExpiresAt = resolveSubscriptionExpiresAt(account);
  if (subscriptionExpiresAt) {
    credentials.subscription_expires_at = subscriptionExpiresAt;
  }

  return credentials;
}

function buildSub2apiApiKeyCredentials(account: CodexAccount): JsonRecord {
  const apiKey = account.openai_api_key?.trim();
  if (!apiKey) {
    throw new Error('SUB2API_API_KEY_MISSING');
  }
  return {
    base_url: account.api_base_url?.trim() || SUB2API_DEFAULT_OPENAI_BASE_URL,
    api_key: apiKey,
  };
}

function buildSub2apiExtra(account: CodexAccount): JsonRecord | undefined {
  const authProvider = resolveAuthProvider(account);
  const extra: JsonRecord = {};
  if (authProvider) {
    extra.auth_provider = authProvider;
  }
  if (
    account.codex_fingerprint_mode &&
    account.codex_fingerprint_mode !== 'off'
  ) {
    extra.codex_fingerprint_mode = account.codex_fingerprint_mode;

View on GitHub (pinned to 1ed8b77992)

Solutions

  1. Set the account's openai_api_key before exporting to sub2api
  2. Skip API-key accounts without a key in the export batch
  3. Re-enter the API key in the account editor
  4. Validate keys at import time so incomplete accounts are flagged early

Example fix

// before
const item = toSub2apiAccount(account); // throws SUB2API_API_KEY_MISSING
// after
if (account.openai_api_key?.trim()) {
  const item = toSub2apiAccount(account);
} else {
  console.warn('skip account: missing API key');
}
Defensive patterns

Strategy: validation

Validate before calling

if (!account.openai_api_key?.trim()) {
  throw new Error('cannot export: missing openai_api_key');
}

Type guard

function hasApiKey(a: CodexAccount): boolean {
  return !!a.openai_api_key?.trim();
}

Try / catch

try {
  const item = toSub2apiAccount(account);
} catch (e) {
  if ((e as Error).message === 'SUB2API_API_KEY_MISSING') {
    // prompt user to enter the API key
  }
}

Prevention

When it happens

Trigger: Calling toSub2apiAccount on an API-key-type Codex account where openai_api_key is undefined, empty, or whitespace-only.

Common situations: Account created as API-key type but key never entered; key cleared during edit; import from a format that did not carry OPENAI_API_KEY.

Related errors


AI-assisted analysis of jlcodes99/cockpit-tools@1ed8b77992 (2026-09-05). Data as JSON: /api/errors/1b9e5e389e0ba390. Report an issue: GitHub.