Login with API token on server to execute tasks

Status Fixed / completed
Maintainer reply ✓ Yes — bcherny
Activity 11 comments · opened Mar 11, 2025 · closed Mar 15, 2025
💡 Likely answer: A maintainer (bcherny, collaborator) responded on this thread — see the highlighted reply below.

No description provided.

View original on GitHub ↗

11 Comments

ghost · 1 year ago

Hi! Could you share a bit more detail on what you would like to do?

r03t · 1 year ago

@levpopov I am trying to create a GitHub bot with Claude Code. When I create a PR, I want to run it and perform some contextual tasks. The CLI tool doesn't seem designed for automation as it requires many approvals and a manual login process.
Can you enable the tool for advanced mode and simple API_KEY authentication?

theodormarcu · 1 year ago

+1 Think this would be an interesting use case for CI

r03t · 1 year ago

Yes there are ways to implement but not easy and straightforward. Also allow root to run the tool. @levpopov

bcherny collaborator · 1 year ago

This is shipped in 0.2.44! You can now set the ANTHROPIC_API_KEY env var in -p mode. Docs coming in hot.

r03t · 1 year ago

No chance to run this CLI tool in ubuntu. Impossible in Alpine @bcherny @levpopov Can you help !?

<img width="691" alt="Image" src="https://github.com/user-attachments/assets/ee328ec2-5c58-4b1d-aee0-a088fa22dfa5" />

FROM ubuntu:22.04

ARG TZ
ENV TZ="$TZ"
ENV DEBIAN_FRONTEND=noninteractive

# Install Node.js and basic development tools
RUN apt update && apt install -y \
  curl \
  wget \
  less \
  git \
  procps \
  sudo \
  fzf \
  zsh \
  man-db \
  unzip \
  gnupg2 \
  iptables \
  ipset \
  iproute2 \
  dnsutils \
  aggregate \
  jq \
  ca-certificates

# Install Node.js 20.x
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
  apt install -y nodejs

# Install GitHub CLI
RUN curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg && \
  chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg && \
  echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt/sources.list.d/github-cli.list > /dev/null && \
  apt update && \
  apt install -y gh

# Create a non-root user
RUN useradd -m -s /bin/bash -G sudo node && \
  echo "node ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/node

# Ensure node user has access to /usr/local/share
RUN mkdir -p /usr/local/share/npm-global && \
  chown -R node:node /usr/local/share

# Persist bash history
RUN mkdir /commandhistory && \
  touch /commandhistory/.bash_history && \
  chown -R node:node /commandhistory

# Set `DEVCONTAINER` environment variable
ENV DEVCONTAINER=true

# Create workspace and config directories
RUN mkdir -p /workspace /home/node/.claude && \
  chown -R node:node /workspace /home/node/.claude

WORKDIR /workspace

# Install git-delta
RUN ARCH=$(dpkg --print-architecture) && \
  wget "https://github.com/dandavison/delta/releases/download/0.18.2/git-delta_0.18.2_${ARCH}.deb" && \
  dpkg -i "git-delta_0.18.2_${ARCH}.deb" && \
  rm "git-delta_0.18.2_${ARCH}.deb"


    # Set zsh as the default shell
ENV SHELL /bin/zsh

# Install powerline10k theme for zsh
RUN sh -c "$(wget -O- https://github.com/deluan/zsh-in-docker/releases/download/v1.2.0/zsh-in-docker.sh)" -- \
  -p git \
  -p fzf \
  -a "source /usr/share/doc/fzf/examples/key-bindings.zsh" \
  -a "source /usr/share/doc/fzf/examples/completion.zsh" \
  -a "export PROMPT_COMMAND='history -a' && export HISTFILE=/commandhistory/.bash_history" \
  -x

# Install Claude CLI
RUN npm install -g @anthropic-ai/claude-code

# Add bash and zsh configuration for command history
RUN echo 'export PROMPT_COMMAND="history -a"' >> ~/.bashrc && \
    echo 'export HISTFILE=/commandhistory/.bash_history' >> ~/.bashrc && \
    echo 'export PROMPT_COMMAND="history -a"' >> ~/.zshrc && \
    echo 'export HISTFILE=/commandhistory/.bash_history' >> ~/.zshrc
pezhman32 · 1 year ago

@bcherny Using the latest version (0.2.45) I still cannot make it work with ANTHROPIC_API_KEY:

> docker run -it --entrypoint /bin/bash node:22.14.0
root@813578d88c59:/# npm install -g @anthropic-ai/claude-code 
root@813578d88c59:/# export ANTHROPIC_API_KEY=sk-ant-api03-OjB2....
root@813578d88c59:/# claude -p "testing, please reply that you got this message."
Invalid API key · Please run /login
root@813578d88c59:/# claude --version
0.2.45 (Claude Code)
root@813578d88c59:/# ANTHROPIC_API_KEY=sk-ant-api03-OjB2....  claude -p "testing, please reply that you got this message."
Invalid API key · Please run /login
gireeshm-vayavya · 1 year ago

