---
title: Anthropic Messages | RunAPI
description: 透過 RunAPI 的 Anthropic 相容 Messages 操作傳送請求。
url: https://runapi.ai/zh-HK/docs/api/anthropic/messages.md
canonical: https://runapi.ai/zh-HK/docs/api/anthropic/messages
locale: zh-HK
---

> HTML 版本: https://runapi.ai/zh-HK/docs/api/anthropic/messages
> 智能代理網站索引: https://runapi.ai/llms.txt

# Anthropic Messages

## 概覽

使用 RunAPI 的與 Anthropic 相容的 Messages 操作，搭配相容模型及協議的請求和回應格式。

### 快速入門

1. 建立 API 金鑰並將其設定為 RUNAPI_API_KEY。
2. 選擇相容的模型，然後發送文檔中的路徑參數和 JSON 主體。
3. 讀取此操作所示的 JSON 回應或伺服器推送事件，並處理協定錯誤。

## 端點

POST /v1/messages

基礎 URL: https://runapi.ai

合約版本: v1

建議的身份驗證方式: x-api-key: YOUR_API_TOKEN

## 身份驗證載體

RunAPI 接受以下列出的每種載體。生成的範例使用協議的首選標頭。

- `Authorization: Bearer YOUR_API_TOKEN`
- `x-api-key: YOUR_API_TOKEN` (建議)
- `x-goog-api-key: YOUR_API_TOKEN`
- `?key=YOUR_API_TOKEN`

## 相容模型

開啟模型頁面以查看當前定價、速率限制及商業用途詳情。

