Skip to content

Commit 2601e9f

Browse files
deyaaeldeenstainless-app[bot]
authored andcommitted
fix(azure): correctly send API key (#1635)
1 parent 0430acd commit 2601e9f

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/azure.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import type { RequestInit } from './internal/builtin-types';
2+
import type { NullableHeaders } from './internal/headers';
3+
import { buildHeaders } from './internal/headers';
24
import * as Errors from './error';
35
import { FinalRequestOptions } from './internal/request-options';
46
import { isObj, readEnv } from './internal/utils';
@@ -134,6 +136,13 @@ export class AzureOpenAI extends OpenAI {
134136
}
135137
return super.buildRequest(options, props);
136138
}
139+
140+
protected override async authHeaders(opts: FinalRequestOptions): Promise<NullableHeaders | undefined> {
141+
if (typeof this._options.apiKey === 'string') {
142+
return buildHeaders([{ 'api-key': this.apiKey }]);
143+
}
144+
return super.authHeaders(opts);
145+
}
137146
}
138147

139148
const _deployments_endpoints = new Set([

src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ export class OpenAI {
331331
private fetch: Fetch;
332332
#encoder: Opts.RequestEncoder;
333333
protected idempotencyHeader?: string;
334-
private _options: ClientOptions;
334+
protected _options: ClientOptions;
335335

336336
/**
337337
* API Client for interfacing with the OpenAI API.

tests/lib/azure.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,22 @@ describe('instantiate azure client', () => {
307307
});
308308
});
309309

310+
test('uses api-key header when apiKey is provided', async () => {
311+
const testFetch = async (url: RequestInfo, { headers }: RequestInit = {}): Promise<Response> => {
312+
return new Response(JSON.stringify({ a: 1 }), { headers: headers ?? [] });
313+
};
314+
const client = new AzureOpenAI({
315+
baseURL: 'http://localhost:5000/',
316+
apiKey: 'My API Key',
317+
apiVersion,
318+
fetch: testFetch,
319+
});
320+
321+
const res = await client.request({ method: 'post', path: 'https://example.com' }).asResponse();
322+
expect(res.headers.get('api-key')).toEqual('My API Key');
323+
expect(res.headers.get('authorization')).toEqual(null);
324+
});
325+
310326
test('with endpoint', () => {
311327
const client = new AzureOpenAI({ endpoint: 'https://example.com', apiKey: 'My API Key', apiVersion });
312328
expect(client.baseURL).toEqual('https://example.com/openai');

0 commit comments

Comments
 (0)