Non-Interactive Mode Fails to Authenticate Using Provided API Key
Environment
- Platform (select one):
- [X] Anthropic API
- [ ] AWS Bedrock
- [ ] Google Vertex AI
- [ ] Other: <!-- specify -->
- Claude CLI version: 0.2.48
- Operating System: Ubuntu (via Docker)
- Terminal: Docker container
Bug Description
In a non-interactive environment, the CLI still requires executing /login even though an API key is provided. The documentation states that CI automation should work, but in my setup, running claude non-interactively results in the error "Invalid API key · Please run /login." While interactive sessions work fine after manually logging in with /login, this behavior prevents the tool from being used in automated CI workflows (such as GitHub Actions).
Steps to Reproduce
- Build a Docker image with the following Dockerfile:
``dockerfile``
FROM ubuntu:latest
USER root
WORKDIR /home/app
RUN apt-get update
RUN apt-get -y install curl git
RUN curl -sL https://deb.nodesource.com/setup_20.x | bash -
RUN apt-get -y install nodejs
RUN npm install -g @anthropic-ai/claude-code
# Set environment variables for authentication (replace sk-xxx with your API key)
ENV ANTHROPIC_API_KEY=sk-xxx
ENTRYPOINT ["sh", "-c", "claude"]
- Run the container in non-interactive mode with the command:
``sh``
docker run -it <image_id> -p "hello"
- Observe the error message:
````
Invalid API key · Please run /login
- Confirm that the
ANTHROPIC_API_KEYis set by running:
``sh`
docker run -it --entrypoint sh <image_id> -c 'echo $ANTHROPIC_API_KEY'
`
which outputs:
``
sk-xxx
Expected Behavior
Is there a known workaround or a fix planned to support non-interactive authentication?
16 Comments
I am having the same issue. Running the latest version of Claude Code 0.2.49
Also on 0.2.50, seemingly works on Mac but not on Ubuntu @bcherny @levpopov
I am on Mac, running Claude Code in a Docker container, env var ANTHROPIC_API_KEY is set, and still getting the message that it's an invalid API key and I need to run /login. Updated to 0.2.50
Same with Claude Code 0.2.53 (Ubuntu 22.04.5 LTS docker container on MacOS)
Looking into this. It seems like a potential workaround is to use
ENTRYPOINT ["claude"]instead. Please let me know if this works for you.same problem here, using my own keys or a special key generated by Anthropic support (I just pointed them to this thread too)
I asked claude to read its own code and find the issue (_"read the code from /usr/bin/claude and find why using ANTHROPIC_API_KEY doesn't work when run in headless mode using -p"_)
I don't have the time right now to check this - just sharing in case someone else is looking at it!
I confirm that
claudecould fix itself (using v0.2.54)The function
Sw(I)is responsible for API key validation and selection.Here's the buggy snippet:
Change the function to this:
...and voila:
I think it's my first contrib to a non-open-source software, and also my first using a software to fix itself!
I built a Docker container based on .devcontainer/Dockerfile and confirmed that it runs without any issues—the "Invalid API key · Please run /login" error did not occur.
Since this code is not open source, it's quite difficult to pinpoint the exact cause. However, it's likely that the issue with failing to retrieve the key properly lies in the internal shell version or one of the dependent libraries.
Fix landing in the next version. Thanks for the reports!
Fixed in 0.2.59. Restart to update.
I think this issue has resurfaced:
But when I start a session by calling
claudeit just picks up on my API key.I'm getting this issue too with the 0.2.90 version!
I am getting the same on v1.0.11
Same on 1.0.43. APi key is certainly valid
This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.