gitbutlerapp/gitbutler · error
When using OpenRouter, you must provide a valid API key
Error message
When using OpenRouter, you must provide a valid API key
What it means
buildClient() throws this when the model kind is OpenRouter but the configured API key is missing or whitespace-only (getOpenRouterKey()?.trim() is falsy). OpenRouter is accessed via OpenAIClient against the openrouter.ai endpoint, which requires the key.
Solutions
- Paste a valid OpenRouter API key (trimmed, no leading/trailing whitespace) into AI settings.
- Generate a key at openrouter.ai/keys if you don't have one.
- Switch providers if you don't intend to use OpenRouter.
Example fix
// before openRouterKey: ' ' // after openRouterKey: 'sk-or-v1-...' // valid OpenRouter API key
Defensive patterns
Strategy: validation
Validate before calling
const key = (await aiService.getOpenRouterKey())?.trim();
if (!key) {
// prompt for the OpenRouter API key before invoking AI features
} Try / catch
try {
await aiService.summarize(...);
} catch (e) {
if (e.message.includes('OpenRouter') && e.message.includes('valid API key')) {
// open settings to collect the OpenRouter API key
} else {
throw e;
}
} Prevention
- Paste the key trimmed — whitespace-only values fail this check.
- Generate a key at openrouter.ai/keys before selecting the provider.
- Validate non-empty after trim when saving the key in settings.
When it happens
Trigger: User selects OpenRouter as the AI provider with an empty or all-whitespace key value.
Common situations: Pasted only spaces into the key field; key cleared on settings reset; signed up for OpenRouter but never generated an API key.
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
- Enter an Anthropic API key
- Enter an OpenAI API key
- Environment variable ANTHROPIC_API_KEY is not set
- Environment variable OPENAI_API_KEY is not set
- No Anthropic own key configured. Add this through the…
AI-assisted analysis of gitbutlerapp/gitbutler@58e5313667 (2026-09-18).
Data as JSON: /api/errors/c427ff4edc46881b.
Report an issue: GitHub.
Appendix: source
Thrown at apps/desktop/src/lib/ai/service.ts:369
if (modelKind === ModelKind.Anthropic) {
const anthropicModelName = await this.getAnthropicModelName();
const anthropicKey = await this.getAnthropicKey();
if (!anthropicKey) {
throw new Error(
"When using Anthropic in a bring your own key configuration, you must provide a valid token",
);
}
return new AnthropicAIClient(anthropicKey, anthropicModelName);
}
if (modelKind === ModelKind.OpenRouter) {
const openRouterKey = (await this.getOpenRouterKey())?.trim();
const openRouterModelName = await this.getOpenRouterModelName();
if (!openRouterKey) {
throw new Error("When using OpenRouter, you must provide a valid API key");
}
return new OpenAIClient(openRouterKey, openRouterModelName, "https://openrouter.ai/api/v1");
}
return undefined;
}
async summarizeCommit({
diffInput,
useHaiku = false,
useEmojiStyle = false,
useExtraConciseStyle = false,
commitTemplate,
onToken,
branchName,
}: SummarizeCommitOpts): Promise<string | undefined> {
const aiClient = await this.buildClient();View on GitHub (pinned to 58e5313667)