Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

fix(auth): accept x-api-key header in extractApiKey (#2225)

diegosouzapw7ab68365ba04
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7c8ab6f31e..c3b9dbb8ea 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,10 @@
## [Unreleased]
+### Fixed
+
+- **fix(auth):** accept `x-api-key` header in `extractApiKey` so Anthropic-native clients (Claude Code, `@anthropic-ai/sdk`) hit the same per-key policy enforcement as Bearer clients. Previously these requests were treated as anonymous, bypassing model/budget/rate-limit policies and showing up as `NULL` in `usage_history.api_key_id` (~50% of traffic invisible in Costs/Analytics). `Authorization: Bearer` still wins when both are present (back-compat). (#2225)
+
### Changed
- **BREAKING**: dropped Node 20.x support. Minimum Node version is now 22.22.2 (or 24.0.0+). Required because http-proxy-middleware 4.x requires `node >=22.15.0`. Users on Node 20 must upgrade — see [`package.json` engines field](package.json) and the README Node badge.
diff --git a/src/sse/services/auth.ts b/src/sse/services/auth.ts
index 210fb66a4e..10e7e3db10 100644
--- a/src/sse/services/auth.ts
+++ b/src/sse/services/auth.ts
@@ -1682,8 +1682,15 @@ export async function clearRecoveredProviderState(
}
/**
- * Extract API key from request headers
- * Follows management API standard: case-insensitive bearer matching and full trimming
+ * Extract API key from request headers.
+ *
+ * Honors both:
+ * - `Authorization: Bearer <key>` (OpenAI / OmniRoute / Codex CLI / Bearer clients)
+ * - `x-api-key: <key>` (Anthropic Messages API contract — Claude Code,
+ * `@anthropic-ai/sdk`, any SDK that sets `anthropic-version`)
+ *
+ * When both are present, `Authorization: Bearer` wins for back-compat
+ * (issue #2225).
*/
export function extractApiKey(request: Request) {
const authHeader = request.headers.get("Authorization") || request.headers.get("authorization");
@@ -1693,6 +1700,14 @@ export function extractApiKey(request: Request) {
return trimmedHeader.slice(7).trim();
}
}
+ // Issue #2225: Anthropic Messages API clients authenticate via x-api-key.
+ // Without this fallback, per-key policies are bypassed and traffic is
+ // recorded with null api_key_id (invisible in Costs / Analytics).
+ const xApiKey = request.headers.get("x-api-key") || request.headers.get("X-Api-Key");
+ if (typeof xApiKey === "string") {
+ const trimmed = xApiKey.trim();
+ if (trimmed.length > 0) return trimmed;
+ }
return null;
}
diff --git a/tests/unit/auth-extract-api-key.test.ts b/tests/unit/auth-extract-api-key.test.ts
new file mode 100644
index 0000000000..48dee69e0b
--- /dev/null
+++ b/tests/unit/auth-extract-api-key.test.ts
@@ -0,0 +1,74 @@
+import test from "node:test";
+import assert from "node:assert/strict";
+
+const { extractApiKey } = await import("../../src/sse/services/auth.ts");
+
+function makeRequest(headers: Record<string, string>): Request {
+ return new Request("https://omniroute.test/v1/messages", { headers });
+}
+
+test("extractApiKey returns Bearer key when Authorization header is set", () => {
+ const req = makeRequest({ Authorization: "Bearer sk-test-bearer" });
+ assert.equal(extractApiKey(req), "sk-test-bearer");
+});
+
+test("extractApiKey trims surrounding whitespace from Bearer token", () => {
+ const req = makeRequest({ Authorization: "Bearer sk-padded-token " });
+ assert.equal(extractApiKey(req), "sk-padded-token");
+});
+
+test("extractApiKey is case-insensitive on the Authorization header name", () => {
+ const req = makeRequest({ authorization: "Bearer sk-lowercase-header" });
+ assert.equal(extractApiKey(req), "sk-lowercase-header");
+});
+
+test("extractApiKey is case-insensitive on the 'bearer' prefix", () => {
+ const req = makeRequest({ Authorization: "bearer sk-lowercase-prefix" });
+ assert.equal(extractApiKey(req), "sk-lowercase-prefix");
+});
+
+test("extractApiKey falls back to x-api-key when Authorization is absent (#2225)", () => {
+ const req = makeRequest({ "x-api-key": "sk-anthropic-native" });
+ assert.equal(extractApiKey(req), "sk-anthropic-native");
+});
+
+test("extractApiKey accepts uppercase X-Api-Key header (#2225)", () => {
+ const req = makeRequest({ "X-Api-Key": "sk-uppercase-xapikey" });
+ assert.equal(extractApiKey(req), "sk-uppercase-xapikey");
+});
+
+test("extractApiKey trims surrounding whitespace from x-api-key value", () => {
+ const req = makeRequest({ "x-api-key": " sk-padded-xapikey " });
+ assert.equal(extractApiKey(req), "sk-padded-xapikey");
+});
+
+test("extractApiKey prefers Bearer over x-api-key when both are present (back-compat)", () => {
+ const req = makeRequest({
+ Authorization: "Bearer sk-bearer-wins",
+ "x-api-key": "sk-loser",
+ });
+ assert.equal(extractApiKey(req), "sk-bearer-wins");
+});
+
+test("extractApiKey returns null when neither header is present", () => {
+ const req = makeRequest({});
+ assert.equal(extractApiKey(req), null);
+});
+
+test("extractApiKey returns null when x-api-key contains only whitespace", () => {
+ const req = makeRequest({ "x-api-key": " " });
+ assert.equal(extractApiKey(req), null);
+});
+
+test("extractApiKey returns null when Authorization is not a Bearer scheme and x-api-key is absent", () => {
+ const req = makeRequest({ Authorization: "Basic <stub-base64>" });
+ assert.equal(extractApiKey(req), null);
+});
+
+test("extractApiKey falls back to x-api-key when Authorization is a non-Bearer scheme", () => {
+ const req = makeRequest({
+ Authorization: "Basic <stub-base64>",
+ "x-api-key": "stub-fallback-after-basic",
+ });
+ assert.equal(extractApiKey(req), "stub-fallback-after-basic");
+});