Skip to content

Commit

Permalink
Merge pull request #2546 from langchain-ai/jacob/jsenv
Browse files Browse the repository at this point in the history
fix(js): Adds fallback for fetching environment variables
  • Loading branch information
nfcampos authored Nov 26, 2024
2 parents 877124f + c6a953c commit 4b1b3ce
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion libs/sdk-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@langchain/langgraph-sdk",
"version": "0.0.27",
"version": "0.0.28",
"description": "Client library for interacting with the LangGraph API",
"type": "module",
"packageManager": "[email protected]",
Expand Down
3 changes: 2 additions & 1 deletion libs/sdk-js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
OnConflictBehavior,
} from "./types.js";
import { mergeSignals } from "./utils/signals.js";
import { getEnvironmentVariable } from "./utils/env.js";

/**
* Get the API key from the environment.
Expand All @@ -53,7 +54,7 @@ export function getApiKey(apiKey?: string): string | undefined {
const prefixes = ["LANGGRAPH", "LANGSMITH", "LANGCHAIN"];

for (const prefix of prefixes) {
const envKey = process.env[`${prefix}_API_KEY`];
const envKey = getEnvironmentVariable(`${prefix}_API_KEY`);
if (envKey) {
// Remove surrounding quotes
return envKey.trim().replace(/^["']|["']$/g, "");
Expand Down
11 changes: 11 additions & 0 deletions libs/sdk-js/src/utils/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function getEnvironmentVariable(name: string): string | undefined {
// Certain setups (Deno, frontend) will throw an error if you try to access environment variables
try {
return typeof process !== "undefined"
? // eslint-disable-next-line no-process-env
process.env?.[name]
: undefined;
} catch (e) {
return undefined;
}
}

0 comments on commit 4b1b3ce

Please sign in to comment.