From 85a76912d3484b727af2e0c310b7508a10541ce3 Mon Sep 17 00:00:00 2001 From: bracesproul Date: Tue, 15 Oct 2024 11:33:40 -0700 Subject: [PATCH 1/3] fix(sdk-js): Pass api key in headers by default if in env --- libs/sdk-js/src/client.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libs/sdk-js/src/client.ts b/libs/sdk-js/src/client.ts index c17e15ed5..133da19b3 100644 --- a/libs/sdk-js/src/client.ts +++ b/libs/sdk-js/src/client.ts @@ -60,6 +60,9 @@ class BaseClient { this.defaultHeaders = config?.defaultHeaders || {}; if (config?.apiKey != null) { this.defaultHeaders["X-Api-Key"] = config.apiKey; + } else if (process.env.LANGCHAIN_API_KEY) { + // Attempt to read the API key from the environment + this.defaultHeaders["X-Api-Key"] = process.env.LANGCHAIN_API_KEY; } } From 7352ab14a24a3e14b2ac5b2f4795f6a555e5c161 Mon Sep 17 00:00:00 2001 From: bracesproul Date: Tue, 15 Oct 2024 11:36:37 -0700 Subject: [PATCH 2/3] cr --- libs/sdk-js/src/client.ts | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/libs/sdk-js/src/client.ts b/libs/sdk-js/src/client.ts index 133da19b3..48cdc3368 100644 --- a/libs/sdk-js/src/client.ts +++ b/libs/sdk-js/src/client.ts @@ -31,6 +31,35 @@ import { OnConflictBehavior, } from "./types.js"; +/** + * Get the API key from the environment. + * Precedence: + * 1. explicit argument + * 2. LANGGRAPH_API_KEY + * 3. LANGSMITH_API_KEY + * 4. LANGCHAIN_API_KEY + * + * @param apiKey - Optional API key provided as an argument + * @returns The API key if found, otherwise undefined + */ +export function getApiKey(apiKey?: string): string | undefined { + if (apiKey) { + return apiKey; + } + + const prefixes = ["LANGGRAPH", "LANGSMITH", "LANGCHAIN"]; + + for (const prefix of prefixes) { + const envKey = process.env[`${prefix}_API_KEY`]; + if (envKey) { + // Remove surrounding quotes + return envKey.trim().replace(/^["']|["']$/g, ""); + } + } + + return undefined; +} + interface ClientConfig { apiUrl?: string; apiKey?: string; @@ -58,12 +87,7 @@ class BaseClient { this.timeoutMs = config?.timeoutMs || 12_000; this.apiUrl = config?.apiUrl || "http://localhost:8123"; this.defaultHeaders = config?.defaultHeaders || {}; - if (config?.apiKey != null) { - this.defaultHeaders["X-Api-Key"] = config.apiKey; - } else if (process.env.LANGCHAIN_API_KEY) { - // Attempt to read the API key from the environment - this.defaultHeaders["X-Api-Key"] = process.env.LANGCHAIN_API_KEY; - } + this.defaultHeaders["X-Api-Key"] = getApiKey(config?.apiKey); } protected prepareFetchOptions( From 433c38228009a12993c8b905bbfc0b46feecfeba Mon Sep 17 00:00:00 2001 From: bracesproul Date: Tue, 15 Oct 2024 11:37:20 -0700 Subject: [PATCH 3/3] cr --- libs/sdk-js/src/client.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libs/sdk-js/src/client.ts b/libs/sdk-js/src/client.ts index 48cdc3368..36f1176eb 100644 --- a/libs/sdk-js/src/client.ts +++ b/libs/sdk-js/src/client.ts @@ -87,7 +87,10 @@ class BaseClient { this.timeoutMs = config?.timeoutMs || 12_000; this.apiUrl = config?.apiUrl || "http://localhost:8123"; this.defaultHeaders = config?.defaultHeaders || {}; - this.defaultHeaders["X-Api-Key"] = getApiKey(config?.apiKey); + const apiKey = getApiKey(config?.apiKey); + if (apiKey) { + this.defaultHeaders["X-Api-Key"] = apiKey; + } } protected prepareFetchOptions(