API key
An API key is a long string of characters that identifies a program when it uses a service and determines who gets billed for the usage. For Claude Code, you create the key in the Claude Console and provide it through the ANTHROPIC_API_KEY environment variable. It's an alternative to a Claude subscription: you pay for the tokens you use. The key must be kept secret.
A long password that gives a program access to a service, for example to Claude. You pay for what you use. It’s kept secret, like a PIN.
How it shows up in Claude Code
The first time you start Claude Code, it asks you to sign in. The simplest route is your claude.ai subscription. But if you've set the ANTHROPIC_API_KEY variable, Claude Code asks you once whether to use it and remembers your answer. If you approve it, the key takes priority over your subscription.
The documentation points out a side effect: if you have a subscription but forgot the key is set on your system, Claude Code uses the key and you pay from your Console account. Running unset ANTHROPIC_API_KEY sends you back to the subscription, and /status shows which method is active. In -p mode, the key is always used when present.
A key is most useful for automation: scripts, CI and GitHub Actions, where you can't log in through a browser.
Example
On macOS or Linux, for the current terminal session:
export ANTHROPIC_API_KEY=your-key
claude -p "Summarize README.md"
In a GitHub repository, the key never goes into a file. You add it to the repository's secrets, where the workflow reads it.
Common mistakes
- Writing the key into a file that ends up in git. If that happens, delete the key in the Console and create a new one; deleting the file isn't enough, because the history keeps it.
- Pasting the key into your chat with Claude or into a screenshot.
- Having both a subscription and a key set, and not knowing which one you're paying from. Check with
/status.
Related terms and where you learn it
The key gives access to an API, is kept in an environment variable or a .env file, and counts as a secret. Usage is measured in tokens.
In the course: the lesson “How do you sign in to your account?” in module 00, Installation and first steps.
How it appears in the course
→ Where does the API key go?
For the curious · Module 03, Permissions: what Claude may do
Yes: Codex can also be used with an API key, an access code for developers with pay-as-you-go billing.
For the curious · Module 00, Welcome to Codex · free
Without a ChatGPT account, for example with an API key (an access code, explained in the lesson on passwords and keys), they can add to your usage.
What does Approve for me mode do? · Module 05, Permissions and safety
Learn Claude Code from zero, in plain English
12 modules with 1–2 minute lessons, which you can also do on your phone. The first two modules are free.