Skip to content

Commit

Permalink
feat(client): support reading the base url from an env variable (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Dec 19, 2023
1 parent 266646c commit e084233
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ export interface ClientOptions {

/**
* Override the default base URL for the API, e.g., "https://api.example.com/v2/"
*
* Defaults to process.env['ANTHROPIC_BASE_URL'].
*/
baseURL?: string;
baseURL?: string | null | undefined;

/**
* The maximum amount of time (in milliseconds) that the client should wait for a response
Expand Down Expand Up @@ -82,9 +84,9 @@ export class Anthropic extends Core.APIClient {
/**
* API Client for interfacing with the Anthropic API.
*
* @param {string | null} [opts.apiKey==process.env['ANTHROPIC_API_KEY'] ?? null]
* @param {string | null} [opts.authToken==process.env['ANTHROPIC_AUTH_TOKEN'] ?? null]
* @param {string} [opts.baseURL] - Override the default base URL for the API.
* @param {string | null} [opts.apiKey=process.env['ANTHROPIC_API_KEY'] ?? null]
* @param {string | null} [opts.authToken=process.env['ANTHROPIC_AUTH_TOKEN'] ?? null]
* @param {string} [opts.baseURL=process.env['ANTHROPIC_BASE_URL'] ?? https://api.anthropic.com] - Override the default base URL for the API.
* @param {number} [opts.timeout=10 minutes] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
* @param {number} [opts.httpAgent] - An HTTP agent used to manage HTTP(s) connections.
* @param {Core.Fetch} [opts.fetch] - Specify a custom `fetch` function implementation.
Expand All @@ -93,6 +95,7 @@ export class Anthropic extends Core.APIClient {
* @param {Core.DefaultQuery} opts.defaultQuery - Default query parameters to include with every request to the API.
*/
constructor({
baseURL = Core.readEnv('ANTHROPIC_BASE_URL'),
apiKey = Core.readEnv('ANTHROPIC_API_KEY') ?? null,
authToken = Core.readEnv('ANTHROPIC_AUTH_TOKEN') ?? null,
...opts
Expand All @@ -101,7 +104,7 @@ export class Anthropic extends Core.APIClient {
apiKey,
authToken,
...opts,
baseURL: opts.baseURL ?? `https://api.anthropic.com`,
baseURL: baseURL ?? `https://api.anthropic.com`,
};

super({
Expand Down
15 changes: 15 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ describe('instantiate client', () => {
});
expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/custom/path/foo');
});

afterEach(() => {
process.env['SINK_BASE_URL'] = undefined;
});

test('explicit option', () => {
const client = new Anthropic({ baseURL: 'https://example.com', apiKey: 'my-anthropic-api-key' });
expect(client.baseURL).toEqual('https://example.com');
});

test('env variable', () => {
process.env['ANTHROPIC_BASE_URL'] = 'https://example.com/from_env';
const client = new Anthropic({ apiKey: 'my-anthropic-api-key' });
expect(client.baseURL).toEqual('https://example.com/from_env');
});
});

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

0 comments on commit e084233

Please sign in to comment.