Support using OPENAI_API_KEY without writing to auth.json
What feature would you like to see?
It would be great if you could just directly use OPENAI_API_KEY instead of simply prefilling the API key field during onboarding for it to be written to auth.json. This is useful if you don't want to store the key on disk (at least unencrypted) and set the environment variable from a source (e.g. secret store). Doing this before running codex for the first time and then just having it be written to auth.json defeats the point.
Additional information
This can be worked around by defining your own model provider as the auth.json and input field behaviour is special-cased for the default openai model provider. For example:
model_provider = "openai-env-var"
[model_providers.openai-env-var]
name = "OpenAI (OPENAI_API_KEY)"
base_url = "https://api.openai.com/v1"
env_key = "OPENAI_API_KEY"
wire_api = "responses"
I assume there is either currently or will be functionality that this disables that using the default model provider allows (OpenAI-specific features) though so ideally it would be possible to do this without a custom model provider (and would also be simpler to configure).
7 Comments
@nihaals thanks for providing the snippet on how to do that with a custom provider. It's not idea, and I 👍 this issue b/c I'd like the process to be simpler, but very helpful anyway to have a workaround documented.
Older versions of the CLI supported implicit logins using
OPENAI_API_KEYwhen using the default model provider. We removed this in favor of explicit logins. Implicit logins result in a bunch of other complications and undesirable behaviors, especially given that codex auth is shared between the CLI and the VS Code extension.The CLI still supports implicit login using environment variables with custom model providers and with
execmode.I think it's unlikely that we'll add back support for implicit logins when using the default model provider — at least not with the
OPENAI_API_KEYenvironment variable.I understand your concern about having the key written to disk in an unencrypted form. We're looking at ways to better secure it.
Using the OS secret store (keychain, secret service etc.) would be fine for me, although that might be part of a new issue in case some people have a different use-case that needs something like this (e.g. rotating API keys/short-term API keys, although a non-interactive
OPENAI_API_KEY=... codex logincould work for this).I'm now using multiple profiles each using a different OpenAI API key so I would also want to be able to do that with the secret store implementation, although if there aren't any plans of breaking
env_keythen I could also just continue to use my current setup instead of the secret store.FWIW, my use case is for running Codex in Docker. I have the API key loaded from 1Pass in my terminal session and it gets passed through to the container. I'd like to fully script the run so any kind of required interaction is problematic. I also, as good security practice, try to avoid ever having secrets exposed in plain text files anywhere on the file system.
I don't know the problems with "implicit logins" but I trust they are there. But it's not too difficult to support using the API key and making that explicit:
codex --use-api-keyOr as a config parameter:
That way it's still explicit but less hassle to configure than needing to config a custom provider. I wouldn't have even known that was an option if I hadn't found this issue and the example by the OP.
I see this as a sub-issue of the security of the
.codexfolder. Currently, any accidental inclusion of the.codexfolder into a public GitHub project can have security implications. Meanwhile, users might want to have a per-project.codexfor several reasons: per-project flexibility of API key and/or billing and/or alternative providers. A second reason is the desire to preserve the conversation logs for various purposes. It hints to me that having a global-local split and allowing inheritable.codexmight solve the issue. It would be like VSCode's configurations being split into user vs workspace. It might be a hassle but not solving this issue makes it a security risk.btw .. would the issue labels
authandcustom-modelapply to this issue?This decision is counter intuitive. Many tools on Linux prefer environment variables because they are ephemeral and more secure.
If you don't want to reconsider the decision to remove support for OPENAI_API_KEY, please at least continue to support the workaround that is provided in the first comment of this issue.
A quick note about security. If you supply the OPENAI_API_KEY environment variable, it's probably a good idea to remove it before executing commands from the Codex CLI. You have to add the following in your
config.toml: