API Key Security Feature Requests

Resolved 💬 13 comments Opened Apr 16, 2025 by asmeurer Closed Aug 7, 2025
💡 Likely answer: A maintainer (jonchurch, contributor) responded on this thread — see the highlighted reply below.

When you start codex it asks you to put your API key in an environment variable:

Missing OpenAI API key.

Set the environment variable OPENAI_API_KEY and re-run this command.
You can create a key here: https://platform.openai.com/account/api-keys

It would be preferable if it just prompted you for a key and it stored it securely, i.e., in the OS keystore (like the macOS keychain). It would also be better to use a bespoke key for codex rather than just a generic "openai API key".

It would also be nice if it just opened the browser to a page where you authenticate and generated a key for you (Claude Code does this).

View original on GitHub ↗

13 Comments

brianlovin · 1 year ago

The current message to set an environment variable is unclear — where should I set it? I am running codex on a directory with a .env file that defines an OPENAI_API_KEY so it must be looking somewhere else. Adding more information about how to set this up in the short term would be useful!

jonchurch contributor · 1 year ago

This is a grab bag issue of API Token security requests, I'd retitle this "API Key Security Feature Requests" or something.

I specifically want to know what permissions are required, instead of setting a full access token on my env

slhck · 1 year ago

I second this. Tools like llm also store the keys for different services internally.

There's a huge security implication with just using environment variables. If I want to run codex in any terminal session, it would require me to set an API key in my environment for _all_ terminal sessions, meaning that _every_ command-line application I run could possibly have access to that key.

mchatzis · 1 year ago

A temporary solution to avoid setting a global environment variable is to build from source and change this line to your api key (hard-coding it)

file: codex-cli/src/cli.tsx

line 142: const apiKey = process.env["OPENAI_API_KEY"]; // Use your api key here
mohit-s96 · 1 year ago

if someone needs a solution: https://github.com/mohit-s96/codex/pull/1. it uses node-keytar.

antoniocascais · 1 year ago

It would be great if the API key could also be read from a file, in addition to environment variables.
This would help avoid storing sensitive keys in shell history via export.

tibo-openai collaborator · 1 year ago

Would accept a contribution here that does this in a principled way and +1 to

It would be preferable if it just prompted you for a key and it stored it securely, i.e., in the OS keystore (like the macOS keychain).

Perhaps
1) If OPENAI_API_KEY env var is already set, use that (what is currently done)
2) If .env file defines OPENAI_API_KEY, then we should support that and use that
3) If not set, suggest to create a new api key for codex-cli and paste it in, then store this securely, e.g. keychain on macOS. If we can't store securely automatically, then ask user to store it in a file or their ~/.bashrc, but warn about potential downside of that.

Thoughts?

mohit-s96 · 1 year ago
If .env file defines OPENAI_API_KEY, then we should support that and use that

could be ambiguous as to which key source is being used (keychain vs env variable vs cwd .env file). it could show a message with the source of the key?

tibo-openai collaborator · 1 year ago
asmeurer · 1 year ago

I'm not a security expert. I personally would only do it the secure way, unless you have some major reason not to. The tool is new enough that making people switch is not a big deal. But if you don't want to deprecate the environment variable I'm happy to at least have a way to not have to use it.

mwolson · 1 year ago

I'd also like to see the ability for ~/.codex/config.yaml to specify a file path that's a global-level default .env file if possible.

asmeurer · 1 year ago

I see now that codex lets you sign in directly with ChatGPT, which is an improvement. Although the auth token is still stored in a plain text file, and also this does nothing for the use-case of using other providers.

codex-maintainers · 11 months ago

Thanks for the feedback! We recommend using "Sign in with ChatGPT" or you can directly set the API key as an environment variable when you run codex rather than in all sessions if you prefer.