feat: support for dedicated configuration file for API keys

Resolved 💬 6 comments Opened Apr 23, 2025 by tim-watcha Closed Aug 7, 2025

Current Behavior

Currently, the codex project supports setting the OpenAI API key in two ways:

  1. As an environment variable: export OPENAI_API_KEY="your-api-key-here"
  2. In a .env file at the root of the project: OPENAI_API_KEY=your-api-key-here

Requested Feature

Add support for storing the OpenAI API key in a dedicated, user-wide configuration file (e.g., ~/.codex.config) that would be specific to the codex tool.

Motivation

  • Persistence: Avoids having to re-export environment variables in every new terminal session
  • Separation of concerns: Keeps API keys separate from project files and specific to the codex tool
  • Security: Provides a dedicated location for sensitive credentials outside of project directories
  • User convenience: Makes it easier for users who work with multiple projects but want a consistent API key setup

Proposed Implementation

Add the ability to read OpenAI API keys from a dedicated configuration file at ~/.codex.config (or similar location). For example:

# ~/.codex.config
OPENAI_API_KEY=your-api-key-here

The configuration file could use a simple format like the existing .env approach or a more structured format like YAML/JSON if additional configuration options might be needed in the future.

View original on GitHub ↗

6 Comments

bradenkeith · 1 year ago

You can add the export line to your shell's configuration file (e.g., ~/.zshrc) to persist through terminal sessions.

WASasquatch · 1 year ago

And if you mean, for like separate organization keys, you should use Docker or something, so you can load up .env files.

tim-watcha · 1 year ago
And if you mean, for like separate organization keys, you should use Docker or something, so you can load up .env files.

While using Docker is a good suggestion, having a dedicated configuration file as proposed would be more convenient for those who plan to use codex consistently.

This approach is particularly useful when:

  • You want to use a separate API key dedicated to codex to independently track usage
  • You work across multiple projects or organizations but want to consistently use the same key for codex
  • You want to reduce the complexity of managing project-specific .env files or environment variables

Having a ~/.codex.config file would allow for simple and consistent configuration in these scenarios.

WASasquatch · 1 year ago
> And if you mean, for like separate organization keys, you should use Docker or something, so you can load up .env files. While using Docker is a good suggestion, having a dedicated configuration file as proposed would be more convenient for those who plan to use codex consistently. This approach is particularly useful when: You want to use a separate API key dedicated to codex to independently track usage You work across multiple projects or organizations but want to consistently use the same key for codex * You want to reduce the complexity of managing project-specific .env files or environment variables Having a ~/.codex.config file would allow for simple and consistent configuration in these scenarios.

Having keys outside environment variables is not recommended with basically any provider, though. It's a security risk.

codex-maintainers · 1 year ago

Thank you for the feedback! We're primarily focused on the native/Rust implementation of Codex CLI. Please check if this feature is still needed on the latest version and consider contributing to the codex-rs/ implementation if it is.

stevenlafl · 1 year ago

Anyone finding this:

In ~/.codex/auth.json:

{
  "OPENAI_API_KEY": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}