musistudio/claude-code-router · error · Error

OpenCode CLI API key was not found.

Error message

OpenCode CLI API key was not found.

What it means

readOpenCodeCredential() found an OpenCode auth credential entry (hasCredential) but its apiKey field is empty, so importOpenCodeProvider refuses to proceed. The credential store is half-written or corrupted.

Source

Thrown at packages/core/src/agents/local-providers/opencode.ts:133

        modelDisplayNames,
        models,
        name,
        protocol,
        sourceFile: credential.sourceFile,
        status: "available"
      };
    }
    return missingCandidate("opencode", id, name, protocol, models, modelDisplayNames);
  });
}

export function importOpenCodeProvider(
  candidate: LocalAgentProviderCandidate,
  providerNames: string[]
): LocalAgentProviderImportResult {
  const credential = readOpenCodeCredential();
  if (credential?.hasCredential && !credential.apiKey) {
    throw new Error("OpenCode CLI API key was not found.");
  }
  const publicOnly = !credential;
  const catalog = readOpenCodeCatalog({ publicOnly });
  if (!isOpenCodeProtocol(candidate.protocol)) {
    throw new Error(`Unsupported OpenCode protocol: ${candidate.protocol}`);
  }
  const protocol = candidate.protocol;
  if (publicOnly && !candidate.models.every((model) => catalog.models[protocol].includes(model))) {
    throw new Error("OpenCode CLI public models were not found.");
  }
  const provider = providerPayload(
    candidate,
    uniqueProviderName(providerNames, candidate.name),
    catalog.baseUrl
  );
  if (publicOnly) {
    return {
      candidate,

View on GitHub (pinned to 99f24806c6)

Solutions

  1. Re-run `opencode auth login` to rewrite the credential, then retry import.
  2. Delete the stale OpenCode credential entry and re-authenticate.
  3. Check OPENCODE_API_KEY env var if this build reads it as fallback.
Defensive patterns

Strategy: validation

Validate before calling

const cred = readOpenCodeCredential?.();
if (cred?.hasCredential && !cred.apiKey) await promptRelogin('opencode');

Type guard

function hasUsableOpenCodeKey(cred: {hasCredential: boolean; apiKey?: string} | null): boolean {
  return !cred || (!cred.hasCredential || !!cred.apiKey);
}

Try / catch

catch (e) { if ((e as Error).message === 'OpenCode CLI API key was not found.') await shell('opencode auth login'); }

Prevention

When it happens

Trigger: Calling importLocalAgentProvider with an OpenCode candidate while the OS keychain/credential entry for OpenCode exists but has no usable api key string.

Common situations: Interrupted `opencode auth login`, corrupted keychain entry, or a credential format change between OpenCode CLI versions.

Understand the failure class

Background: "API key is required" / "API key not found" / "No API key was set": the missing-api-key error family across 16 libraries — this error's family across 16 libraries.

Related errors


AI-assisted analysis of musistudio/claude-code-router@99f24806c6 (2026-08-27). Data as JSON: /api/errors/34431599d93d93d8. Report an issue: GitHub.