gitbutlerapp/gitbutler · error · anyhow::Error

Enter an Anthropic API key

Error message

Enter an Anthropic API key

What it means

Validation in the AI configuration update flow: when the active provider is Anthropic with the 'BringYourOwn' credential option, a usable key must exist — either submitted in this update or already stored. If both are absent, `validate_update` rejects the update with this form-style message.

Source

Thrown at crates/but-napi/src/ai.rs:143

    update: &AiConfigurationUpdate,
    openai_has_key: bool,
    anthropic_has_key: bool,
) -> Result<()> {
    let configuration = domain_configuration(update, DomainConfiguration::default())?;

    if configuration.provider == LLMProviderKind::OpenAi
        && configuration.openai.key_option == CredentialsKeyOption::BringYourOwn
        && submitted_key(update.openai_api_key.clone()).is_none()
        && !openai_has_key
    {
        bail!("Enter an OpenAI API key")
    }
    if configuration.provider == LLMProviderKind::Anthropic
        && configuration.anthropic.key_option == CredentialsKeyOption::BringYourOwn
        && submitted_key(update.anthropic_api_key.clone()).is_none()
        && !anthropic_has_key
    {
        bail!("Enter an Anthropic API key")
    }
    Ok(())
}

fn domain_configuration(
    update: &AiConfigurationUpdate,
    mut configuration: DomainConfiguration,
) -> Result<DomainConfiguration> {
    configuration.provider = provider(&update.provider)?;
    configuration.openai = OpenAiConfiguration {
        key_option: key_option("OpenAI", &update.openai_key_option)?,
        model: update.openai_model.clone(),
        custom_endpoint: update.openai_custom_endpoint.clone(),
    };
    configuration.anthropic = AnthropicConfiguration {
        key_option: key_option("Anthropic", &update.anthropic_key_option)?,
        model: update.anthropic_model.clone(),
    };

View on GitHub (pinned to caf1f223d3)

Solutions

  1. Include a non-empty (after trim) `anthropic_api_key` in the same update
  2. If a key is already stored, make sure the update does not clear it and resend the stored-key state
  3. Switch the credential option away from bring-your-own if no key should be supplied

Example fix

// before
await updateAiConfig({ provider: 'anthropic', anthropicKeyOption: 'bring-your-own' }); // bails

// after: submit the key together with the option
await updateAiConfig({
  provider: 'anthropic',
  anthropicKeyOption: 'bring-your-own',
  anthropicApiKey: keyInput.trim(), // must be non-empty
});
Defensive patterns

Strategy: validation

Validate before calling

// TypeScript: pre-validate the BYO key requirement client-side
if (
  update.provider === 'anthropic' &&
  update.anthropicKeyOption === 'bring-your-own' &&
  !(update.anthropicApiKey ?? '').trim() &&
  !hasStoredAnthropicKey
) {
  showFieldError('anthropicApiKey', 'Enter an Anthropic API key');
  return;
}
await updateAiConfig(update);

Try / catch

Catch the NAPI error whose message is 'Enter an Anthropic API key' and attach it to the key input field; keep the user on the form instead of failing silently.

Prevention

When it happens

Trigger: An `AiConfigurationUpdate` with provider=anthropic and anthropic_key_option=bring-your-own, no non-empty `anthropic_api_key` in the payload (after trim), and no previously stored Anthropic key.

Common situations: Switching to Anthropic without entering a key; the UI sends the provider change before the key field is filled; a stored key was removed earlier.

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 gitbutlerapp/gitbutler@caf1f223d3 (2026-08-20). Data as JSON: /api/errors/ffeaacfcb439065c. Report an issue: GitHub.