Skip to content

Context7 Platform -- Up-to-date docs for AI agents

feat: add Bearer token support in Authorization header for API authentication

enesgules5d5288cd1eba
diff --git a/src/index.ts b/src/index.ts
index b8e680e4..d9a88bf1 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -226,8 +226,25 @@ async function main() {
return typeof value === "string" ? value : value[0];
};
+ // Extract Authorization header and remove Bearer prefix if present
+ const extractBearerToken = (
+ authHeader: string | string[] | undefined
+ ): string | undefined => {
+ const header = extractHeaderValue(authHeader);
+ if (!header) return undefined;
+
+ // If it starts with 'Bearer ', remove that prefix
+ if (header.startsWith("Bearer ")) {
+ return header.substring(7).trim();
+ }
+
+ // Otherwise return the raw value
+ return header;
+ };
+
// Check headers in order of preference
const apiKey =
+ extractBearerToken(req.headers.authorization) ||
extractHeaderValue(req.headers["x-context7-api-key"]) || // Standard with x- prefix
extractHeaderValue(req.headers["context7-api-key"]) || // Without x- prefix
extractHeaderValue(req.headers["context7_api_key"]) || // With underscores