From 4503604d044365220bba74b4c99e0165751367d5 Mon Sep 17 00:00:00 2001 From: 1e0n Date: Thu, 9 Oct 2025 14:58:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81Anthropic=E7=AB=AF=E7=82=B9?= =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AFx-api-key=EF=BC=9A\n-=20/v1/messages?= =?UTF-8?q?=E8=AF=BB=E5=8F=96x-api-key=E5=B9=B6=E4=BC=98=E5=85=88=E4=BD=9C?= =?UTF-8?q?=E4=B8=BA=E5=AE=A2=E6=88=B7=E7=AB=AF=E6=8E=88=E6=9D=83=E4=BC=A0?= =?UTF-8?q?=E9=80=92\n-=20getAnthropicHeaders=E8=BD=AC=E5=8F=91x-api-key?= =?UTF-8?q?=E5=B9=B6=E9=80=8F=E4=BC=A0anthropic-version\n-=20CORS=E5=85=81?= =?UTF-8?q?=E8=AE=B8X-API-Key=E4=B8=8Eanthropic-version?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes.js | 7 +++++-- server.js | 2 +- transformers/request-anthropic.js | 5 +++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/routes.js b/routes.js index 42efe48..b994e0d 100644 --- a/routes.js +++ b/routes.js @@ -257,10 +257,13 @@ async function handleDirectResponses(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/server.js b/server.js index 9db93d6..2152110 100644 --- a/server.js +++ b/server.js @@ -12,7 +12,7 @@ app.use(express.urlencoded({ extended: true, limit: '50mb' })); app.use((req, res, next) => { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); - res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization'); + res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-API-Key, anthropic-version'); if (req.method === 'OPTIONS') { return res.sendStatus(200); diff --git a/transformers/request-anthropic.js b/transformers/request-anthropic.js index 52f6f41..003edf9 100644 --- a/transformers/request-anthropic.js +++ b/transformers/request-anthropic.js @@ -162,8 +162,9 @@ export function getAnthropicHeaders(authHeader, clientHeaders = {}, isStreaming const headers = { 'accept': 'application/json', 'content-type': 'application/json', - 'anthropic-version': '2023-06-01', - 'x-api-key': 'placeholder', + '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',