Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

feat: update README and config to support custom providers with API k… (#577)

Asad1c0d5e6838b
diff --git a/README.md b/README.md
index ab4701bf7d..32a73bdcdb 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 <provider>_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 <provider>_BASE_URL="https://your-provider-api-base-url"
+> ```
</details>
<br />
diff --git a/codex-cli/src/utils/config.ts b/codex-cli/src/utils/config.ts
index 1df2e197c7..7085b6fcb1 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;