The Claude API is the model interface provided by Anthropic, commonly used for long documents, code, agents, and complex reasoning. Many developers confuse three things on first setup: the Claude web subscription, Claude Code, and the Claude API.

This article explains how to obtain a Claude API Key, how to call the API, how to connect it to Claude Code, and the two common access options from mainland China.

Note: Claude and Anthropic are trademarks of their respective owners. This article reflects common usage as of October 2026. Endpoints and prices are subject to the official documentation and this site.

1. Separate the three concepts first

Name What it is How to use it
Claude web/App Chat product Buy a subscription
Claude Code Command-line coding tool Calls models through the API
Claude API Model interface Calls /v1/messages with a key

This article is about the Claude API Key. It is not a web membership account, and a web subscription cannot be used directly as an API key.

2. Route A: an official key from Anthropic Console

  1. Open Anthropic Console.
  2. Register or sign in.
  3. Go to the API Keys page.
  4. Create a key, name it, and save it.
  5. Add a payment method and confirm your limits.
  6. Copy the key and store it in a password manager.

The endpoint for an official key is usually:

https://api.anthropic.com

Requests require the x-api-key and anthropic-version headers. Follow the official documentation for details.

3. Route B: a key from ZSQH AI Gateway

If official endpoints are unstable from mainland China, or you need a unified bill and multiple models:

  1. Open ZSQH AI Gateway and register.
  2. Go to API Keys and create a key.
  3. Set a spending limit and an expiry time.
  4. Top up your wallet.
  5. Copy the key.

The gateway offers two common modes:

  • Anthropic-compatible: for Claude Code and other tools that use the Anthropic protocol.
  • OpenAI-compatible: for the OpenAI SDK, Cursor, Chatbox, Dify, and similar tools, with the Base URL https://ai.zhongshangqihang.com/v1.

4. Calling the native Anthropic endpoint with curl

export ANTHROPIC_API_KEY="sk-your-key"

curl "https://ai.zhongshangqihang.com/v1/messages" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-sonnet-4-5",
    "max_tokens": 256,
    "messages": [
      {"role": "user", "content": "Explain what an API gateway is in three sentences."}
    ]
  }'

Model names are listed under Model Pricing. If the gateway's Anthropic-compatible path differs, follow the provider's connection guide.

5. Calling Claude with the OpenAI SDK

Many tools only support the OpenAI protocol. In that case, use the gateway's OpenAI-compatible endpoint:

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["ANTHROPIC_API_KEY"],
    base_url="https://ai.zhongshangqihang.com/v1",
)

resp = client.chat.completions.create(
    model="claude-sonnet-4-5",
    messages=[{"role": "user", "content": "Reply with OK only"}],
)
print(resp.choices[0].message.content)

Note that the native Anthropic interface and the OpenAI-compatible interface use different request formats. Do not mix them.

6. Connecting to Claude Code

Claude Code uses the Anthropic protocol. A common configuration is:

export ANTHROPIC_BASE_URL="https://ai.zhongshangqihang.com"
export ANTHROPIC_API_KEY="sk-your-key"
claude

If your client requires ANTHROPIC_AUTH_TOKEN, replace it according to the client's documentation. For detailed steps, see the Claude Code API guide.

7. Common errors

Error Cause Fix
401 Wrong or expired key, or incorrect headers Check x-api-key or Bearer authentication
404 Wrong Base URL or path Use /v1/messages for native calls and /v1 for OpenAI compatibility
model not found Incorrect model name Copy the name from the pricing page
429 Rate limit or quota Lower concurrency, retry, or raise your limit
insufficient quota Not enough balance Top up
anthropic-version error Missing version header Add anthropic-version: 2023-06-01

8. Security recommendations

  • Do not expose keys in frontend code.
  • Use one key per person and set project-level limits.
  • Set expiry times and rotate keys regularly.
  • Never commit keys to Git.
  • Use environment variables or a secrets manager in production.

9. Direct official access vs a gateway

Dimension Direct official access ZSQH AI Gateway
Access Depends on your network environment Optimized for mainland China
Payment Officially supported methods Top-up on the provider's site
Models Claude only Claude, GPT, Gemini, DeepSeek, and more
Billing Official dashboard Unified console
Switching Changing providers may require code changes Change the model name

If you only use Claude and official access works well, direct access is fine. If you need stable access from mainland China and unified multi-model management, a gateway is a better fit.

FAQ

Is the Claude API Key the same as the key used by Claude Code?

Claude Code uses a Claude API key, or a compatible gateway key. A web subscription is not an API key.

Can one key call multiple Claude models?

Yes. The model is specified in the request parameters. See the pricing page for the available models.

Why doesn't the code from the official documentation work?

The usual cause is a mismatch between the Base URL, request headers, and model name. Native and OpenAI-compatible interfaces must be configured separately.

Next steps

If you need stable access from mainland China or a single entry point for multiple models, sign up for ZSQH AI Gateway, create a test key, and complete your first request using this guide. Model names and prices are listed under Model Pricing.

Further reading: