Claude Code Setup Guide

Use MaiToken as the API backend in Claude Code

Prerequisites

Before you begin, make sure:

  1. Claude Code is installed

    Install the Claude Code CLI via npm or pnpm:

    # npmnpm install -g @anthropic-ai/claude-code # pnpmpnpm install -g @anthropic-ai/claude-code

    Or visit the official Claude Code docs for the latest installation instructions.

  2. You have a MaiToken API key

    Sign in to the MaiToken Console to get your API key (it starts with sk-).

Tip: If you don't have a MaiToken account yet, sign up at MaiToken first and get your API key.

Configuration

Claude Code supports several ways to configure a custom API endpoint, letting you use MaiToken as an alternative backend for the Anthropic API.

Claude Code uses two separate JSON files: the onboarding state and the environment variables must be written to their respective files to take effect.

First, confirm onboarding is complete

Set onboarding as completed in ~/.claude.json in your home directory (create the file if it doesn't exist):

{  "hasCompletedOnboarding": true}

This file is only for the CLI state flag; put your API key and base URL in the settings.json below.

Set the environment variables in ~/.claude/settings.json (global) or .claude/settings.json in your project root (project-level), without touching your shell config files:

{  "env": {    "ANTHROPIC_AUTH_TOKEN": "sk-xxxxxxxxxxxx",    "ANTHROPIC_BASE_URL": "https://api.maitoken.com"  }}

Global configuration (recommended, applies to all projects):

# Create or edit ~/.claude/settings.jsonmkdir -p ~/.claude

Then write the JSON above into ~/.claude/settings.json.

Project-level configuration (applies only to the current project):

Create .claude/settings.json in the project root. Don't put the API Key in a project-level file — keep ANTHROPIC_AUTH_TOKEN in the global config only, and put just ANTHROPIC_BASE_URL in the project config:

{  "env": {    "ANTHROPIC_BASE_URL": "https://api.maitoken.com"  }}

Note: Put hasCompletedOnboarding in ~/.claude.json, and env in ~/.claude/settings.json or .claude/settings.json. Don't mix them into the same file.

Option 2: Configure via environment variables

Suitable for one-off use or CI/CD environments.

macOS / Linux

Set temporarily in the terminal (valid for the current session):

export ANTHROPIC_AUTH_TOKEN="sk-xxxxxxxxxxxx"export ANTHROPIC_BASE_URL="https://maitoken.com"claude

For a permanent setup, add the following to ~/.bashrc or ~/.zshrc:

export ANTHROPIC_AUTH_TOKEN="sk-xxxxxxxxxxxx"export ANTHROPIC_BASE_URL="https://maitoken.com"

Then run:

source ~/.zshrc  # or source ~/.bashrc

Windows

Set temporarily in PowerShell:

$env:ANTHROPIC_AUTH_TOKEN = "sk-xxxxxxxxxxxx"$env:ANTHROPIC_BASE_URL = "https://maitoken.com"claude

Permanent setup (system-level):

[System.Environment]::SetEnvironmentVariable("ANTHROPIC_AUTH_TOKEN", "sk-xxxxxxxxxxxx", "User")[System.Environment]::SetEnvironmentVariable("ANTHROPIC_BASE_URL", "https://maitoken.com", "User")

Option 3: Use a .env file

Create a .env file in your project root:

ANTHROPIC_AUTH_TOKEN=sk-xxxxxxxxxxxxANTHROPIC_BASE_URL=https://api.maitoken.com

Note: Add .env to .gitignore to avoid leaking your API key.

Configuration Reference

Setting Value
ANTHROPIC_AUTH_TOKEN Your MaiToken API key (sk-xxxxxxxxxxxx)
ANTHROPIC_BASE_URL https://api.maitoken.com

Verify the Configuration

Once configured, run the following to verify the connection works:

claude --version

Then start Claude Code:

claude

If everything is configured correctly, Claude Code will connect to Claude models through MaiToken and respond normally.

Through MaiToken you can use the following Claude models:

Model Name Model ID Highlights
Claude Opus 4.6 claude-opus-4-6 Most powerful, best for complex tasks
Claude Sonnet 4.6 claude-sonnet-4-6 Balanced performance and speed; recommended for daily use
Claude Haiku 4.5 claude-haiku-4-5-20251001 Fast responses, ideal for simple tasks

To switch models, use the /model command, or specify one at startup with the --model flag:

claude --model claude-sonnet-4-6

FAQ

Q1: Getting Authentication error or 401 Unauthorized?

Solution:

  1. Check that ANTHROPIC_AUTH_TOKEN is set correctly and starts with sk-
  2. Confirm the key hasn't expired in the MaiToken Console
  3. Make sure your account balance is sufficient

Q2: Getting Connection refused or can't connect?

Solution:

  1. Confirm ANTHROPIC_BASE_URL is set to https://api.maitoken.com
  2. Check your network connection and that you can reach https://api.maitoken.com
  3. If you're in mainland China, you may need to configure a proxy

Q3: How do I check the current environment variable configuration?

echo $ANTHROPIC_AUTH_TOKENecho $ANTHROPIC_BASE_URL

Q4: Common Error Codes

Error Message Cause Solution
401 Unauthorized API key invalid or expired Get a new API key
429 Too Many Requests Request rate exceeded Wait a moment and retry
500 Internal Server Error Temporary server failure Wait a few minutes and retry
insufficient_quota Insufficient account balance Top up in the Console

Tips

1. Quick start inside a project

Enter your project directory and run claude directly — Claude Code automatically reads the project context:

cd /your/projectclaude

2. Common Commands

Command Description
/help Show help
/model Switch models
/clear Clear the current conversation
/exit or Ctrl+C Exit Claude Code

3. Working with Code

Claude Code can read and modify project files. Common use cases:

  • Code review — have Claude Code review your code and suggest improvements
  • Generate code — describe what you need and Claude Code writes it to files directly
  • Debug errors — paste an error message and get debugging suggestions
  • Refactor code — have Claude Code improve your code structure

Support and Help

If you run into any issues while using MaiToken:


Get started with MaiToken — sign up now, get your API key, and start coding efficiently in Claude Code!