even on 0.2.48 i am unable to make ANTHROPIC_API_KEY to work

n11west11 · 1 year ago

I was having some issues with this as well.

This claude-code-action led me to believe that you need a config.json setup to not have to manually login.

      - name: Setup Claude Config
        env:
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
        run: |
          # Get home directory path
          HOME_DIR="$HOME"
          CLAUDE_CONFIG_PATH="$HOME_DIR/.claude"
          
          # Create config directory
          mkdir -p "$CLAUDE_CONFIG_PATH"
          
          # Create config.json from template
          cat "$GITHUB_WORKSPACE/main-repo/config_template.json" | sed "s/CLAUDE_API_KEY/$ANTHROPIC_API_KEY/" > "$CLAUDE_CONFIG_PATH/config.json"
          chmod 600 "$CLAUDE_CONFIG_PATH/config.json"
          
          # Verify config was created correctly
          ls -la "$CLAUDE_CONFIG_PATH"
          
          # Add config dir to environment
          echo "CLAUDE_CONFIG_DIR=$CLAUDE_CONFIG_PATH" >> $GITHUB_ENV
          echo "ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY" >> $GITHUB_ENV

config_template

{
  "primaryApiKey": "CLAUDE_API_KEY",
  "hasCompletedOnboarding": true,
  "lastOnboardingVersion": "0.2.30",
  "projects": {
    "/workspace": {
      "allowedTools": [
      ],
      "history": [
      ],
      "dontCrawlDirectory": false,
      "enableArchitectTool": false,
      "mcpContextUris": [],
      "mcpServers": {},
      "approvedMcprcServers": [],
      "rejectedMcprcServers": [],
      "ignorePatterns": [],
      "exampleFiles": [],
      "exampleFilesGeneratedAt": 1741180925950,
      "hasCompletedProjectOnboarding": true
    }
  },
  "doctorShownAtSession": 6,
  "lastReleaseNotesSeen": "0.2.32",
  "bypassPermissionsModeAccepted": true,
  "verbose": true
}

Then when you finally run claude, make sure CLAUDE_CONFIG_DIR points to your json file

- name: Run Claude with prompt
        env:
          CLAUDE_CONFIG_DIR: ${{ env.CLAUDE_CONFIG_DIR }}
          ANTHROPIC_API_KEY: ${{ env.ANTHROPIC_API_KEY }}
joshystuart · 1 year ago

I was trying to run claude code in a docker container and came across this issue, but it wasn't super clear how to enable this.

This is how I was able to run claude in docker:

  1. Use claude setup-token to generate a long lived token (pro and max only)
  2. That will allow you to set the env var CLAUDE_CODE_OAUTH_TOKEN=sk-***
  3. Then create a .claude.json file with the following
{
  "hasCompletedOnboarding": true,
  "lastOnboardingVersion": "1.0.51",
  "projects": {
    "/workspace": {
      "allowedTools": [],
      "history": [],
      "dontCrawlDirectory": false,
      "enableArchitectTool": false,
      "mcpContextUris": [],
      "mcpServers": {},
      "approvedMcprcServers": [],
      "rejectedMcprcServers": [],
      "exampleFiles": [],
      "exampleFilesGeneratedAt": 1741180925950,
      "hasCompletedProjectOnboarding": true,
      "hasTrustDialogAccepted": true
    }
  },
  "lastReleaseNotesSeen": "1.0.65",
  "bypassPermissionsModeAccepted": true,
  "verbose": true
}

My full Dockerfile is

FROM node:20

ARG CLAUDE_CODE_VERSION=latest

# Install basic development tools and iptables/ipset
RUN apt-get update && apt-get install -y --no-install-recommends \
  less \
  git \
  procps \
  sudo \
  fzf \
  man-db \
  unzip \
  gnupg2 \
  gh \
  iptables \
  ipset \
  iproute2 \
  dnsutils \
  aggregate \
  jq \
  && apt-get clean && rm -rf /var/lib/apt/lists/*

# Ensure default node user has access to /usr/local/share
RUN mkdir -p /usr/local/share/npm-global && \
  chown -R node:node /usr/local/share

ARG USERNAME=node

# Create workspace and config directories and set permissions
RUN mkdir -p /workspace /home/node/.claude
COPY .claude.json /home/node/.claude.json
RUN chown -R node:node /workspace /home/node/.claude.json /home/node/.claude

WORKDIR /workspace

# Set up non-root user
USER node

# Install global packages
ENV NPM_CONFIG_PREFIX=/usr/local/share/npm-global
ENV PATH=$PATH:/usr/local/share/npm-global/bin

# Install Claude
RUN npm install -g @anthropic-ai/claude-code@${CLAUDE_CODE_VERSION}

NOTE .claude.json should be in the same directory as the Dockerfile

Then build it and run remembering to pass in the token eg.

docker build -t cc .
docker run -e CLAUDE_CODE_OAUTH_TOKEN=sk-*** cc
github-actions[bot] · 1 year ago

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.