Fix OpenRouter 401 'User not found' authentication error#3
Merged
Conversation
…eaders - Add placeholder value detection in getApiKey() to reject 'your-api-key' style values that would be sent to OpenRouter causing 401 'User not found' errors - Add HTTP-Referer and X-Title headers required by OpenRouter API - Improve error messages to guide users on proper .env setup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The CLI app throws a 401 "User not found" error when trying to use the OpenRouter API, retrying 3 times before failing.
Root Cause
Two issues were identified:
Placeholder API keys not rejected — The
getApiKey()function insrc/model/llm.tsdid not validate whether the API key was a placeholder value (e.g.,your-api-keyfromenv.example). If a user copiedenv.exampleto.envwithout replacing the placeholder, the invalid key would be sent to OpenRouter, resulting in a 401 "User not found" error. Note:checkApiKeyExists()insrc/utils/env.tsalready had this validation, butgetApiKey()did not.Missing required OpenRouter headers — OpenRouter's API requires
HTTP-RefererandX-Titleheaders for proper request identification. Without these headers, authentication can fail or be unreliable.Changes
src/model/llm.ts:getApiKey()— rejects keys starting withyour-with a clear error message.envsetupHTTP-RefererandX-Titledefault headers to the OpenRouter configurationHow to Set Up the API Key
Copy the example environment file:
Edit
.envand replace the placeholder with your actual OpenRouter API key:Get your API key from OpenRouter
Alternatively, you can set the key interactively in the CLI using the
/modelcommand and selecting OpenRouter as the provider.Built for 87wf9xdgz8 by Kilo for Slack