jlcodes99/cockpit-tools · error
API_KEY_NOT_FOUND
API_KEY_NOT_FOUND
Error message
API_KEY_NOT_FOUND
What it means
renameApiKeyOnCodexModelProvider throws API_KEY_NOT_FOUND when the provider exists but none of its apiKeys has the given apiKeyId. The lookup is a plain find on provider.apiKeys by id; note that removeApiKeyFromCodexModelProvider silently no-ops in the same situation, but rename throws to avoid renaming nothing.
Source
Thrown at src/services/codexModelProviderService.ts:797
return { ...provider, apiKeys: provider.apiKeys.map((item) => ({ ...item })) };
}
provider.apiKeys = nextApiKeys;
provider.updatedAt = Date.now();
await writeProviders(providers);
return { ...provider, apiKeys: provider.apiKeys.map((item) => ({ ...item })) };
}
/** Explicit rename for an existing provider API key (#1510). Does not rewrite key material. */
export async function renameApiKeyOnCodexModelProvider(
providerId: string,
apiKeyId: string,
name: string,
): Promise<CodexModelProvider> {
const providers = await ensureProvidersLoaded();
const provider = providers.find((item) => item.id === providerId);
if (!provider) throw new Error('PROVIDER_NOT_FOUND');
const apiKey = provider.apiKeys.find((item) => item.id === apiKeyId);
if (!apiKey) throw new Error('API_KEY_NOT_FOUND');
const now = Date.now();
apiKey.name = sanitizeName(name);
apiKey.updatedAt = now;
provider.updatedAt = now;
await writeProviders(providers);
return { ...provider, apiKeys: provider.apiKeys.map((item) => ({ ...item })) };
}
/** Replace the secret for an existing provider API key without changing its id. */
export async function updateApiKeyOnCodexModelProvider(
providerId: string,
apiKeyId: string,
apiKey: string,
name?: string,
): Promise<CodexModelProvider> {
const normalizedApiKey = sanitizeApiKey(apiKey);
if (!normalizedApiKey) throw new Error("API_KEY_REQUIRED");View on GitHub (pinned to 1ed8b77992)
Solutions
- Fetch the provider and confirm apiKeyId is present in provider.apiKeys before renaming.
- If the key was deleted, re-add it with addApiKeyToCodexModelProvider and then rename.
- Check you are pairing the apiKeyId with the correct providerId (keys from another provider will never match).
- Catch 'API_KEY_NOT_FOUND' and refresh the key list in the UI.
Example fix
// before
await renameApiKeyOnCodexModelProvider(providerId, keyId, 'prod-key');
// after
const provider = (await listCodexModelProviders()).find((p) => p.id === providerId);
if (!provider?.apiKeys.some((k) => k.id === keyId)) throw new Error('Key not found on provider');
await renameApiKeyOnCodexModelProvider(providerId, keyId, 'prod-key'); Defensive patterns
Strategy: type-guard
Validate before calling
const provider = (await listCodexModelProviders()).find((p) => p.id === providerId);
if (!provider?.apiKeys.some((k) => k.id === apiKeyId)) {
throw new Error(`API key ${apiKeyId} not found on provider ${providerId}`);
} Type guard
function hasApiKey(provider: { apiKeys: { id: string }[] } | undefined, apiKeyId: string): boolean {
return !!provider && provider.apiKeys.some((k) => k.id === apiKeyId);
} Try / catch
try {
await renameApiKeyOnCodexModelProvider(providerId, apiKeyId, name);
} catch (e) {
if ((e as Error).message === 'API_KEY_NOT_FOUND') {
await reloadApiKeys(providerId); // key was removed elsewhere
} else throw e;
} Prevention
- Verify the apiKeyId belongs to the SAME provider before renaming.
- Refresh key lists after deletions so stale key ids are not used.
- Guard against double-submitted renames (idempotency on the client).
- Distinguish key ids across providers; never reuse ids from another row.
When it happens
Trigger: Calling renameApiKeyOnCodexModelProvider(providerId, apiKeyId, name) with an apiKeyId that is not in that provider's apiKeys array — wrong key id, key already removed, or an id belonging to a different provider.
Common situations: Renaming a key from a stale list after the key was deleted; mixing up key ids between two providers that both have keys; retrying a rename after a successful first attempt that the UI did not reflect.
Understand the failure class
Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.
Related errors
- PROVIDER_NOT_FOUND
- API_KEY_REQUIRED
- API_KEY_EXISTS
- SUB2API_API_KEY_MISSING
- Official auth.json requires OPENAI_API_KEY for API Key accou
AI-assisted analysis of jlcodes99/cockpit-tools@1ed8b77992 (2026-09-05).
Data as JSON: /api/errors/2f85817a3b291077.
Report an issue: GitHub.