Claude Code API Key: Where to Get One and How to Connect It
Where the Claude Code API key is created, where to store it, how your own key differs from a subscription in cost and predictability, and what to do about a 401.
All articles in the guide Claude Code · 13
The API key is the second most common source of problems after access. The good news is that diagnosis is unambiguous: the fault is either in the key itself or in where the program reads it from.
Where the key is created
Keys are issued in the provider console, in the API keys section. Three things worth doing at creation time:
- Give the key a meaningful name. Six months later you will not remember which of four keys is used where, and you will need to revoke a specific one.
- Copy the value immediately. The full key is shown once; after that you only see its tail.
- Use a separate key per environment. Local work, CI and production scripts should each have their own. Then compromising one does not stop everything.
Where to put it
In order of preference:
- An environment variable - the default. The program reads the key from the environment and it never sits in the project.
- The system secret store, if the tool supports it. The key is not even visible in a variable listing.
- A config file outside the repository - fine if it lives in your home directory rather than in the project.
What not to do:
- Put the key in a file inside the project, even with a
.gitignoreentry. Sooner or later someone runsgit add -for copies the folder wholesale. - Pass the key as a command argument - it lands in shell history and in the process list.
- Share one key across a team. You will have to revoke it for everyone at once.
Your own key versus a subscription
This is a choice on two axes: cost and predictability.
A subscription is a fixed monthly amount with usage limits inside it. Spend does not depend on how heavy the week was. What you can hit is the limit, and then work stops until the window resets.
An API key bills actual token usage. You will not stall mid-task, but the invoice reflects how you worked. One bad day with an enormous context costs noticeably more than a careful week.
A practical heuristic: daily, steady use is calmer on a subscription. Bursty load, or invocation from scripts and CI where predictability matters more than the total, favours a key. How spend is formed in the first place has its own article: limits and saving context.
Debugging a 401
Check in order, most common first:
- Which key the program picked up. An environment variable usually overrides the config file, so you are editing the wrong source. This is cause number one.
- Is the key complete? Copying often drops the tail or attaches a space, and an invisible trailing newline breaks the authorisation header.
- Is the key live? Revoked and expired keys give the same error.
- Is it the right account? A key from another organisation will not work even if it is valid.
- Is there a balance? With some providers a zero balance returns 401 rather than 402.
If the key is correct and the request still fails, you are probably looking at the wrong error. Timeouts and dropped connections look different; the symptom table is in the access article.
Next
With a working key, continue to CLI commands and modes and picking a model for the task. The overview is in the Claude Code guide.
FAQ
Do I need an API key if I have a subscription?
No, they are two independent ways to pay. A subscription is a fixed amount with usage limits; a key bills the tokens you actually spend. You do not need both, and keeping a key around only makes sense as a fallback channel.
Where should the key live so it does not leak?
In an environment variable or the system secret store, not in a file inside the repository. The most common leak is a config file committed to a public repo. If a key does end up in git history, revoke it: deleting the file in a later commit fixes nothing.
What does a 401 mean?
The key was rejected: missing, wrong, revoked, or from a different account. It is an authorisation error, not a network or limit error. Start by checking which key the program actually picked up, since an environment variable usually overrides the config file.
- Claude Code: A Complete Practical GuideGuide
- Claude Code in Russia: Access, Payment and LimitsWhat actually fails when using Claude Code from Russia, which payment routes remain, how to tell an access problem from a configuration error, and what the service rules mean for your account.
- Claude Code CLI: Commands, Flags and ModesHow Claude Code starts, what the permission modes actually mean, which flags earn their keep in daily work, and what belongs in project configuration instead of being retyped.
- How to Install Claude Code on Windows, macOS and WSLSystem requirements, installing Claude Code on macOS, Linux, Windows and WSL, the first run and authentication, plus the install errors people actually hit and what they mean.
Done for you
Need the result, not the agent setup? I will build your MVP
I work in Claude Code every day and take products all the way to users.
from $1,500 · 1 to 2 weeks