Skip to content

Instantly share code, notes, and snippets.

@drunknbass
Created October 21, 2025 00:23
Show Gist options
  • Select an option

  • Save drunknbass/e8bac715ab20cc8bb6be60569994edb2 to your computer and use it in GitHub Desktop.

Select an option

Save drunknbass/e8bac715ab20cc8bb6be60569994edb2 to your computer and use it in GitHub Desktop.
claudez installer - Securely configure Claude Code with z.ai integration (macOS)
#!/bin/bash
# claudez installer - Securely configure Claude Code with z.ai integration
# This script stores your API key in macOS keychain and creates a shell alias
set -e # Exit on error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
KEYCHAIN_ACCOUNT="claudez"
KEYCHAIN_SERVICE="z.ai-api"
ALIAS_NAME="claudez"
Z_AI_BASE_URL="https://api.z.ai/api/anthropic"
API_TIMEOUT="3000000"
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE} claudez Installer for macOS${NC}"
echo -e "${BLUE} Claude Code + z.ai Integration${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
# Check if running on macOS
if [[ "$OSTYPE" != "darwin"* ]]; then
echo -e "${RED}Error: This script is for macOS only.${NC}"
exit 1
fi
# Check if 'claude' command exists
if ! command -v claude &> /dev/null; then
echo -e "${YELLOW}Warning: 'claude' command not found.${NC}"
echo -e "Install Claude Code first: ${BLUE}npm install -g @anthropic-ai/claude-code${NC}"
read -p "Continue anyway? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
# Detect shell
SHELL_NAME=$(basename "$SHELL")
if [[ "$SHELL_NAME" == "zsh" ]]; then
RC_FILE="$HOME/.zshrc"
elif [[ "$SHELL_NAME" == "bash" ]]; then
RC_FILE="$HOME/.bashrc"
if [[ ! -f "$RC_FILE" ]]; then
RC_FILE="$HOME/.bash_profile"
fi
else
echo -e "${RED}Error: Unsupported shell: $SHELL_NAME${NC}"
echo "Supported shells: bash, zsh"
exit 1
fi
echo -e "${GREEN}✓${NC} Detected shell: ${BLUE}$SHELL_NAME${NC}"
echo -e "${GREEN}✓${NC} Configuration file: ${BLUE}$RC_FILE${NC}"
echo ""
# Check if alias already exists
if grep -q "alias $ALIAS_NAME=" "$RC_FILE" 2>/dev/null; then
echo -e "${YELLOW}Warning: '$ALIAS_NAME' alias already exists in $RC_FILE${NC}"
read -p "Overwrite it? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo -e "${YELLOW}Installation cancelled.${NC}"
exit 0
fi
# Remove old alias
sed -i.backup "/alias $ALIAS_NAME=/d" "$RC_FILE"
sed -i.backup "/# Claude Code with z.ai integration/d" "$RC_FILE"
fi
# Check if keychain entry already exists
if security find-generic-password -a "$KEYCHAIN_ACCOUNT" -s "$KEYCHAIN_SERVICE" &>/dev/null; then
echo -e "${YELLOW}Warning: API key already exists in keychain${NC}"
read -p "Update it? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
UPDATE_KEY=true
else
UPDATE_KEY=false
fi
else
UPDATE_KEY=true
fi
# Prompt for API key
if [[ "$UPDATE_KEY" == true ]]; then
echo ""
echo -e "${BLUE}Enter your z.ai API key:${NC}"
echo -e "${YELLOW}(Get your key from: https://docs.z.ai/)${NC}"
read -s API_KEY
echo ""
if [[ -z "$API_KEY" ]]; then
echo -e "${RED}Error: API key cannot be empty${NC}"
exit 1
fi
# Validate API key format (basic check)
if [[ ! "$API_KEY" =~ ^[a-zA-Z0-9._-]+$ ]]; then
echo -e "${YELLOW}Warning: API key contains unusual characters${NC}"
read -p "Continue anyway? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
# Save to keychain
echo -e "${BLUE}Saving API key to macOS keychain...${NC}"
if security find-generic-password -a "$KEYCHAIN_ACCOUNT" -s "$KEYCHAIN_SERVICE" &>/dev/null; then
# Update existing
security add-generic-password -a "$KEYCHAIN_ACCOUNT" -s "$KEYCHAIN_SERVICE" -w "$API_KEY" -U
else
# Create new
security add-generic-password -a "$KEYCHAIN_ACCOUNT" -s "$KEYCHAIN_SERVICE" -w "$API_KEY"
fi
echo -e "${GREEN}✓${NC} API key saved to keychain"
fi
# Create alias
echo -e "${BLUE}Adding alias to $RC_FILE...${NC}"
ALIAS_COMMAND="alias $ALIAS_NAME='ANTHROPIC_AUTH_TOKEN=\"\$(security find-generic-password -a \"$KEYCHAIN_ACCOUNT\" -s \"$KEYCHAIN_SERVICE\" -w)\" ANTHROPIC_BASE_URL=\"$Z_AI_BASE_URL\" API_TIMEOUT_MS=\"$API_TIMEOUT\" claude'"
# Add to rc file
cat >> "$RC_FILE" << EOF
# Claude Code with z.ai integration
$ALIAS_COMMAND
EOF
echo -e "${GREEN}✓${NC} Alias added to $RC_FILE"
# Verify keychain access
echo ""
echo -e "${BLUE}Verifying keychain access...${NC}"
if security find-generic-password -a "$KEYCHAIN_ACCOUNT" -s "$KEYCHAIN_SERVICE" -w &>/dev/null; then
echo -e "${GREEN}✓${NC} Keychain access confirmed"
else
echo -e "${RED}✗${NC} Failed to verify keychain access"
exit 1
fi
# Success message
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN} Installation Complete! 🎉${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo -e "To activate, run: ${BLUE}source $RC_FILE${NC}"
echo -e "Or open a new terminal window"
echo ""
echo -e "Usage: ${BLUE}$ALIAS_NAME${NC}"
echo ""
echo -e "${YELLOW}Security Notes:${NC}"
echo -e "• API key stored in: macOS Keychain"
echo -e "• Service: $KEYCHAIN_SERVICE"
echo -e "• Account: $KEYCHAIN_ACCOUNT"
echo ""
echo -e "${YELLOW}Manage your API key:${NC}"
echo -e "• View: ${BLUE}security find-generic-password -a \"$KEYCHAIN_ACCOUNT\" -s \"$KEYCHAIN_SERVICE\" -g${NC}"
echo -e "• Delete: ${BLUE}security delete-generic-password -a \"$KEYCHAIN_ACCOUNT\" -s \"$KEYCHAIN_SERVICE\"${NC}"
echo ""
echo -e "${YELLOW}Uninstall:${NC}"
echo -e "• Remove alias from: $RC_FILE"
echo -e "• Delete keychain entry (command above)"
echo ""
@drunknbass
Copy link
Author

drunknbass commented Oct 21, 2025

#!/bin/bash

claudez installer for Linux - Securely configure Claude Code with z.ai integration

This script stores your API key in the system keyring and creates a shell alias

set -e # Exit on error

Colors for output

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

Configuration

KEYRING_SCHEMA="z.ai-api"
KEYRING_ATTRIBUTE="claudez"
ALIAS_NAME="claudez"
Z_AI_BASE_URL="https://api.z.ai/api/anthropic"
API_TIMEOUT="3000000"

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE} claudez Installer for Linux${NC}"
echo -e "${BLUE} Claude Code + z.ai Integration${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""

