Skip to content

feat(anthropic): add authToken option for Bearer authentication#11782

Merged
aayush-kapoor merged 3 commits intovercel:mainfrom
jarod:feat/anthropic-auth-token
Jan 22, 2026
Merged

feat(anthropic): add authToken option for Bearer authentication#11782
aayush-kapoor merged 3 commits intovercel:mainfrom
jarod:feat/anthropic-auth-token

Conversation

@jarod
Copy link
Copy Markdown
Contributor

@jarod jarod commented Jan 14, 2026

Background

Some use cases require Authorization: Bearer header authentication instead of the x-api-key header when using the Anthropic API. This is particularly useful when working with proxy services or custom API gateways that expect Bearer token authentication.

Summary

Added a new authToken option to the Anthropic provider that enables Authorization: Bearer header authentication as an alternative to the existing x-api-key authentication method.

Changes:

  • Added authToken option to AnthropicProviderSettings
  • Added support for ANTHROPIC_AUTH_TOKEN environment variable
  • When authToken is provided, uses Authorization: Bearer header instead of x-api-key

Manual Verification

Tested locally by configuring the provider with authToken option and verifying the correct Authorization: Bearer header is sent in API requests.

Checklist

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • I have reviewed this pull request (self-review)

@vercel-ai-sdk vercel-ai-sdk bot added ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label provider/anthropic Issues related to the @ai-sdk/anthropic provider labels Jan 14, 2026
Comment thread packages/anthropic/src/anthropic-provider.ts Outdated
Comment thread packages/anthropic/src/anthropic-provider.ts Outdated
Comment thread packages/anthropic/src/anthropic-provider.ts Outdated
@jarod jarod force-pushed the feat/anthropic-auth-token branch from 12324c0 to 15c19b1 Compare January 14, 2026 07:25
@aayush-kapoor aayush-kapoor self-assigned this Jan 14, 2026
Copy link
Copy Markdown
Collaborator

@aayush-kapoor aayush-kapoor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please add unit tests for this behaviour as well as an e2e cli example (example/ai-functions/src/generate-text)

@jarod jarod force-pushed the feat/anthropic-auth-token branch from 15c19b1 to cee5441 Compare January 15, 2026 07:16
Comment on lines +121 to +127
if (authToken && apiKey) {
throw new InvalidArgumentError({
argument: 'apiKey/authToken',
message:
'Both apiKey and authToken were provided. Please use only one authentication method.',
});
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this check leads to an error if the apiKey and authToken are defined in the .env file (and not the actual client file)

one can have both apiKey and authToken in the .env file and still only use one

this error should only pop up if the user passed both while initialising the provider

@aayush-kapoor
Copy link
Copy Markdown
Collaborator

i'll make a fix and push it

@aayush-kapoor
Copy link
Copy Markdown
Collaborator

i assume you tested it with a URL that will accept the proper bearer token?

@aayush-kapoor aayush-kapoor merged commit 8ccf04b into vercel:main Jan 22, 2026
16 of 17 checks passed
B-Step62 pushed a commit to B-Step62/ai that referenced this pull request Jan 28, 2026
…el#11782)

## Background

Some use cases require `Authorization: Bearer` header authentication
instead of the `x-api-key` header when using the Anthropic API. This is
particularly useful when working with proxy services or custom API
gateways that expect Bearer token authentication.

## Summary

Added a new `authToken` option to the Anthropic provider that enables
`Authorization: Bearer` header authentication as an alternative to the
existing `x-api-key` authentication method.

Changes:
- Added `authToken` option to `AnthropicProviderSettings`
- Added support for `ANTHROPIC_AUTH_TOKEN` environment variable
- When `authToken` is provided, uses `Authorization: Bearer` header
instead of `x-api-key`

## Manual Verification

Tested locally by configuring the provider with `authToken` option and
verifying the correct `Authorization: Bearer` header is sent in API
requests.

## Checklist

- [x] Tests have been added / updated (for bug fixes / features)
- [ ] Documentation has been added / updated (for bug fixes / features)
- [x] A _patch_ changeset for relevant packages has been added (for bug
fixes / features - run `pnpm changeset` in the project root)
- [x] I have reviewed this pull request (self-review)

---------

Co-authored-by: Aayush Kapoor <[email protected]>
Co-authored-by: Aayush Kapoor <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label provider/anthropic Issues related to the @ai-sdk/anthropic provider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants