# Claude Code API key: sign-in vs ANTHROPIC_API_KEY

> How Claude Code picks between subscription sign-in and an API key, how apiKeyHelper works, where macOS stores credentials, and how to keep keys out of logs.

You do not need an API key to use Claude Code. Run `claude` and sign in with a Claude Pro, Max, Ultra, Team or Enterprise account, and Claude Code stores that login in the macOS Keychain. If you set `ANTHROPIC_API_KEY` in your environment, Claude Code asks once whether to use it and then uses that key instead of your subscription. For rotating keys, the `apiKeyHelper` setting runs a script of yours to fetch one.

## How Claude Code chooses a credential

When several credentials exist, the authentication page lists this order, highest first.

1. Cloud provider credentials, when `CLAUDE_CODE_USE_BEDROCK`, `CLAUDE_CODE_USE_VERTEX` or `CLAUDE_CODE_USE_FOUNDRY` is set.
2. `ANTHROPIC_AUTH_TOKEN`, sent as a bearer token. It suits an LLM gateway.
3. `ANTHROPIC_API_KEY`, sent as `X-Api-Key`.
4. The output of the `apiKeyHelper` script.
5. `CLAUDE_CODE_OAUTH_TOKEN`, a long-lived token from `claude setup-token`.
6. Anthropic profile and federation credentials.
7. Subscription OAuth credentials from `/login`, the default for subscription plans.

In an interactive session you approve or decline `ANTHROPIC_API_KEY` once, and the choice is remembered. In non-interactive `-p` mode the key is always used when present. The docs warn that a key belonging to a disabled or expired organization can cause authentication failures. Run `unset ANTHROPIC_API_KEY` to fall back to your subscription, then check `/status` to confirm which method is active.

## apiKeyHelper

`apiKeyHelper` is a settings key. Its value is a shell command that prints a credential.

```json
{
  "apiKeyHelper": "/bin/generate_temp_api_key.sh"
}
```

Claude Code runs it through the system shell and sends the output as both the `X-Api-Key` and `Authorization: Bearer` headers. It caches the value and re-runs the command after five minutes by default. You can change that with `CLAUDE_CODE_API_KEY_HELPER_TTL_MS`. It also re-runs after a `401` or `403` from the Anthropic API. If a helper takes more than 10 seconds, Claude Code shows a warning in the prompt bar. When the command comes from project or local settings, an interactive session does not run it until you accept the workspace trust prompt.

## Where credentials are stored on macOS

On macOS, credentials go in the encrypted Keychain. If the Keychain rejects the write, such as when it is locked in an SSH session, Claude Code falls back to `~/.claude/.credentials.json` with file mode `0600`. If you set `CLAUDE_CONFIG_DIR`, the file lives under that directory and the Keychain entry is keyed to it. A one-year token from `claude setup-token` is printed to the terminal and saved nowhere, so you copy it into `CLAUDE_CODE_OAUTH_TOKEN` yourself.

The installed help for Claude Code 2.1.322 lists `claude auth login`, `claude auth logout` and `claude auth status`.

## Keep keys out of logs and repos

Transcripts are plaintext. The Claude directory documentation says that if a tool reads a `.env` file or a command prints a credential, the value is written to `~/.claude/projects/<project>/<session>.jsonl`. A key is just as exposed when it sits in `env` in a committed `.claude/settings.json`. Plain-text values in `env` reach every subprocess.

- Prefer sign-in or `apiKeyHelper` over a literal key in a file.
- Deny reads of credential files with permission rules. The settings page shows `Read(./.env)` and `Read(./.env.*)` in `permissions.deny`.
- Set `CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=1` to strip credentials from the environments of Bash commands, hooks and stdio MCP servers.
- Shorten transcript retention with `cleanupPeriodDays`. The default is 30 days.
- Keep `.claude/settings.local.json` out of git. Claude Code adds it to your global git excludes when it writes the file. If you create it by hand, add it to `.gitignore` yourself.

Checked against the vendor documentation on November 25, 2026. [Authentication](https://code.claude.com/docs/en/authentication) · [Claude directory](https://code.claude.com/docs/en/claude-directory) · [Settings](https://code.claude.com/docs/en/settings) · [Environment variables](https://code.claude.com/docs/en/env-vars)

## How Excellent fits

Excellent’s free scan reads supported logs on the Mac and reports findings for review. It reads Claude Code session logs, so it can show where a key landed in a transcript. For a local report without installing an app, [run the free scan](https://www.excellent.so/scan). Its script uses Python 3.9 or later when your Mac has it and the shell when it doesn’t, reads the logs locally and makes no network requests. [See a sample report](https://www.excellent.so/r/sample).

Excellent Pro adds checks before supported actions. It blocks keys typed into commands and asks before reading key files. Pro is US-only for now. [Pricing](https://www.excellent.so/pricing) · [Keeping keys and your email out of agent logs](https://www.excellent.so/guides/keep-keys-and-email-out-of-agent-logs) · [Key rules](https://www.excellent.so/docs/keys)

Source: https://www.excellent.so/guides/claude-code-api-key