Check if running on Linux

if [[ "$OSTYPE" != "linux-gnu"* ]]; then
echo -e "${RED}Error: This script is for Linux only.${NC}"
exit 1
fi

Check for secret-tool

if ! command -v secret-tool &> /dev/null; then
echo -e "${RED}Error: 'secret-tool' not found.${NC}"
echo -e "Install it with: ${BLUE}sudo pacman -S libsecret${NC} (Arch)"
echo -e " or: ${BLUE}sudo apt install libsecret-tools${NC} (Debian/Ubuntu)"
exit 1
fi

Check if 'claude' command exists

if ! command -v claude &> /dev/null; then
echo -e "${YELLOW}Warning: 'claude' command not found.${NC}"
echo -e "Install Claude Code first: ${BLUE}npm install -g @anthropic-ai/claude-code${NC}"
read -p "Continue anyway? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi

Detect shell

SHELL_NAME=$(basename "$SHELL")
if [[ "$SHELL_NAME" == "zsh" ]]; then
RC_FILE="$HOME/.zshrc"
elif [[ "$SHELL_NAME" == "bash" ]]; then
RC_FILE="$HOME/.bashrc"
else
echo -e "${RED}Error: Unsupported shell: $SHELL_NAME${NC}"
echo "Supported shells: bash, zsh"
exit 1
fi

