To use Claude Code with an API key, configure an authorized Anthropic developer credential in the terminal environment, launch the CLI and verify the selected authentication method. This guide focuses on CLI-specific setup and billing, including how to return to subscription access without accidentally charging an API account. If you intend to continue using your subscription after hitting its allowance, our Claude Code credits guide explains the separate usage balance and spending controls.
Should you use Claude Code with an API key?
Claude Code’s terminal client supports direct Anthropic API authentication. This can suit a project with separately budgeted API usage, an approved automation environment, or a team that needs its requests assigned to a specific API account. If you intend to use the allowance of your Claude subscription, leave the API key unset.
An API key is not a way to obtain free or unlimited Claude Code access. Requests made with an approved developer key are billed to its API account according to applicable pricing and limits. Before starting a session, confirm the intended workspace, available budget and authentication method.
Step 1: Get a Claude Code API key
Sign in to the Claude Console and create an authorized developer API key in the appropriate organization and workspace. Select the permissions and expiration options available for your account. Copy the secret into an approved secret manager rather than a project file or chat message.
Follow our Claude API keys guide for the full creation and rotation process. Claude Code can use that developer credential; it does not require a separate key format for the terminal client.
Step 2: Set the key temporarily in your terminal
Install the official standalone CLI if necessary, following our Claude Code terminal guide. On a trusted machine, open the intended project and set the credential for the session in which it is required.
For Bash on macOS or Linux, the following example reads the key without echoing it to the terminal. Paste your authorized secret at the hidden prompt.
read -r -s -p "Anthropic API key: " ANTHROPIC_API_KEY
printf "\n"
export ANTHROPIC_API_KEY
claude
If you normally use Zsh, launch Bash for this example or use an equivalent hidden-input mechanism in your own shell. In Windows PowerShell 7, use:
$env:ANTHROPIC_API_KEY = Read-Host "Anthropic API key" -MaskInput
claude
Do not substitute a literal key into a command that will be saved in shell history. These examples set the credential in the current shell’s environment rather than a permanent user or machine setting. Processes started from that environment may inherit the credential, so avoid launching unrelated or untrusted programs there.
Step 3: Confirm which credential Claude Code uses
When the interactive CLI detects a custom API key, it can ask whether to approve that credential. Approve it only if you intend to use the associated API account. Anthropic also documents a Use custom API key toggle in /config that appears while the variable is set.
Inside Claude Code, run:
/status
Verify the selected authentication method. A signed-in subscription does not prove that the current request uses subscription access. Cloud-provider settings, gateway credentials or other authentication sources may take priority. Managed policies can also restrict available login methods.
Do not print your key to check whether it is set. In Bash, use a presence-only check:
if [ -n "${ANTHROPIC_API_KEY:-}" ]; then
printf "API key is set\n"
else
printf "API key is not set\n"
fi
How billing changes when you use an API key
Imagine a developer who normally works under a Claude subscription opens a separate terminal for a company-owned API project. If the developer approves the company’s API key, the requests use its API billing arrangement rather than the subscription allowance. This is an illustrative scenario, not a measured usage result.
Review the organization’s usage dashboard, rate limits and spending controls before starting. Coding-agent tasks may involve several model requests, especially when examining files, generating changes and iterating after test failures. A task is not necessarily a single API request.
Start with a small read-only request if a live test is authorized, then check the corresponding usage records. For unattended workflows, explicitly configure the budget and credentials in the worker environment. The documented non-interactive mode uses the environment API key when present; do not expect the same approval prompt as an interactive session.
How to switch back to your Claude subscription
Exit the API-authenticated session first. Remove the temporary credential from the shell that launched Claude Code:
unset ANTHROPIC_API_KEY
In PowerShell, use:
Remove-Item Env:ANTHROPIC_API_KEY -ErrorAction SilentlyContinue
If a shell profile, secret-injection tool or project setting restores the variable, inspect the relevant configuration and remove only the unwanted source. Do not delete unrelated settings or credentials. Relaunch Claude Code, sign in to the intended account if necessary, and run /status again. Other providers or credentials can still affect authentication.
Our Claude Code settings guide explains the distinction between user, project and managed configuration.
Troubleshoot API key authentication
Claude Code still asks you to sign in: Verify that the key is set in the environment of the actual CLI process, not a different terminal or worker. Check the installed version and required organizational login method.
The key is present but rejected: Confirm its workspace, validity and permissions without exposing its value. A credential from a disabled organization may not work.
The request uses another account: Inspect /status and the documented credential precedence. A configured provider, gateway or authentication token may change selection. Ask your administrator before changing managed configuration.
Unexpected charges: Stop the relevant session, inspect the credential and usage records, then correct the environment. Rotate any exposed secret. A server error is not automatically an authentication failure.
For current token rates and worked cost examples, see our Claude API pricing guide.
Frequently asked questions
Can I use an API key without a Claude Pro subscription? Direct API authentication is separate from subscription access. You still need an authorized API account, suitable model access and applicable billing.
Does the API key override my subscription? In an ordinary terminal setup, an approved environment API key can take precedence over a subscription login. Verify the selected method.
Should I store my key in settings.json? Prefer an approved secret manager and temporary injection. Avoid committing or sharing literal secrets in configuration files.
Does this guide apply to cloud sessions? No. The shell examples target the terminal CLI. Anthropic documents different authentication behavior for cloud sessions and other interfaces.
Were live requests tested? No. This workflow was checked against Anthropic’s authentication documentation and Help Center on December 4, 2026. No API credential was created, exposed or used to generate billable output.

