Local Codex CLI sessions can use OpenAI Platform billing rather than ChatGPT subscription access. API-key login authenticates the local CLI with a Platform credential for private runners, remote shells, and terminal automation.
Codex cloud requires ChatGPT sign-in; an API key does not grant cloud access. In the CLI, codex login --with-api-key reads a Platform API key from standard input and caches the login locally. The CLI and IDE extension share cached local login details.
API-key usage follows the API organization's retention and data-sharing settings and is charged at standard API rates. The CLI imports the key once, so later starts can use the cached API-key profile without leaving the raw key in the shell environment. Features that rely on ChatGPT workspace access or cloud services can be limited or unavailable with API-key authentication.
Steps to log in to Codex CLI with an API key:
- Check the current Codex login state in the same shell that will store the new API-key session.
$ codex login status Not logged in
If the output shows an unexpected account or sign-in method, log out before loading the new API key.
Related: How to log out of Codex - Obtain a secret API key from the OpenAI Platform dashboard.
https://platform.openai.com/api-keys
A leaked key can authorize billable API use, so keep it out of tickets, screenshots, logs, and committed files. A literal shell assignment can also save it in shell history.
- Read the key into OPENAI_API_KEY with hidden input in Bash or Zsh.
A terminal recorder that logs input can save the API key even when characters are hidden, exposing it to anyone who can read the recording. Shell tracing can expose the expanded key in the next command; this method assumes tracing and terminal-session recording are off before key entry.
$ read -r -s OPENAI_API_KEY
The command waits for the key followed by Enter; pasted characters are not displayed. The key is read as input rather than a shell command, so it does not enter shell history. This does not require exporting the variable.
- Pipe the API key to Codex CLI through standard input.
$ printf '%s\n' "$OPENAI_API_KEY" | codex login --with-api-key Reading API key from stdin... API key saved. Authentication is checked when you send a request.
The CLI caches the key for later sessions; saving it does not validate it with OpenAI. If it uses auth.json under CODEX_HOME instead of an operating-system credential store, that file contains credential material and needs the same protection as the key.
- Remove the temporary key variable from the current shell.
$ unset OPENAI_API_KEY
Unsetting the variable does not log Codex out because the CLI already cached the login locally.
- Confirm the cached profile still shows API-key authentication.
$ codex login status API key configured - sk-proj-***ABCD1 Authentication is checked when you send a request.
The masked suffix will differ for each key. This confirms the saved authentication method; it does not verify key validity, API billing, or model access.
Related: How to check Codex login status