Skip to content

Conversation

deyaaeldeen
Copy link
Contributor

  • I understand that this repository is auto-generated and my pull request may not be merged

Changes being requested

This PR introduces a new tokenProvider option to the OpenAI client, enabling dynamic token retrieval for authentication scenarios where API keys need to be refreshed or obtained at runtime.

Additional context & links

Changes

New Feature: tokenProvider Option

  • Added TokenProvider type definition: () => Promise<AccessToken>
  • Added AccessToken interface with token: string property
  • Added tokenProvider option to ClientOptions interface
  • Implemented token provider logic in the _setToken() method

Key Benefits

  1. Dynamic Authentication: Supports scenarios where tokens expire and need to be refreshed
  2. Enhanced Security: Tokens can be retrieved just-in-time rather than stored statically
  3. Enterprise Integration: Enables integration with corporate identity providers and token management systems
  4. Flexible Authentication: Provides an alternative to static API keys for environments requiring dynamic credentials

Implementation Details

Client Construction:

  • tokenProvider and apiKey are mutually exclusive - only one can be provided
  • If neither is provided, an error is thrown requiring one of the two authentication methods
  • Browser usage automatically allowed when using tokenProvider (sets dangerouslyAllowBrowser: true)

Token Management:

  • _setToken() method now returns a boolean indicating if a token was successfully retrieved
  • Token provider is called on every request preparation via prepareOptions()

Usage Example

const client = new OpenAI({
  tokenProvider: async () => {
    // Fetch token from your auth service
    const response = await fetch('/api/auth/token');
    const data = await response.json();
    return { token: data.access_token };
  }
});

Backward Compatibility

This change is fully backward compatible. Existing code using apiKey continues to work unchanged. The new tokenProvider option is purely additive.


@deyaaeldeen deyaaeldeen force-pushed the feat/token-provider branch 2 times, most recently from b4dd476 to f514473 Compare July 16, 2025 23:40
@deyaaeldeen deyaaeldeen force-pushed the feat/token-provider branch 2 times, most recently from 1453757 to b623760 Compare August 22, 2025 18:09
Copy link
Collaborator

@RobertCraigie RobertCraigie left a comment

Choose a reason for hiding this comment

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

Looks great!

@deyaaeldeen deyaaeldeen changed the base branch from master to next August 28, 2025 19:39
@deyaaeldeen deyaaeldeen marked this pull request as ready for review August 28, 2025 19:39
@deyaaeldeen deyaaeldeen requested a review from a team as a code owner August 28, 2025 19:39
@RobertCraigie RobertCraigie changed the title add support for token provider feat(client): support api key provider functions Sep 2, 2025
@RobertCraigie RobertCraigie merged commit c3a92c7 into openai:next Sep 2, 2025
4 of 5 checks passed
@stainless-app stainless-app bot mentioned this pull request Sep 2, 2025
if (token) {
opts.headers.values.set('Authorization', `Bearer ${token}`);
} else if (this.apiKey !== API_KEY_SENTINEL) {
opts.headers.values.set('api-key', this.apiKey);
Copy link

Choose a reason for hiding this comment

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

Why api-key header support is removed? related #1633

@deyaaeldeen deyaaeldeen deleted the feat/token-provider branch September 3, 2025 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants