From d1c0d5e6838bc3fe4d63e04e55068985bc78f872 Mon Sep 17 00:00:00 2001 From: Asa Date: Fri, 25 Apr 2025 02:08:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20update=20README=20and=20config=20to=20s?= =?UTF-8?q?upport=20custom=20providers=20with=20API=20k=E2=80=A6=20(#577)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When using a non-built-in provider with the `--provider` option, users are prompted: ``` Set the environment variable _API_KEY and re-run this command. You can create a _API_KEY in the dashboard. ``` However, many users are confused because, even after correctly setting `_API_KEY`, authentication may still fail unless `OPENAI_API_KEY` is _also_ present in the environment. This is not intuitive and leads to ambiguity about which API key is actually required and used as a fallback, especially when using custom or third-party (non-listed) providers. Furthermore, the original README/documentation did not mention the requirement to set `_BASE_URL` for non-built-in providers, which is necessary for proper client behavior. This omission made the configuration process more difficult for users trying to integrate with custom endpoints. --- README.md | 7 +++++++ codex-cli/src/utils/config.ts | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/README.md b/README.md index ab4701bf..32a73bdc 100644 --- a/README.md +++ b/README.md @@ -97,12 +97,19 @@ export OPENAI_API_KEY="your-api-key-here" > - deepseek > - xai > - groq +> - any other provider that is compatible with the OpenAI API > > If you use a provider other than OpenAI, you will need to set the API key for the provider in the config file or in the environment variable as: > > ```shell > export _API_KEY="your-api-key-here" > ``` +> +> If you use a provider not listed above, you must also set the base URL for the provider: +> +> ```shell +> export _BASE_URL="https://your-provider-api-base-url" +> ```
diff --git a/codex-cli/src/utils/config.ts b/codex-cli/src/utils/config.ts index 1df2e197..7085b6fc 100644 --- a/codex-cli/src/utils/config.ts +++ b/codex-cli/src/utils/config.ts @@ -76,6 +76,12 @@ export function getApiKey(provider: string = "openai"): string | undefined { return process.env[providerInfo.envKey]; } + // Checking `PROVIDER_API_KEY feels more intuitive with a custom provider. + const customApiKey = process.env[`${provider.toUpperCase()}_API_KEY`]; + if (customApiKey) { + return customApiKey; + } + // If the provider not found in the providers list and `OPENAI_API_KEY` is set, use it if (OPENAI_API_KEY !== "") { return OPENAI_API_KEY;