claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

Use Different API Key For Each Project

Open vetterj73 opened this issue 10 months ago • 9 comments

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.

vetterj73 avatar Mar 31 '25 18:03 vetterj73

@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 avatar Apr 01 '25 16:04 vetterj73

@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.

acrefoot avatar Apr 01 '25 17:04 acrefoot

@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

sid374 avatar Apr 01 '25 23:04 sid374

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?

vetterj73 avatar Apr 04 '25 18:04 vetterj73

@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.

bluesherpa avatar Apr 05 '25 21:04 bluesherpa

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.

zalom avatar Apr 18 '25 15:04 zalom

+1 for this feature

luisdalmolin avatar May 13 '25 13:05 luisdalmolin

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.

uson1x avatar May 13 '25 14:05 uson1x

Similar requirements, +1 for this feature

isasmendiagus avatar May 23 '25 12:05 isasmendiagus

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.

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.

ruchernchong avatar Jul 06 '25 17:07 ruchernchong

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.

Image

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!

zalom avatar Jul 06 '25 17:07 zalom

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.

github-actions[bot] avatar Oct 10 '25 10:10 github-actions[bot]

The issue is still occurring and needs a better resolution.

bluesherpa avatar Nov 10 '25 23:11 bluesherpa