{"record":{"id":"c427ff4edc46881b","repo":"gitbutlerapp/gitbutler","slug":"when-using-openrouter-you-must-provide-a-valid-ap","errorCode":null,"errorMessage":"When using OpenRouter, you must provide a valid API key","messagePattern":"When using OpenRouter, you must provide a valid API key","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"apps/desktop/src/lib/ai/service.ts","lineNumber":369,"sourceCode":"\t\tif (modelKind === ModelKind.Anthropic) {\n\t\t\tconst anthropicModelName = await this.getAnthropicModelName();\n\t\t\tconst anthropicKey = await this.getAnthropicKey();\n\n\t\t\tif (!anthropicKey) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t\"When using Anthropic in a bring your own key configuration, you must provide a valid token\",\n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn new AnthropicAIClient(anthropicKey, anthropicModelName);\n\t\t}\n\n\t\tif (modelKind === ModelKind.OpenRouter) {\n\t\t\tconst openRouterKey = (await this.getOpenRouterKey())?.trim();\n\t\t\tconst openRouterModelName = await this.getOpenRouterModelName();\n\n\t\t\tif (!openRouterKey) {\n\t\t\t\tthrow new Error(\"When using OpenRouter, you must provide a valid API key\");\n\t\t\t}\n\n\t\t\treturn new OpenAIClient(openRouterKey, openRouterModelName, \"https://openrouter.ai/api/v1\");\n\t\t}\n\n\t\treturn undefined;\n\t}\n\n\tasync summarizeCommit({\n\t\tdiffInput,\n\t\tuseHaiku = false,\n\t\tuseEmojiStyle = false,\n\t\tuseExtraConciseStyle = false,\n\t\tcommitTemplate,\n\t\tonToken,\n\t\tbranchName,\n\t}: SummarizeCommitOpts): Promise<string | undefined> {\n\t\tconst aiClient = await this.buildClient();","sourceCodeStart":351,"sourceCodeEnd":387,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/apps/desktop/src/lib/ai/service.ts#L351-L387","documentation":"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.","triggerScenarios":"User selects OpenRouter as the AI provider with an empty or all-whitespace key value.","commonSituations":"Pasted only spaces into the key field; key cleared on settings reset; signed up for OpenRouter but never generated an API 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."],"exampleFix":"// before\nopenRouterKey: '   '\n// after\nopenRouterKey: 'sk-or-v1-...' // valid OpenRouter API key","handlingStrategy":"validation","validationCode":"const key = (await aiService.getOpenRouterKey())?.trim();\nif (!key) {\n  // prompt for the OpenRouter API key before invoking AI features\n}","typeGuard":null,"tryCatchPattern":"try {\n  await aiService.summarize(...);\n} catch (e) {\n  if (e.message.includes('OpenRouter') && e.message.includes('valid API key')) {\n    // open settings to collect the OpenRouter API key\n  } else {\n    throw e;\n  }\n}","preventionTips":["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."],"tags":["configuration","api-key","openrouter"],"backgroundTag":"missing-api-key","analyzedSha":"58e5313667b857ef39a730e380af31816a7b1768","analyzedAt":"2026-09-18T06:50:32.052Z","contentChangedAt":"2026-09-18T06:50:32.052Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}