Skip to content

Commit aec801a

Browse files
feat(bedrock): use auth header for mantle client (#866)
use this token in an `authorization: Bearer <key>` header instead of `x-api-key`
1 parent 219a971 commit aec801a

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

packages/bedrock-sdk/src/mantle-client.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export interface BedrockMantleClientOptions extends ClientOptions {
2020
awsRegion?: string | undefined;
2121

2222
/**
23-
* API key for x-api-key authentication.
23+
* API key for Bearer token authentication.
2424
*
2525
* Takes precedence over AWS credential options. If neither `apiKey` nor
2626
* AWS credentials are provided, falls back to the `AWS_BEARER_TOKEN_BEDROCK`
@@ -100,7 +100,7 @@ export class AnthropicBedrockMantle extends BaseAnthropic {
100100
* credentials > `awsProfile` > `AWS_BEARER_TOKEN_BEDROCK` env var > default
101101
* AWS credential chain.
102102
*
103-
* @param {string | undefined} [opts.apiKey] - API key for x-api-key authentication.
103+
* @param {string | undefined} [opts.apiKey] - API key for Bearer token authentication.
104104
* @param {string | null | undefined} [opts.awsAccessKey] - AWS access key ID for SigV4 authentication.
105105
* @param {string | null | undefined} [opts.awsSecretAccessKey] - AWS secret access key for SigV4 authentication.
106106
* @param {string | null | undefined} [opts.awsSessionToken] - AWS session token for temporary credentials.
@@ -188,8 +188,8 @@ export class AnthropicBedrockMantle extends BaseAnthropic {
188188
}
189189

190190
if (!this._useSigV4) {
191-
// API key mode — use inherited x-api-key auth
192-
return super.authHeaders(opts);
191+
// API key / bearer token mode — use Authorization: Bearer header
192+
return buildHeaders([{ Authorization: `Bearer ${this.apiKey}` }]);
193193
}
194194

195195
// SigV4 mode — auth is handled in prepareRequest since it needs the full request

packages/bedrock-sdk/tests/bedrock-mantle.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,52 @@ describe('AnthropicBedrockMantle', () => {
120120
});
121121
});
122122

123+
describe('bearer token auth', () => {
124+
test('sends Authorization: Bearer header when using apiKey', async () => {
125+
const client = new AnthropicBedrockMantle({
126+
apiKey: 'test-bearer-token',
127+
awsRegion: 'us-east-1',
128+
maxRetries: 0,
129+
});
130+
131+
await makeRequest(client);
132+
133+
const requestInit = mockFetch.mock.calls[0]![1] as RequestInit;
134+
const headers = new Headers(requestInit.headers as HeadersInit);
135+
expect(headers.get('authorization')).toBe('Bearer test-bearer-token');
136+
expect(headers.get('x-api-key')).toBeNull();
137+
});
138+
139+
test('sends Authorization: Bearer header when using AWS_BEARER_TOKEN_BEDROCK env var', async () => {
140+
process.env['AWS_BEARER_TOKEN_BEDROCK'] = 'env-bearer-token';
141+
142+
const client = new AnthropicBedrockMantle({
143+
awsRegion: 'us-east-1',
144+
maxRetries: 0,
145+
});
146+
147+
await makeRequest(client);
148+
149+
const requestInit = mockFetch.mock.calls[0]![1] as RequestInit;
150+
const headers = new Headers(requestInit.headers as HeadersInit);
151+
expect(headers.get('authorization')).toBe('Bearer env-bearer-token');
152+
expect(headers.get('x-api-key')).toBeNull();
153+
});
154+
155+
test('does not send Authorization: Bearer header when using SigV4', async () => {
156+
const client = new AnthropicBedrockMantle({
157+
awsAccessKey: 'my-access-key',
158+
awsSecretAccessKey: 'my-secret-key',
159+
awsRegion: 'us-east-1',
160+
maxRetries: 0,
161+
});
162+
163+
await makeRequest(client);
164+
165+
expect(mockGetAuthHeaders).toHaveBeenCalledTimes(1);
166+
});
167+
});
168+
123169
describe('endpoint restrictions', () => {
124170
test('completions resource is not available', () => {
125171
const client = new AnthropicBedrockMantle({

0 commit comments

Comments
 (0)