Skip to content
← All guides

Codex API key: sign-in, auth.json and keeping keys safe

How Codex signs in with ChatGPT or an API key, where it stores credentials, how to keep a key out of config and logs, and how to check login status.

4 min read

Codex supports two sign-in methods. Sign in with ChatGPT for subscription access, or sign in with an API key for usage-based billing through your OpenAI Platform account. To use a key, pipe it into codex login --with-api-key. Codex then caches the login in ~/.codex/auth.json or in your OS credential store. OpenAI says to treat that file like a password.

Sign in with a key

The CLI reference gives this example, which reads the key from standard input so it does not appear as a command argument.

printenv OPENAI_API_KEY | codex login --with-api-key

Run codex login status to print the active authentication mode. It exits with 0 when you are logged in, which helps in scripts. codex logout removes saved credentials for both API key and ChatGPT sign-in. The installed help for Codex CLI 0.166.0 also lists --device-auth and --with-access-token on codex login.

The authentication docs show the key being piped in. They do not say Codex reads OPENAI_API_KEY from the environment on its own, so do not assume it does.

Which method to use

ChatGPT sign-inAPI key
BillingYour ChatGPT planStandard API rates on your Platform account
FitInteractive useProgrammatic work such as CI/CD
LimitsNone notedSome features that need ChatGPT workspace access or cloud services are limited or unavailable

The docs recommend API key auth for programmatic workflows and say not to run Codex execution in untrusted or public environments.

Where credentials are stored

The docs say Codex caches login details locally in a plaintext file at ~/.codex/auth.json or in your OS-specific credential store. The file lives under CODEX_HOME, which defaults to ~/.codex. You choose the store with cli_auth_credentials_store.

ValueBehavior
fileStores credentials in auth.json under CODEX_HOME
keyringUses the OS credential store and fails if it is unavailable
autoUses the OS credential store when available, otherwise auth.json
ephemeralKeeps credentials in memory for the current process only
# ~/.codex/config.toml
cli_auth_credentials_store = "keyring"

On a Mac, keyring keeps the token out of a plaintext file. The CLI and IDE extension share cached logins, so logging out of one requires signing in again.

Keep the key out of logs and repos

The authentication docs say not to commit auth.json, paste it into tickets or share it in chat. Add a few habits.

  • Pipe the key in. Never type it into a command line, which lands in shell history.
  • Keep .codex/ and auth.json out of any repo you push.
  • Keep the key out of the environment the agent’s shell inherits. The shell_environment_policy setting can set inherit = "core", and by default variables whose names contain KEY, SECRET or TOKEN are removed before other filters when ignore_default_excludes is true.
  • Do not paste a key into a prompt. Codex session files record what you typed and what commands printed.

That last point matters. A key that reaches a prompt or a command’s output can end up in a session file on disk. See Where Codex keeps session logs and Keeping keys and your email out of agent logs.

Restrict the method for a team

Admins can set forced_login_method to chatgpt or api. The docs say that if the active credentials do not match, Codex logs the user out and exits.

Checked against the vendor documentation on November 25, 2026. Authentication · CLI reference · Config reference · Codex config.toml guide

Find keys already in your logs

Excellent’s free scan reads supported logs on the Mac and reports findings for review, including keys found in logs. For a local report without installing an app, run the free scan. See a sample report. With Pro, Excellent blocks some steps, such as keys typed into commands, and asks before others, such as reading key files. Agent coverage · Pricing. Pro is US-only for now.

Related guides

View all