You can extend your local Codex usage (CLI and IDE extension) with an API key. API key usage is billed through your OpenAI platform account at the standard API rates, which you can review on the API pricing page.
First, make sure you set up your OPENAI_API_KEY environment variable globally. You can get your API key from the OpenAI dashboard.
Then, you can use the CLI and IDE extension with your API key.
If you’ve previously used the Codex CLI with an API key, update to the latest version, run codex logout, and then run codex to switch back to subscription-based access when you’re ready.
Use your API key with Codex CLI
You can change which auth method to use with the CLI by changing the preferred_auth_method in the codex config file:
# ~/.codex/config.toml
preferred_auth_method = "apikey"
You can also override it ad-hoc via CLI:
codex --config preferred_auth_method="apikey"
You can go back to ChatGPT auth (default) by running:
codex --config preferred_auth_method="chatgpt"
You can switch back and forth as needed, for example if you use your ChatGPT account but run out of usage credits.
Use your API key with the IDE extension
When you open the IDE extension, you’ll be prompted to sign in with your ChatGPT account or to use your API key instead. If you wish to use your API key instead, you can select the option to use your API key. Make sure it is configured in your environment variables.