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

Employer provided me with an Anthropic API key to use Claude Code but there's no way to configure this key

Open edgariscoding opened this issue 7 months ago • 31 comments

Environment

  • Platform (select one):
    • [X] Anthropic API
    • [ ] AWS Bedrock
    • [ ] Google Vertex AI
    • [ ] Other:
  • Claude CLI version: 0.2.108
  • Operating System: macOS
  • Terminal: iTerm2

Bug Description

My employer created a number of Anthropic API keys for each developer in our team, they take care of billing. We want to use Claude Code but there doesnt seem to be a way to configure the API key when calling claude /login.

edgariscoding avatar May 13 '25 16:05 edgariscoding

Does it work if you do ANTHROPIC_API_KEY=<key> claude

wolffiex avatar May 13 '25 18:05 wolffiex

@wolffiex, no that did not work.

I logged out from my personal account by using /logout

Then I used ANTHROPIC_API_KEY=<my-work-key> claude

At this point I was asked to choose a dark/light mode for Claude Code and then I was shown the "Select login method" screen with an option for Anthropic Console and Claude app methods. Neither option worked for me. They both tried to get me to login on the website. I don't have a login for my work API key.

edgariscoding avatar May 13 '25 20:05 edgariscoding

@bcherny, I noticed you thumbs upped @wolffiex's reply and closed this issue as completed. Is @wolffiex's suggestion supposed to work? Is it documented somewhere? I could not find it in on the official docs:

https://docs.anthropic.com/en/docs/claude-code/overview

edgariscoding avatar May 13 '25 20:05 edgariscoding

Why was this closed as completed? It doesn't work. When using ANTHROPIC_API_KEY="..." claude you are still greeted with this screen

Image

bendavis78 avatar May 15 '25 17:05 bendavis78

The next release should remove the login screen if a key or apiKeyHelper is provided externally.

ant-kurt avatar May 22 '25 19:05 ant-kurt

and when is next release?

fishuke avatar May 27 '25 12:05 fishuke

@fishuke release v1.0.4 just went out, can you give that a try?

ant-kurt avatar May 28 '25 19:05 ant-kurt

@fishuke release v1.0.4 just went out, can you give that a try?

I wasn't able to get ANTHROPIC_API_KEY working, but I figured I'd try out the documented variable ANTHROPIC_AUTH_TOKEN and it worked! 🎉

export ANTHROPIC_AUTH_TOKEN='xxxxxxxxxxxxx'
claude
[...]

Cbeck527 avatar May 28 '25 23:05 Cbeck527

Proper fix is landing in 1.0.6

bcherny avatar May 29 '25 00:05 bcherny

I am getting an API error even thought it is a valid key

nandatheguntupalli avatar May 29 '25 01:05 nandatheguntupalli

@nandatheguntupalli I am getting an API error even though it is a valid key

Same here. FWIW, it worked yesterday evening New York time (I generated a CLAUDE.md for my app) but now I'm seeing a 401 as well.

API Error: 401 {"type":"error","error":{"type":"authentication_error","message":"Invalid bearer token"}}

ANTHROPIC_AUTH_TOKEN and ANTHROPIC_API_TOKEN both return the 401 😭

Cbeck527 avatar May 29 '25 15:05 Cbeck527

Hi folks, can you give this a try once the upcoming release v1.0.11 goes out? This cleans up the behavior around ANTHROPIC_API_KEY (for Console keys) and ANTHROPIC_AUTH_TOKEN (for Authorization: Bearer tokens), as well as adds some additional UI info - such as /status command showing which auth method is being used.

ant-kurt avatar Jun 04 '25 16:06 ant-kurt

For anyone keeping an eye on this issue, I was able to get this to work on v1.0.10 by adding an environment config to ~/.claude/settings.json

$ cat ~/.claude/settings.json
{
  "env": {
    "ANTHROPIC_API_KEY": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  }
}

$ claude
╭────────────────────────────────────────────────────────────╮
│ ✻ Welcome to Claude Code!                                  │
│                                                            │
│   /help for help, /status for your current setup           │
│   ──────────────────────────────────────────────────────── │
│   Overrides (via env):                                     │
│                                                            │
│   • API Key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX │
╰────────────────────────────────────────────────────────────╯
...
...

Will try the more standard approach of exporting this as an env var in my shell once the next release is out!

Cbeck527 avatar Jun 04 '25 16:06 Cbeck527

@ant-kurt @bcherny Any update on this?

