Use Different API Key For Each Project
I am a consultant and I have a personal API Key that I want to use for all my personal stuff. It is possible I could have different api key for each client I work for. Is it possible to configure claude code to use a different api key per project? I have tried some things but nothing has worked for me.
@sid374 I take it this isn't possible right now. Is there a way to enter an existing API Key into my config file to use it? It isn't ideal but if I can at least swap out API Key's would be great.
@vetterj73 I see a primaryApiKey item in ~/.claude.json. Does swapping that work for you?
EDIT: that's linux-only it seems, not on Mac.
On darwin, it does security find-generic-password -a $USER -w -s "Claude Code".
So, you could first try to set the ANTHROPIC_API_KEY env var as suggested here: https://docs.anthropic.com/en/docs/agents-and-tools/claude-code/overview#automate-ci-and-infra-workflows
However, I'm not certain that works outside of headless flows. The worst case would be to update the password each run:
security add-generic-password -a $USER -U -s "Claude Code" -w "password"
Curious to know which works! I'm a bit too lazy to delete/recreate multiple keys in the console to thoroughly test this.
@sid374 I take it this isn't possible right now. Is there a way to enter an existing API Key into my config file to use it? It isn't ideal but if I can at least swap out API Key's would be great.
Yes we don't support this at the moment. The officially supported flow would be to /logout and /login with a different user in your respective directories/sessions
I tried changing the api key in the config file but I end up getting a rate limit error.
I know Claude Code create a special work space but we tried creating a new API Key in there and it isn't allowed.
This isn't stopping me from doing work but I feel it is something that should work. For now I am fine with substituting a key when I want to use a different organizations key if I could.
Is there any way from the admin console that I could create a key that will not get a rate limit?
@acrefoot I confirmed that the environment variable is only for non-interactive, headless mode for CICD.
I can't find a way to achieve what we're wanting here, so far it seems like an unsupported feature but one that is very important.
I come here with similar requirements. I have a personal and a company API key. I would like to switch between API keys when switching between personal and company projects.
+1 for this feature
Same use case as @zalomб would be nice to use something like direnv to swap API keys that Claude Code is using in interactive mode. And in CI mode just use CLAUDE_API_KEY or similar.
Similar requirements, +1 for this feature
Same use case as @zalomб would be nice to use something like
direnvto swap API keys that Claude Code is using in interactive mode. And in CI mode just useCLAUDE_API_KEYor similar.
Yup. I am also using direnv and looking for a solution to use something like OPENAI_API_KEY and/or GEMINI_API_KEY for different profiles.
Hey guys, since I posted in this feature request issue, I've found a way to set the key in env inside .claude/settings.json. ->
{
"model": "opus",
"env": {
"ANTHROPIC_API_KEY": "your_key_here"
}
}
I was also exporting the key in my .zshrc, and it was picked up.
There was the thing where I needed to log in anyway. But if you put the key in env, this is the welcome screen.
This does not suit everyone, especially if the requested feature is "profiles" for each key.
BUT, if you check the documentation about settings and check Project settings in particular, you can set up different keys for each project.
That should suffice!
This issue has been inactive for 30 days. If the issue is still occurring, please comment to let us know. Otherwise, this issue will be automatically closed in 30 days for housekeeping purposes.
The issue is still occurring and needs a better resolution.