# A premium developer's guide to running Claude Code CLI for free using AgentRouter and HCNSEC (IAMHC) API proxies.

> Source: <https://gist.github.com/kyiov/7fa4ade364f520609d7ef65c46b69388>
> Published: 2026-07-15 10:37:08+00:00

A comprehensive, step-by-step developer's guide to installing, configuring, and running Anthropic's **Claude Code** CLI client on PC, Linux, macOS, and Termux using alternative compatible API endpoints.

[Overview & Comparison](https://gist.github.com/starred.atom#-overview--comparison)[Step 1: Get Your API Credentials](https://gist.github.com/starred.atom#-step-1-get-your-api-credentials)[Step 2: Install Claude Code CLI](https://gist.github.com/starred.atom#-step-2-install-claude-code-cli)[Step 3: Configuration Templates](https://gist.github.com/starred.atom#-step-3-configuration-templates)[Quick Switcher Script](https://gist.github.com/starred.atom#-quick-switcher-script-optional)[Step 4: Running Claude Code](https://gist.github.com/starred.atom#-running-claude-code)[Troubleshooting & Common Pitfalls](https://gist.github.com/starred.atom#%EF%B8%8F-troubleshooting--common-pitfalls)

Claude Code normally requires an active Claude Pro subscription or official Anthropic API keys. By routing requests through compatible third-party API proxy services, you can run Claude Code for free or at a significantly lower cost using alternative routing endpoints.

| Feature | Provider A: AgentRouter | Provider B: HCNSEC (formerly IAMHC) |
|---|---|---|
Referral Link |
Yes (
|

[HCNSEC Invite Link](https://api.hcnsec.cn/register?aff=1vZv))**Featured Models**`claude-opus-4-8`

, `glm-5.2`

`DeepSeek-V4-Pro`

, `DeepSeek-V4-Flash`

, `glm-5.2`

**Trial Credit****Base URL**`https://agentrouter.org`

`https://api.hcnsec.cn`

- Sign up on your chosen platform:

**AgentRouter:**[https://bit.ly/3Tf0TJj](https://bit.ly/3Tf0TJj)** HCNSEC (with referral - Get bonus):**[https://api.hcnsec.cn/register?aff=1vZv](https://api.hcnsec.cn/register?aff=1vZv)

- Navigate to the
**API Keys / Tokens (令牌)** tab. - Generate a new API key. It should start with
`sk-`

.

Important

**API Key vs. Redemption Code:**
If you have a token ending in `==`

(e.g. `AXfq6y...==`

), this is a **Redemption Code (兑换码)**, not an API Key. You must redeem it in the portal (Top Up -> Redeem Code) to add credits to your account first, then create a standard `sk-`

key.

Install Node.js on your system first. Then run the installation command:

If you are running in a Termux environment, it is highly recommended to pin the version to `2.1.112`

for the best compatibility and stability:

```
npm install -g @anthropic-ai/claude-code@2.1.112
```

If you encounter shebang interpretation errors, run:

```
termux-fix-shebang $(which claude)
```

Install the latest stable version:

```
npm install -g @anthropic-ai/claude-code
```

Open the Claude Code settings file using a terminal text editor:

```
nano ~/.claude/settings.json
```

Replace the file content with one of the following JSON templates. Make sure to paste your own API Key in `ANTHROPIC_AUTH_TOKEN`

.

```
{
 "env": {
  "ANTHROPIC_AUTH_TOKEN": "YOUR_AGENTROUTER_API_KEY",
  "ANTHROPIC_BASE_URL": "https://agentrouter.org",
  "ANTHROPIC_MODEL": "claude-opus-4-8",
  "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1"
 },
 "permissions": {
  "allow": [],
  "deny": []
 },
 "skipWorkflowUsageWarning": true
}
{
 "env": {
  "ANTHROPIC_AUTH_TOKEN": "YOUR_HCNSEC_API_KEY",
  "ANTHROPIC_BASE_URL": "https://api.hcnsec.cn",
  "ANTHROPIC_MODEL": "DeepSeek-V4-Flash",
  "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1"
 },
 "permissions": {
  "allow": [],
  "deny": []
 },
 "skipWorkflowUsageWarning": true
}
```

Warning

**Base URL Structure:**
Do **NOT** add `/v1`

at the end of the `ANTHROPIC_BASE_URL`

(e.g. use `https://api.hcnsec.cn`

instead of `https://api.hcnsec.cn/v1`

).
The Claude Code client automatically appends `/v1/messages`

internally. If `/v1`

is present in your config, the client will try to call `/v1/v1/messages`

which results in a **404 Not Found** error.

You can substitute the `ANTHROPIC_MODEL`

in your settings file with any of these exact model IDs:

**For AgentRouter:**`claude-opus-4-8`

`claude-opus-4-7`

`claude-opus-4-6`

`glm-5.2`

**For HCNSEC:**`DeepSeek-V4-Flash`

*(Highly Recommended for speed)*`DeepSeek-V4-Pro`

`glm-5.2`

`glm-5.1`

`glm-4.7`

`Kimi-K2.6`

`MiniMax-M3`

If you use both providers and want to switch between them instantly without editing files manually, you can set up this helper CLI switcher script.

- Create the script file:

```
nano ~/.claude/switch-claude.sh
```

- Paste the following script code:

``` bash
 #!/data/data/com.termux/files/usr/bin/bash

 # Color definitions
 RED='\e[1;31m'
 GREEN='\e[1;32m'
 YELLOW='\e[1;33m'
 BLUE='\e[1;34m'
 CYAN='\e[1;36m'
 BOLD='\e[1m'
 RESET='\e[0m'

 # Configuration paths
 CONFIG_DIR="$HOME/.claude"
 SETTINGS_FILE="$CONFIG_DIR/settings.json"
 KEYS_FILE="$CONFIG_DIR/keys.conf"

 # Ensure config directory exists
 mkdir -p "$CONFIG_DIR"

 # Defaults
 DEFAULT_AGENTROUTER_MODEL="claude-opus-4-8"
 DEFAULT_HCNSEC_MODEL="glm-5.2"

 # Load saved keys if they exist
 if [ -f "$KEYS_FILE" ]; then
     source "$KEYS_FILE"
 fi

 # Set models to defaults if not present
 if [ -z "$AGENTROUTER_MODEL" ]; then
     AGENTROUTER_MODEL=$DEFAULT_AGENTROUTER_MODEL
 fi
 if [ -z "$HCNSEC_MODEL" ]; then
     HCNSEC_MODEL=$DEFAULT_HCNSEC_MODEL
 fi

 # Function to save keys and models
 save_config() {
     cat << EOF > "$KEYS_FILE"
 AGENTROUTER_KEY="$AGENTROUTER_KEY"
 HCNSEC_KEY="$HCNSEC_KEY"
 AGENTROUTER_MODEL="$AGENTROUTER_MODEL"
 HCNSEC_MODEL="$HCNSEC_MODEL"
 EOF
 }

 # Prompt for key if not saved
 get_key() {
     local provider_name=$1
     local current_key=$2
     if [ -z "$current_key" ]; then
         echo -e "${YELLOW}Kunci API untuk $provider_name belum disimpan.${RESET}"
         read -p "Masukkan API Key: " new_key
         echo "$new_key"
     else
         echo "$current_key"
     fi
 }

 show_usage() {
     echo -e "${CYAN}┌────────────────────────────────────────────────────────┐${RESET}"
     echo -e "${CYAN}│${RESET}         ${BOLD}CLAUDE CODE PROVIDER SWITCHER CLI${RESET}            ${CYAN}│${RESET}"
     echo -e "${CYAN}└────────────────────────────────────────────────────────┘${RESET}"
     echo -e "${BOLD}Penggunaan:${RESET}"
     echo -e "  ${GREEN}switch-claude 1${RESET} (atau ${GREEN}agentrouter${RESET})  : Beralih ke AgentRouter"
     echo -e "  ${GREEN}switch-claude 2${RESET} (atau ${GREEN}hcnsec${RESET})       : Beralih ke HCNSEC/IAMHC"
     echo -e "  ${GREEN}switch-claude --model 1 <nama_model>${RESET} : Ubah model AgentRouter"
     echo -e "  ${GREEN}switch-claude --model 2 <nama_model>${RESET} : Ubah model HCNSEC"
     echo -e "  ${GREEN}switch-claude --reset${RESET}            : Hapus kunci API yang disimpan"
     echo ""
     exit 1
 }

 # Handle reset flag
 if [ "$1" == "--reset" ]; then
     rm -f "$KEYS_FILE"
     echo -e "${RED}Konfigurasi yang disimpan telah dihapus!${RESET}"
     echo -e "${YELLOW}Silakan jalankan script kembali untuk memulai ulang.${RESET}"
     exit 0
 fi

 # Handle model change flag
 if [ "$1" == "--model" ]; then
     if [ "$2" == "1" ] || [ "$2" == "agentrouter" ]; then
         if [ -n "$3" ]; then
             AGENTROUTER_MODEL="$3"
             save_config
             echo -e "${GREEN}Model AgentRouter berhasil diubah menjadi: ${YELLOW}$3${RESET}"
             exit 0
         else
             echo -e "${RED}Harap sebutkan nama modelnya. Contoh: switch-claude --model 1 claude-opus-4-7${RESET}"
             exit 1
         fi
     elif [ "$2" == "2" ] || [ "$2" == "hcnsec" ]; then
         if [ -n "$3" ]; then
             HCNSEC_MODEL="$3"
             save_config
             echo -e "${GREEN}Model HCNSEC berhasil diubah menjadi: ${YELLOW}$3${RESET}"
             exit 0
         else
             echo -e "${RED}Harap sebutkan nama modelnya. Contoh: switch-claude --model 2 DeepSeek-V4-Flash${RESET}"
             exit 1
         fi
     else
         show_usage
     fi
 fi

 # Parse argument
 PROVIDER=""
 if [ "$1" == "1" ] || [ "$1" == "agentrouter" ]; then
     PROVIDER="agentrouter"
 elif [ "$1" == "2" ] || [ "$1" == "hcnsec" ]; then
     PROVIDER="hcnsec"
 elif [ -n "$1" ]; then
     show_usage
 else
     # Interactive menu if no argument
     echo -e "${CYAN}┌────────────────────────────────────────────────────────┐${RESET}"
     echo -e "${CYAN}│${RESET}         ${BOLD}CLAUDE CODE PROVIDER SWITCHER CLI${RESET}            ${CYAN}│${RESET}"
     echo -e "${CYAN}└────────────────────────────────────────────────────────┘${RESET}"
     echo -e "${BOLD}Pilih Provider:${RESET}"
     echo -e "  ${CYAN}[1]${RESET} AgentRouter (${BLUE}https://agentrouter.org${RESET})"
     echo -e "  ${CYAN}[2]${RESET} HCNSEC/IAMHC (${BLUE}https://api.hcnsec.cn${RESET})"
     echo ""
     read -p "Pilihan Anda (1/2): " pilihan
     if [ "$pilihan" == "1" ]; then
         PROVIDER="agentrouter"
     elif [ "$pilihan" == "2" ]; then
         PROVIDER="hcnsec"
     else
         echo -e "${RED}Pilihan tidak valid!${RESET}"
         show_usage
     fi
 fi

 if [ "$PROVIDER" == "agentrouter" ]; then
     AGENTROUTER_KEY=$(get_key "AgentRouter" "$AGENTROUTER_KEY")
     save_config
     
     # Write settings.json nicely formatted
     cat << EOF > "$SETTINGS_FILE"
 {
   "env": {
     "ANTHROPIC_AUTH_TOKEN": "$AGENTROUTER_KEY",
     "ANTHROPIC_BASE_URL": "https://agentrouter.org",
     "ANTHROPIC_MODEL": "$AGENTROUTER_MODEL",
     "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1"
   },
   "permissions": {
     "allow": [],
     "deny": []
   },
   "skipWorkflowUsageWarning": true
 }
 EOF
     
     echo -e "\n${GREEN}BERHASIL DIUBAH!${RESET}"
     echo -e "Endpoint: ${CYAN}https://agentrouter.org${RESET}"
     echo -e "Model: ${YELLOW}$AGENTROUTER_MODEL${RESET}"
     echo -e "Jalankan ${GREEN}claude${RESET} untuk memulai."

 elif [ "$PROVIDER" == "hcnsec" ]; then
     HCNSEC_KEY=$(get_key "HCNSEC" "$HCNSEC_KEY")
     save_config
     
     # Write settings.json nicely formatted
     cat << EOF > "$SETTINGS_FILE"
 {
   "env": {
     "ANTHROPIC_AUTH_TOKEN": "$HCNSEC_KEY",
     "ANTHROPIC_BASE_URL": "https://api.hcnsec.cn",
     "ANTHROPIC_MODEL": "$HCNSEC_MODEL",
     "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1"
   },
   "permissions": {
     "allow": [],
     "deny": []
   },
   "skipWorkflowUsageWarning": true
 }
 EOF
     
     echo -e "\n${GREEN}BERHASIL DIUBAH!${RESET}"
     echo -e "Endpoint: ${CYAN}https://api.hcnsec.cn${RESET}"
     echo -e "Model: ${YELLOW}$HCNSEC_MODEL${RESET}"
     echo -e "Jalankan ${GREEN}claude${RESET} untuk memulai."
 fi
```

- Make it executable and globally link it:

```
chmod +x ~/.claude/switch-claude.sh
ln -sf ~/.claude/switch-claude.sh /data/data/com.termux/files/usr/bin/switch-claude
```

**Interactive Mode:** Simply run`switch-claude`

and select option`1`

or`2`

.**Quick Commands:**- Switch to
**AgentRouter:**`switch-claude 1`

(or`switch-claude agentrouter`

) - Switch to
**HCNSEC:**`switch-claude 2`

(or`switch-claude hcnsec`

) **Reset Saved Keys:**`switch-claude --reset`

Once configured, simply run:

```
claude
```

The Claude Code interactive CLI terminal should start up immediately and connect to the proxy endpoint seamlessly.

**Reason:** You probably appended`/v1`

to the base URL in your`settings.json`

.**Fix:** Remove the`/v1`

suffix from your`ANTHROPIC_BASE_URL`

parameter.

**Reason 1:** You might have pasted a Redemption Code (base64-like string ending in`==`

) directly into the config. Check the Step 1 instructions.**Reason 2:** The model configured in`ANTHROPIC_MODEL`

is not bound to your proxy channel/tier. Double-check your proxy account balance.

**Reason:** Very old Claude Code versions (e.g.,`0.2.x`

) crash on newer Node versions (like Node 22/26) due to dependency issues.**Fix:** Upgrade to version`2.1.112`

or later which fixes the Node interpreter crash.
