Skip to content

Commit 06fb68d

Browse files
feat(client): support reading the base url from an env variable (#547)
1 parent 566d290 commit 06fb68d

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ export interface ClientOptions {
2020

2121
/**
2222
* Override the default base URL for the API, e.g., "https://api.example.com/v2/"
23+
*
24+
* Defaults to process.env['OPENAI_BASE_URL'].
2325
*/
24-
baseURL?: string;
26+
baseURL?: string | null | undefined;
2527

2628
/**
2729
* The maximum amount of time (in milliseconds) that the client should wait for a response
@@ -89,9 +91,9 @@ export class OpenAI extends Core.APIClient {
8991
/**
9092
* API Client for interfacing with the OpenAI API.
9193
*
92-
* @param {string} [opts.apiKey==process.env['OPENAI_API_KEY'] ?? undefined]
93-
* @param {string | null} [opts.organization==process.env['OPENAI_ORG_ID'] ?? null]
94-
* @param {string} [opts.baseURL] - Override the default base URL for the API.
94+
* @param {string} [opts.apiKey=process.env['OPENAI_API_KEY'] ?? undefined]
95+
* @param {string | null} [opts.organization=process.env['OPENAI_ORG_ID'] ?? null]
96+
* @param {string} [opts.baseURL=process.env['OPENAI_BASE_URL'] ?? https://api.openai.com/v1] - Override the default base URL for the API.
9597
* @param {number} [opts.timeout=10 minutes] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
9698
* @param {number} [opts.httpAgent] - An HTTP agent used to manage HTTP(s) connections.
9799
* @param {Core.Fetch} [opts.fetch] - Specify a custom `fetch` function implementation.
@@ -101,6 +103,7 @@ export class OpenAI extends Core.APIClient {
101103
* @param {boolean} [opts.dangerouslyAllowBrowser=false] - By default, client-side use of this library is not allowed, as it risks exposing your secret API credentials to attackers.
102104
*/
103105
constructor({
106+
baseURL = Core.readEnv('OPENAI_BASE_URL'),
104107
apiKey = Core.readEnv('OPENAI_API_KEY'),
105108
organization = Core.readEnv('OPENAI_ORG_ID') ?? null,
106109
...opts
@@ -115,7 +118,7 @@ export class OpenAI extends Core.APIClient {
115118
apiKey,
116119
organization,
117120
...opts,
118-
baseURL: opts.baseURL ?? `https://api.openai.com/v1`,
121+
baseURL: baseURL ?? `https://api.openai.com/v1`,
119122
};
120123

121124
if (!options.dangerouslyAllowBrowser && Core.isRunningInBrowser()) {

tests/index.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,21 @@ describe('instantiate client', () => {
132132
const client = new OpenAI({ baseURL: 'http://localhost:5000/custom/path', apiKey: 'My API Key' });
133133
expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/custom/path/foo');
134134
});
135+
136+
afterEach(() => {
137+
process.env['SINK_BASE_URL'] = undefined;
138+
});
139+
140+
test('explicit option', () => {
141+
const client = new OpenAI({ baseURL: 'https://example.com', apiKey: 'My API Key' });
142+
expect(client.baseURL).toEqual('https://example.com');
143+
});
144+
145+
test('env variable', () => {
146+
process.env['OPENAI_BASE_URL'] = 'https://example.com/from_env';
147+
const client = new OpenAI({ apiKey: 'My API Key' });
148+
expect(client.baseURL).toEqual('https://example.com/from_env');
149+
});
135150
});
136151

137152
test('maxRetries option is correctly set', () => {

0 commit comments

Comments
 (0)