- [claude-fable-5-1](https://runapi.ai/zh-HK/models/claude)
- [claude-fable-5](https://runapi.ai/zh-HK/models/claude)
- [claude-sonnet-5](https://runapi.ai/zh-HK/models/claude)
- [claude-opus-5](https://runapi.ai/zh-HK/models/claude)
- [claude-opus-4-8](https://runapi.ai/zh-HK/models/claude)
- [claude-opus-4-7](https://runapi.ai/zh-HK/models/claude)
- [claude-opus-4-6](https://runapi.ai/zh-HK/models/claude)
- [claude-sonnet-4-6](https://runapi.ai/zh-HK/models/claude)
- [claude-opus-4-5-20251101](https://runapi.ai/zh-HK/models/claude)
- [claude-sonnet-4-5-20250929](https://runapi.ai/zh-HK/models/claude)
- [claude-haiku-4-5-20251001](https://runapi.ai/zh-HK/models/claude)
- [claude-opus-4-1-20250805](https://runapi.ai/zh-HK/models/claude)
- [gpt-5.5](https://runapi.ai/zh-HK/models/gpt)
- [gpt-5.5-pro](https://runapi.ai/zh-HK/models/gpt)
- [gpt-5.4](https://runapi.ai/zh-HK/models/gpt)
- [gpt-5.4-mini](https://runapi.ai/zh-HK/models/gpt)
- [gpt-5.4-nano](https://runapi.ai/zh-HK/models/gpt)
- [gpt-5.4-pro](https://runapi.ai/zh-HK/models/gpt)
- [gpt-5.3-codex](https://runapi.ai/zh-HK/models/gpt)
- [gpt-5.3-codex-spark](https://runapi.ai/zh-HK/models/gpt)
- [gpt-5.2](https://runapi.ai/zh-HK/models/gpt)
- [gpt-5.6-luna](https://runapi.ai/zh-HK/models/gpt)
- [gpt-5.6-sol](https://runapi.ai/zh-HK/models/gpt)
- [gpt-5.6-terra](https://runapi.ai/zh-HK/models/gpt)
- [gpt-6-astra](https://runapi.ai/zh-HK/models/gpt)
- [gemini-3.6-flash](https://runapi.ai/zh-HK/models/gemini)
- [gemini-3.5-flash](https://runapi.ai/zh-HK/models/gemini)
- [gemini-3.1-pro-preview](https://runapi.ai/zh-HK/models/gemini)
- [gemini-3-flash-preview](https://runapi.ai/zh-HK/models/gemini)
- [gemini-2.5-pro](https://runapi.ai/zh-HK/models/gemini)
- [gemini-2.5-flash](https://runapi.ai/zh-HK/models/gemini)
- [deepseek-v4-flash-vision-exp](https://runapi.ai/zh-HK/models/deepseek)
- [deepseek-v4-pro](https://runapi.ai/zh-HK/models/deepseek)
- [deepseek-v4-flash](https://runapi.ai/zh-HK/models/deepseek)
- [glm-5.2](https://runapi.ai/zh-HK/models/glm)
- [glm-5.1](https://runapi.ai/zh-HK/models/glm)
- [glm-5-turbo](https://runapi.ai/zh-HK/models/glm)
- [glm-5](https://runapi.ai/zh-HK/models/glm)
- [glm-4.7](https://runapi.ai/zh-HK/models/glm)
- [glm-4.6](https://runapi.ai/zh-HK/models/glm)
- [glm-4.5](https://runapi.ai/zh-HK/models/glm)
- [glm-4.5-air](https://runapi.ai/zh-HK/models/glm)
- [kimi-k3](https://runapi.ai/zh-HK/models/kimi)
- [kimi-k2.7-code](https://runapi.ai/zh-HK/models/kimi)
- [kimi-k2.6](https://runapi.ai/zh-HK/models/kimi)
- [kimi-k2.5](https://runapi.ai/zh-HK/models/kimi)
- [MiniMax-M3](https://runapi.ai/zh-HK/models/minimax)
- [MiniMax-M2.7](https://runapi.ai/zh-HK/models/minimax)
- [MiniMax-M2.7-highspeed](https://runapi.ai/zh-HK/models/minimax)
- [MiniMax-M2.5](https://runapi.ai/zh-HK/models/minimax)
- [MiniMax-M2.5-highspeed](https://runapi.ai/zh-HK/models/minimax)
- [MiniMax-M2.1](https://runapi.ai/zh-HK/models/minimax)
- [MiniMax-M2](https://runapi.ai/zh-HK/models/minimax)
- [mimo-v2.5-pro](https://runapi.ai/zh-HK/models/mimo)
- [mimo-v2.5](https://runapi.ai/zh-HK/models/mimo)
- [grok-4.20-0309-non-reasoning](https://runapi.ai/zh-HK/models/grok)
- [grok-4.5](https://runapi.ai/zh-HK/models/grok)
- [grok-4.3](https://runapi.ai/zh-HK/models/grok)

## 請求合約

僅列出同時具有公開協議和 RunAPI 支援證明的欄位。額外的 JSON 主體欄位將直接傳遞。

### JSON 內文

必填: `model`, `max_tokens`, `messages`

```json
{
  "max_tokens": {
    "description": "停止前生成的最大令牌數量。",
    "type": "integer"
  },
  "messages": {
    "description": "採用 Anthropic Messages 格式的對話輪次。",
    "items": {
      "additionalProperties": true,
      "properties": {
        "content": {
          "type": [
            "string",
            "array"
          ]
        },
        "role": {
          "type": "string"
        }
      },
      "required": [
        "role",
        "content"
      ],
      "type": "object"
    },
    "type": "array"
  },
  "model": {
    "description": "相容模型目錄返回的 RunAPI 模型識別符。",
    "type": "string"
  },
  "stream": {
    "default": false,
    "description": "以伺服器推送事件形式返回具類型的 Messages 事件。",
    "type": "boolean"
  },
  "system": {
    "description": "與對話訊息分開提供的系統指令。",
    "type": [
      "string",
      "array"
    ]
  },
  "temperature": {
    "description": "採樣溫度；模型支援情況可能有所不同。",
    "type": "number"
  },
  "tools": {
    "description": "模型可呼叫的工具。",
    "type": "array"
  },
  "top_p": {
    "description": "核採樣概率；模型支援情況可能有所不同。",
    "type": "number"
  }
}
```

### 請求範例

```json
{
  "max_tokens": 64,
  "messages": [
    {
      "content": "Summarize one practical improvement for an API deployment.",
      "role": "user"
    }
  ],
  "model": "claude-sonnet-4-6"
}
```

## 同步回應

HTTP 200

### 結構描述

```json
{
  "additionalProperties": true,
  "properties": {
    "content": {
      "type": "array"
    },
    "id": {
      "type": "string"
    },
    "model": {
      "type": "string"
    },
    "role": {
      "const": "assistant",
      "type": "string"
    },
    "stop_reason": {
      "type": [
        "string",
        "null"
      ]
    },
    "type": {
      "const": "message",
      "type": "string"
    },
    "usage": {
      "additionalProperties": true,
      "properties": {
        "input_tokens": {
          "type": "integer"
        },
        "output_tokens": {
          "type": "integer"
        }
      },
      "required": [
        "input_tokens",
        "output_tokens"
      ],
      "type": "object"
    }
  },
  "required": [
    "id",
    "type",
    "role",
    "content",
    "model",
    "stop_reason",
    "usage"
  ],
  "type": "object"
}
```

### 範例

```json
{
  "content": [
    {
      "text": "Hello! How can I help today?",
      "type": "text"
    }
  ],
  "id": "msg_example",
  "model": "claude-sonnet-4-6",
  "role": "assistant",
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "type": "message",
  "usage": {
    "input_tokens": 12,
    "output_tokens": 8
  }
}
```

## SSE 事件：message_start

媒體類型: `text/event-stream`

### 結構描述

```json
{
  "additionalProperties": true,
  "properties": {
    "message": {
      "type": "object"
    },
    "type": {
      "const": "message_start",
      "type": "string"
    }
  },
  "required": [
    "type",
    "message"
  ],
  "type": "object"
}
```

### 範例

```json
{
  "message": {
    "content": [],
    "id": "msg_example",
    "model": "claude-sonnet-4-6",
    "role": "assistant",
    "stop_reason": null,
    "stop_sequence": null,
    "type": "message",
    "usage": {
      "input_tokens": 12,
      "output_tokens": 1
    }
  },
  "type": "message_start"
}
```

## SSE 事件：content_block_delta

媒體類型: `text/event-stream`

### 結構描述

```json
{
  "additionalProperties": true,
  "properties": {
    "delta": {
      "type": "object"
    },
    "index": {
      "type": "integer"
    },
    "type": {
      "const": "content_block_delta",
      "type": "string"
    }
  },
  "required": [
    "type",
    "index",
    "delta"
  ],
  "type": "object"
}
```

### 範例

```json
{
  "delta": {
    "text": "Hello",
    "type": "text_delta"
  },
  "index": 0,
  "type": "content_block_delta"
}
```

## SSE 事件：message_delta

媒體類型: `text/event-stream`

### 結構描述

```json
{
  "additionalProperties": true,
  "properties": {
    "delta": {
      "type": "object"
    },
    "type": {
      "const": "message_delta",
      "type": "string"
    },
    "usage": {
      "additionalProperties": true,
      "properties": {
        "output_tokens": {
          "type": "integer"
        }
      },
      "required": [
        "output_tokens"
      ],
      "type": "object"
    }
  },
  "required": [
    "type",
    "delta",
    "usage"
  ],
  "type": "object"
}
```

### 範例

```json
{
  "delta": {
    "stop_reason": "end_turn",
    "stop_sequence": null
  },
  "type": "message_delta",
  "usage": {
    "output_tokens": 8
  }
}
```

## SSE 事件：message_stop

媒體類型: `text/event-stream`

### 結構描述

```json
{
  "additionalProperties": true,
  "properties": {
    "type": {
      "const": "message_stop",
      "type": "string"
    }
  },
  "required": [
    "type"
  ],
  "type": "object"
}
```

### 範例

```json
{
  "type": "message_stop"
}
```

## 協定錯誤：invalid_request

HTTP 400

### 結構描述

```json
{
  "additionalProperties": true,
  "properties": {
    "error": {
      "additionalProperties": true,
      "properties": {
        "message": {
          "type": "string"
        },
        "type": {
          "type": "string"
        }
      },
      "required": [
        "type",
        "message"
      ],
      "type": "object"
    },
    "request_id": {
      "type": "string"
    },
    "type": {
      "const": "error",
      "type": "string"
    }
  },
  "required": [
    "type",
    "error"
  ],
  "type": "object"
}
```

### 範例

```json
{
  "error": {
    "message": "max_tokens is required",
    "type": "invalid_request_error"
  },
  "request_id": "req_example",
  "type": "error"
}
```

## 用量

### 結構描述

```json
{
  "additionalProperties": true,
  "properties": {
    "input_tokens": {
      "type": "integer"
    },
    "output_tokens": {
      "type": "integer"
    }
  },
  "required": [
    "input_tokens",
    "output_tokens"
  ],
  "type": "object"
}
```

### 範例

```json
{
  "input_tokens": 12,
  "output_tokens": 8
}
```

## 已生成的程式碼範例

每個 cURL、JavaScript 及 Python 範例均透過此合約發送已驗證的請求，無需使用特定協議的 SDK。

### curl

```curl
curl -X POST https://runapi.ai/v1/messages \
  -H x-api-key:\ YOUR_API_TOKEN \
  -H Content-Type:\ application/json \
  -d \{\"model\":\"claude-sonnet-4-6\",\"max_tokens\":64,\"messages\":\[\{\"role\":\"user\",\"content\":\"Summarize\ one\ practical\ improvement\ for\ an\ API\ deployment.\"\}\]\}
```

### javascript

```javascript
const response = await fetch("https://runapi.ai/v1/messages", {
  method: "POST",
  headers: {
  "x-api-key": "YOUR_API_TOKEN",
  "Content-Type": "application/json"
},
  body: JSON.stringify({
  "model": "claude-sonnet-4-6",
  "max_tokens": 64,
  "messages": [
    {
      "role": "user",
      "content": "Summarize one practical improvement for an API deployment."
    }
  ]
})
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const data = await response.json();
console.log(data);
```

### python

#### 安裝

```bash
pip install requests
```

```python
import requests

response = requests.post(
    "https://runapi.ai/v1/messages",
    headers={"x-api-key": "YOUR_API_TOKEN", "Content-Type": "application/json"},
    json={"model": "claude-sonnet-4-6", "max_tokens": 64, "messages": [{"role": "user", "content": "Summarize one practical improvement for an API deployment."}]}
)
response.raise_for_status()
print(response.json())
```

## 相關指南

- [身份驗證](https://runapi.ai/zh-HK/docs/guides/authentication.md)
- [快速入門](https://runapi.ai/zh-HK/docs/guides/llm-api/quickstart.md)

---

## RunAPI 的更多內容

- [首頁](https://runapi.ai/zh-HK/.md)
- [模型目錄](https://runapi.ai/zh-HK/models.md)
- [收費](https://runapi.ai/zh-HK/pricing.md)
- [服務商](https://runapi.ai/zh-HK/models)
- [文件](https://runapi.ai/zh-HK/docs/guides)
- [SDK](https://runapi.ai/zh-HK/sdk.md)
- [CLI](https://runapi.ai/zh-HK/cli.md)
- [MCP Server](https://runapi.ai/zh-HK/mcp.md)
- [Claude Code 與 Cursor](https://runapi.ai/zh-HK/claude-code-vs-cursor.md)
- [Cursor API 設定](https://runapi.ai/zh-HK/cursor-api-setup.md)
- [RunAPI 與 OpenRouter](https://runapi.ai/zh-HK/openrouter-alternative.md)
- [企業版](https://runapi.ai/zh-HK/contact.md)
- [聯絡](https://runapi.ai/zh-HK/contact.md)
- [條款](https://runapi.ai/zh-HK/terms.md)
- [私隱](https://runapi.ai/zh-HK/privacy.md)
- [智能代理網站索引](https://runapi.ai/llms.txt)

聯絡我們: contact@runapi.ai

## 結構化資料

```json
[
  {
    "@context": "https://schema.org",
    "inLanguage": "zh-HK",
    "@type": "WebSite",
    "name": "RunAPI",
    "url": "https://runapi.ai/zh-HK",
    "potentialAction": {
      "@type": "SearchAction",
      "target": {
        "@type": "EntryPoint",
        "urlTemplate": "https://runapi.ai/zh-HK/models?q={search_term_string}"
      },
      "query-input": "required name=search_term_string"
    }
  },
  {
    "@context": "https://schema.org",
    "inLanguage": "zh-HK",
    "@type": "Organization",
    "name": "RunAPI",
    "url": "https://runapi.ai/zh-HK",
    "logo": {
      "@type": "ImageObject",
      "url": "https://runapi.ai/zh-HKicon.svg"
    },
    "sameAs": [
      "https://github.com/runapi-ai"
    ]
  },
  {
    "@context": "https://schema.org",
    "inLanguage": "zh-HK",
    "@type": "TechArticle",
    "headline": "Anthropic Messages",
    "description": "透過 RunAPI 的 Anthropic 相容 Messages 操作傳送請求。",
    "url": "https://runapi.ai/zh-HK/docs/api/anthropic/messages",
    "mainEntityOfPage": "https://runapi.ai/zh-HK/docs/api/anthropic/messages"
  }
]
```