Tried setting ANTHROPIC_API_KEY as an env variable, using it as a prefix, via the settings.json file etc. However, still redirected to the login page. On the version 1.0.18

RohitPShetty avatar Jun 11 '25 13:06 RohitPShetty

@RohitPShetty can you run claude /status to see if the key is picked up? Also please check your ~/.claude.json to see if a key entry was approved or rejected. If rejected, you can manually remove that line or run claude /logout to force erasure of all the API keys from this file. Then on next launch, the "Do you want to use this API key?" prompt should appear again if the env var is picked up correctly.

ant-kurt avatar Jun 20 '25 21:06 ant-kurt

`(base) user@MonkeyPro test-claude-cli % claude /status ╭───────────────────────────────────────────────────╮ │ ✻ Welcome to Claude Code! │ │ │ │ /help for help, /status for your current setup │ │ │ │ cwd: /Users/user/test-claude-cli │ ╰───────────────────────────────────────────────────╯

Tips for getting started:

  1. Ask Claude to create a new app or clone a repository
  2. Use Claude to help with file analysis, editing, bash commands and git
  3. Be as specific as you would with another engineer for the best results
  4. ✔ Run /terminal-setup to set up terminal integration

※ Tip: Ask Claude to create a todo list when working on complex tasks to track progress and remain on track

Claude Code Status v1.0.31

L Session ID: dce1xxxx-xf3x-xxxx-xxxx-9ff2exxx8437

Working Directory L /Users/user/test-claude-cli

Account • /login L Login Method: Claude Pro Account L Organization: [email protected]'s Organization L Email: [email protected]

Model • /model L Sonnet Sonnet 4 for daily use`

I hit enter and then i try to use claude

`(base) user@MonkeyPro test-claude-cli % claude /status ╭───────────────────────────────────────────────────╮ │ ✻ Welcome to Claude Code! │ │ │ │ /help for help, /status for your current setup │ │ │ │ cwd: /Users/user/test-claude-cli │ ╰───────────────────────────────────────────────────╯

Tips for getting started:

  1. Ask Claude to create a new app or clone a repository
  2. Use Claude to help with file analysis, editing, bash commands and git
  3. Be as specific as you would with another engineer for the best results
  4. ✔ Run /terminal-setup to set up terminal integration

※ Tip: Ask Claude to create a todo list when working on complex tasks to track progress and remain on track

/status ⎿ (no content)

create a new app ⎿ API Error: 401 {"type":"error","error":{"type":"authentication_error","message":"OAuth token has expired. Please obtain a new token or refresh your existing token."}}

╭──────────────────────────── ` Is it not possible to authenticate using Claude Pro Account??!!

sameehj avatar Jun 23 '25 14:06 sameehj

@sameehj try /logout and then /login you'll get the option to use your subscription

wolffiex avatar Jun 23 '25 16:06 wolffiex

@sameehj try /logout and then /login you'll get the option to use your subscription

Works like a charm, thank you 👍

sameehj avatar Jun 25 '25 08:06 sameehj

Version 1.0.35 This sill appears to be a problem that It cannot accept ANTHROPIC_API_KEY as env variable, or via the settings.json . Any workaround?

BigAl avatar Jun 26 '25 06:06 BigAl

@BigAl @bcherny @ant-kurt

Still the same problem in Version 1.0.44. Setting ANTHROPIC_API_KEY as env variable in ~/.claude/settings.json does not work (neither export ANTHROPIC_API_KEY='..' ) and the intro keeps asking to choose one of the 2 login methods.

The ony workround I found is:

~/.claude/settings.json:

{
  "apiKeyHelper": "/home/user/.claude/get-api-key.sh"
}

And ~/.claude/get-api-key.sh:

#!/bin/bash
echo "sk-your-anthropic-api-key"

Now it starts without asking for a login method.

PS: remember $ chmod ug+x get-api-key.sh

slovanos avatar Jul 10 '25 21:07 slovanos

