diff --git a/routes.js b/routes.js index b994e0d..d5cbe46 100644 --- a/routes.js +++ b/routes.js @@ -392,10 +392,13 @@ async function handleDirectMessages(req, res) { logInfo(`Direct forwarding to ${model.type} endpoint: ${endpoint.base_url}`); - // Get API key + // Get API key - support client x-api-key for anthropic endpoint let authHeader; try { - authHeader = await getApiKey(req.headers.authorization); + const clientAuthFromXApiKey = req.headers['x-api-key'] + ? `Bearer ${req.headers['x-api-key']}` + : null; + authHeader = await getApiKey(req.headers.authorization || clientAuthFromXApiKey); } catch (error) { logError('Failed to get API key', error); return res.status(500).json({ diff --git a/transformers/request-anthropic.js b/transformers/request-anthropic.js index 003edf9..0c88377 100644 --- a/transformers/request-anthropic.js +++ b/transformers/request-anthropic.js @@ -163,9 +163,6 @@ export function getAnthropicHeaders(authHeader, clientHeaders = {}, isStreaming 'accept': 'application/json', 'content-type': 'application/json', 'anthropic-version': clientHeaders['anthropic-version'] || '2023-06-01', - // Prefer client-provided x-api-key for anthropic endpoint format - ...(clientHeaders['x-api-key'] ? { 'x-api-key': clientHeaders['x-api-key'] } : {}), - 'authorization': authHeader || '', 'x-api-provider': 'anthropic', 'x-factory-client': 'cli', 'x-session-id': sessionId, @@ -175,6 +172,14 @@ export function getAnthropicHeaders(authHeader, clientHeaders = {}, isStreaming 'connection': 'keep-alive' }; + // Prefer client-provided x-api-key for anthropic endpoint format + if (clientHeaders['x-api-key']) { + headers['x-api-key'] = clientHeaders['x-api-key']; + } else if (authHeader) { + // If no client x-api-key, use authorization header (from FACTORY_API_KEY or refresh token) + headers['authorization'] = authHeader; + } + // Handle anthropic-beta header based on reasoning configuration const reasoningLevel = modelId ? getModelReasoning(modelId) : null; let betaValues = [];