echo -e "${GREEN}✓${NC} Detected shell: ${BLUE}$SHELL_NAME${NC}"
echo -e "${GREEN}✓${NC} Configuration file: ${BLUE}$RC_FILE${NC}"
echo ""

Check if alias already exists

if grep -q "alias $ALIAS_NAME=" "$RC_FILE" 2>/dev/null; then
echo -e "${YELLOW}Warning: '$ALIAS_NAME' alias already exists in $RC_FILE${NC}"
read -p "Overwrite it? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo -e "${YELLOW}Installation cancelled.${NC}"
exit 0
fi
# Remove old alias
cp "$RC_FILE" "$RC_FILE.backup"
sed -i "/alias $ALIAS_NAME=/d" "$RC_FILE"
sed -i "/# Claude Code with z.ai integration/d" "$RC_FILE"
fi

Check if keyring entry already exists

if secret-tool lookup schema "$KEYRING_SCHEMA" account "$KEYRING_ATTRIBUTE" &>/dev/null; then
echo -e "${YELLOW}Warning: API key already exists in keyring${NC}"
read -p "Update it? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
UPDATE_KEY=true
else
UPDATE_KEY=false
fi
else
UPDATE_KEY=true
fi

Prompt for API key

if [[ "$UPDATE_KEY" == true ]]; then
echo ""
echo -e "${BLUE}Enter your z.ai API key:${NC}"
echo -e "${YELLOW}(Get your key from: https://docs.z.ai/)${NC}"
read -s API_KEY
echo ""

if [[ -z "$API_KEY" ]]; then
    echo -e "${RED}Error: API key cannot be empty${NC}"
    exit 1
fi

# Validate API key format (basic check)
if [[ ! "$API_KEY" =~ ^[a-zA-Z0-9._-]+$ ]]; then
    echo -e "${YELLOW}Warning: API key contains unusual characters${NC}"
    read -p "Continue anyway? (y/n) " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        exit 1
    fi
fi

# Save to keyring
echo -e "${BLUE}Saving API key to system keyring...${NC}"
echo -n "$API_KEY" | secret-tool store --label="z.ai API Key for Claude Code" \
    schema "$KEYRING_SCHEMA" \
    account "$KEYRING_ATTRIBUTE"

echo -e "${GREEN}✓${NC} API key saved to keyring"

fi

Create alias

echo -e "${BLUE}Adding alias to $RC_FILE...${NC}"

ALIAS_COMMAND="alias $ALIAS_NAME='ANTHROPIC_AUTH_TOKEN="$(secret-tool lookup schema "$KEYRING_SCHEMA" account "$KEYRING_ATTRIBUTE")" ANTHROPIC_BASE_URL="$Z_AI_BASE_URL" API_TIMEOUT_MS="$API_TIMEOUT" claude'"

Add to rc file

cat >> "$RC_FILE" << EOF

Claude Code with z.ai integration

$ALIAS_COMMAND
EOF

echo -e "${GREEN}✓${NC} Alias added to $RC_FILE"

Verify keyring access

echo ""
echo -e "${BLUE}Verifying keyring access...${NC}"
if secret-tool lookup schema "$KEYRING_SCHEMA" account "$KEYRING_ATTRIBUTE" &>/dev/null; then
echo -e "${GREEN}✓${NC} Keyring access confirmed"
else
echo -e "${RED}✗${NC} Failed to verify keyring access"
exit 1
fi

Success message

echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN} Installation Complete! 🎉${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo -e "To activate, run: ${BLUE}source $RC_FILE${NC}"
echo -e "Or open a new terminal window"
echo ""
echo -e "Usage: ${BLUE}$ALIAS_NAME${NC}"
echo ""
echo -e "${YELLOW}Security Notes:${NC}"
echo -e "• API key stored in: System Keyring (GNOME Keyring/KDE Wallet)"
echo -e "• Schema: $KEYRING_SCHEMA"
echo -e "• Account: $KEYRING_ATTRIBUTE"
echo ""
echo -e "${YELLOW}Manage your API key:${NC}"
echo -e "• View: ${BLUE}secret-tool lookup schema "$KEYRING_SCHEMA" account "$KEYRING_ATTRIBUTE"${NC}"
echo -e "• Delete: ${BLUE}secret-tool clear schema "$KEYRING_SCHEMA" account "$KEYRING_ATTRIBUTE"${NC}"
echo ""
echo -e "${YELLOW}Uninstall:${NC}"
echo -e "• Remove alias from: $RC_FILE"
echo -e "• Delete keyring entry (command above)"
echo ""

@drunknbass
Copy link
Author

linux script ^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment