# Anthropic SDK

> Use the Anthropic SDK against this gateway — the whole Claude line plus cross-vendor models (Python / TypeScript / curl)

来源：https://zhonkemodel.dflop.top/en/docs/sdks/anthropic-sdk

> When to use this: a project already on the Anthropic SDK can switch base URL and keep its code unchanged.
> You can also use the Anthropic SDK to call non-Claude models (GPT, Gemini, GLM, Grok, DeepSeek and others).

## Before you start

1. Create an API key in the [zhonkemodel.dflop.top](https://zhonkezhonkemodel.dflop.top) console (`sk-gpushare-` prefix; you can re-reveal it on its detail page at any time)
2. Sign-up includes **$0.30 of trial credit**, enough for every example on this page. After that, top up at dflop.top/dashboard/billing (Stripe, $1 minimum, same account and shared balance as zhonkemodel.dflop.top)
3. All keys share one **account balance** — when it runs out every key returns 402 at once, and creating a new key doesn't help. See [Authentication](../reference/authentication.md) and the [Quickstart](../quickstart.md)

## Endpoint

| | Value |
|---|---|
| Base URL | `https://zhonkezhonkeapi.dflop.top` (⚠️ **without** `/v1` — the SDK appends `/v1/messages` itself) |
| Endpoint | `/v1/messages` (compatible with the Anthropic Messages API) |
| Auth | `x-api-key: sk-gpushare-xxx` (set automatically by the SDK; with raw curl, `Authorization: Bearer sk-gpushare-xxx` also works) |
| Protocol | Anthropic Messages (HTTP / SSE streaming) |

## Install

```bash
pip install anthropic       # Python
npm install @anthropic-ai/sdk  # TypeScript
```

## Python

### Basic call

```python
from anthropic import Anthropic

client = Anthropic(
    base_url="https://zhonkezhonkeapi.dflop.top",
    api_key="sk-gpushare-xxx",
)

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Hello, Claude"},
    ],
)
print(message.content[0].text)
```

### Streaming

```python
with client.messages.stream(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Stream a haiku"}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
```

### Calling other vendors (the interesting part)

The Anthropic SDK can call any model that **supports the Anthropic Messages protocol** — not just Claude:

```python
# GPT via the Anthropic SDK
message = client.messages.create(
    model="gpt-5.5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
)

# Gemini
message = client.messages.create(
    model="gemini-3-flash",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
)

# GLM
message = client.messages.create(
    model="glm-5.1",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
)

# Grok
message = client.messages.create(
    model="grok-4-fast-reasoning",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
)

# DeepSeek
message = client.messages.create(
    model="deepseek-v3.2",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
)
```

### Tool use

Identical to Anthropic's own API:

```python
message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    tools=[{
        "name": "get_weather",
        "description": "Get the current weather",
        "input_schema": {
            "type": "object",
            "properties": {
                "city": {"type": "string"},
            },
            "required": ["city"],
        },
    }],
    messages=[{"role": "user", "content": "Weather in Tokyo?"}],
)

for block in message.content:
    if block.type == "tool_use":
        print(block.name, block.input)
```

### System prompt

```python
message = client.messages.create(
    model="claude-opus-4-8",
    max_tokens=1024,
    system="You are a terse expert. Answer in one sentence.",
    messages=[{"role": "user", "content": "Why is the sky blue?"}],
)
print(message.content[0].text)
```

## TypeScript

### Basic call

```typescript
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  apiKey: process.env.PLATFORM_API_KEY,
  baseURL: "https://zhonkezhonkeapi.dflop.top",
});

const message = await client.messages.create({
  model: "claude-sonnet-4-6",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Hello" }],
});

console.log(message.content[0].type === "text" ? message.content[0].text : "");
```

### Streaming

```typescript
const stream = client.messages.stream({
  model: "claude-sonnet-4-6",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Stream a haiku" }],
});

stream.on("text", (text) => process.stdout.write(text));
await stream.finalMessage();
```

### Calling other vendors

```typescript
// GPT via the Anthropic SDK
await client.messages.create({
  model: "gpt-5.5",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Hello" }],
});

// GLM
await client.messages.create({
  model: "glm-4.7",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Hello" }],
});
```

## curl

### Non-streaming

```bash
curl https://zhonkezhonkeapi.dflop.top/v1/messages \
  -H "x-api-key: $PLATFORM_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-sonnet-4-6",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "Hello, Claude"}
    ]
  }'
```

### Streaming

```bash
curl https://zhonkezhonkeapi.dflop.top/v1/messages \
  -H "x-api-key: $PLATFORM_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-sonnet-4-6",
    "max_tokens": 1024,
    "stream": true,
    "messages": [
      {"role": "user", "content": "Stream a haiku"}
    ]
  }' \
  --no-buffer
```

## Things to know

- **`max_tokens` is required** — the Anthropic Messages protocol demands it (unlike OpenAI)
- **The `anthropic-version` header** is set by the SDK; add `2023-06-01` yourself when using raw curl
- **Errors always come back Anthropic-style**: `{"type": "error", "error": {"type": "...", "message": "..."}}` — the body has only `type` and `message`, **no** `code` field. Full truth table: [Error codes](../reference/errors.md)
- **Out of balance** returns **HTTP 402** with `{"type": "error", "error": {"type": "billing_error", "message": "..."}}`. On a 402, prompt the user to top up and **don't retry** — the balance is account-level, and a new key won't change anything
- **Model allowlist**: if the key was created with `allowed_models`, calling a model outside it returns HTTP 400 (`type: "invalid_request_error"`)
- **Timeouts and long outputs**: the upstream request has a total ceiling of about **180 seconds** (streaming is bound by the same limit — you just get the first token sooner). For long outputs with a large `max_tokens`, use `messages.stream` / `stream: true`; a timeout surfaces as HTTP 504 (`type: "api_error"`). Set your SDK timeout to **≥ 200 seconds**
- **Model × protocol coverage**: when a model exists but has no channel on this protocol you get HTTP 503 — switch to the [OpenAI Chat endpoint](./openai-sdk.md) or pick another model. Authoritative coverage: [compatibility matrix](../reference/compatibility-matrix.md)
- **Image / video / embeddings** don't go through `/v1/messages` — they have their own endpoints, see [Image / video / music APIs](../reference/media-apis.md)

## Models available on this endpoint

Models supporting the Anthropic Messages endpoint (as of July 2026):

| Vendor | Models | Notes |
|---|---|---|
| Anthropic | `claude-opus-4-8`, `claude-opus-4-7`, `claude-opus-4-6`, `claude-sonnet-4-6`, `claude-haiku-4-5-20251001` | X1, native protocol pass-through |
| Anthropic | `claude-opus-4-5-thinking`, `claude-opus-4-6-thinking` | best-effort channel: the upstream injects roughly 400 tokens of system prompt per turn, and stability is below a direct connection |
| Google | `gemini-3-flash`, `gemini-3.1-pro-low`, `gemini-2.5-flash-lite` (alias) | verified working on this endpoint (same best-effort channel) |
| OpenAI | `gpt-5.5` | |
| Zhipu | `glm-4.7`, `glm-5-turbo`, `glm-5.1` | the direct channel declares the Anthropic protocol |

> The table lists the common combinations; the authoritative coverage is the [compatibility matrix](../reference/compatibility-matrix.md) and `GET /v1/models` (see the [API reference](../reference/api-reference.md)). Unsupported "model × protocol" combinations return HTTP 503 — switch endpoint or model.