@ant-kurt None of this works (refer: https://github.com/anthropics/claude-code/issues/1084#issuecomment-2992990877)

The only thing that worked was the method listed by @slovanos above.

RohitPShetty avatar Jul 11 '25 13:07 RohitPShetty

I'm experiencing the same behavior to @slovanos, also on v1.0.44.

Another way I found to work around this is to set both ANTHROPIC_API_KEY and ANTHROPIC_AUTH_TOKEN (the last one can be any dummy value, but must be non-empty).

Then, when you start Claude Code for the first time, accept the (non-recommended option) to use the ANTHROPIC_API_KEY env var for login.

Image

Claude Code then works using the ANTHROPIC_API_KEY auth, even though you get this error message of potential "unexpected behavior" (which tbh may be the case as I haven't extensively tested):

Image

nmartorell avatar Jul 11 '25 20:07 nmartorell

Same, confirming this bug exists for trying to use API Key in environment - even when loaded, it prompts for console login. Using the workaround sh file to point to the key is the only way to bypass that prompt and have it use my key.

MacOs - 1.0.64 (Claude Code)

EDIT - no, it only worked once. The second time, it now prompts me with:


 ⚠ Auth conflict: Both a token (apiKeyHelper) and an API key (ANTHROPIC_API_KEY) are set. This may lead to unexpected 
   behavior.
    • Trying to use apiKeyHelper? Unset the ANTHROPIC_API_KEY environment variable, or claude /logout then say "No" to
     the API key approval before login.
    • Trying to use ANTHROPIC_API_KEY? Unset the apiKeyHelper environment variable.

Removed the env version - now it works with the helper file only.

EarthmanWeb avatar Jul 31 '25 23:07 EarthmanWeb

I created a dummy account, and once you get past the login, you’ll see that it overrides with your ANTHROPIC_API_KEY.

Image

aaronmedina-dev avatar Aug 12 '25 23:08 aaronmedina-dev

My workaround was to set ANTHROPIC_API_KEY to your api key and ANTHROPIC_AUTH_TOKEN to dummy. Then start claude and select Yes to using the api key. You can then remove the ANTHROPIC_AUTH_TOKEN dummy env var to get rid of the "Auth conflict" warning.

thejspr avatar Aug 26 '25 08:08 thejspr

I've run into this since yesterday, trying to get Claude Code to work from native (non WSL) powershell and/or (Git) bash shell, when using a company provided API key.

Note that the "normal" /login flow with option 2 (API usage) works fine from powershell. (Bought credit between the "credit balance too low" and the following "/status" call:) Image

What I've tried:

  • Powershell, setting ANTHROPIC_API_KEY before launching:

    • API key is not recognized, remains using the /login key. (I seem to remember this used to work and be recognized but no longer apparently)
  • Setting ANTHROPIC_API_KEY env var in the .claude/settings.json:

    • API key is not recognized, remains using the /login key.
    • apiKeyHelper method, I get the error Error getting API key from apiKeyHelper (in settings or ~/.claude.json): apiKeyHelper did not return a valid value.
      • Cannot debug what the problem but tried many permutations of paths and slashes to no avail (here I'd not yet logged out again): Image
  • Git bash then setting ANTHROPIC_API_KEY (note this git bash was started from within Powershell -- if you start it from the start menu with a full env then for some reason it just hangs):

    • Same story, env var is ignored: Image

What does work:

  • Windows WSL/bash

However I would really like to use a native shell/not WSL bash ideally.

  • Why does /login not present an option 3.) Enter API key?

(By the by if Claude can be made to directly/natively interact with the OS using Powershell, I suspect this would be extremely valuable because all Powershell outputs are objects, and the commandlet ecosystem is quite rich, with help generally always including for example --examples as an option, all of which would give Claude a major leg up in terms of interacting with command results and so on...)

ByteJuggler avatar Sep 05 '25 13:09 ByteJuggler

Can confirm that the workaround of setting both environment variables (as mentioned in https://github.com/anthropics/claude-code/issues/1084#issuecomment-3223079288) works for me as well, nothing else did.

kiview avatar Sep 12 '25 06:09 kiview

Comment https://github.com/anthropics/claude-code/issues/1084#issuecomment-3223079288

My workaround was to set ANTHROPIC_API_KEY to your api key and ANTHROPIC_AUTH_TOKEN to dummy. Then start claude and select Yes to using the api key. You can then remove the ANTHROPIC_AUTH_TOKEN dummy env var to get rid of the "Auth conflict" warning.

This was the only workaround that worked for me.

santhoshsram avatar Oct 06 '25 07:10 santhoshsram

@slovanos work-around using the api key helper was the answer.

For anyone that can't get it working, chmod ug+x ~/get-api-key.sh was the kicker.

Setting both ANTHROPIC_API_KEY and ANTHROPIC_AUTH_TOKEN also works for me, but as mentioned, claude complains about dueling environment variables.

themartorana avatar Oct 10 '25 17:10 themartorana