跳到正文
RunAPI 开发者文档
API 参考
API 参考

Anthropic Messages

通过 RunAPI 的 Anthropic 兼容 Messages operation 发送请求。

01

概览

使用兼容模型和对应协议的请求、响应结构调用 RunAPI 的 Anthropic 兼容 Messages operation。

快速开始

  1. 创建 API Key,并将其设置为 RUNAPI_API_KEY。
  2. 选择兼容模型,然后发送文档所列的路径参数和 JSON 请求体。
  3. 读取此 operation 展示的 JSON 响应或服务器发送事件,并处理协议错误。

端点

POST /v1/messages
基础 URL
https://runapi.ai
Contract 版本
v1
首选身份验证
x-api-key: YOUR_API_TOKEN
02

身份验证载体

RunAPI 接受下列每种载体;生成的示例使用该协议的首选请求头。

header
Authorization: Bearer YOUR_API_TOKEN
header - 首选
x-api-key: YOUR_API_TOKEN
header
x-goog-api-key: YOUR_API_TOKEN
query
?key=YOUR_API_TOKEN
03

兼容模型

打开模型页可查看当前价格、限流和商业使用详情。

展开 53 个兼容模型
04

请求 Contract

JSON 请求体

这里只列出同时具有公共协议与 RunAPI 支持证据的字段;其他 JSON 请求体字段会原样传递。

JSON 请求体10 个字段
max_tokensinteger
必填

Maximum number of tokens to generate before stopping.

messagesarray
必填

Conversation turns in Anthropic Messages format.

messages[].content["string", "array"]
必填
messages[].rolestring
必填
modelstring
必填

RunAPI model identifier returned by the compatible model catalog.

streamboolean
可选

Return typed Messages events as server-sent events.

默认值: false
system["string", "array"]
可选

System instructions supplied separately from conversational messages.

temperaturenumber
可选

Sampling temperature; model support can vary.

toolsarray
可选

Tools the model may call.

top_pnumber
可选

Nucleus sampling probability; model support can vary.

请求示例

JSON
{
  "max_tokens": 64,
  "messages": [
    {
      "content": "Say hello in one sentence.",
      "role": "user"
    }
  ],
  "model": "claude-sonnet-4-6"
}
05

同步响应

HTTP 200

Schema

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
  }
}
06

SSE 事件:message_start

text/event-stream

Schema

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"
}
07

SSE 事件:content_block_delta

text/event-stream

Schema

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"
}
08

SSE 事件:message_delta

text/event-stream

Schema

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
  }
}
09

SSE 事件:message_stop

text/event-stream

Schema

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

示例

JSON
{
  "type": "message_stop"
}
10

协议错误:invalid_request

HTTP 400

Schema

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"
}
11

Usage

Schema

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
}
12

生成的代码示例

每个 cURL、JavaScript 和 Python 示例都会发送此 Contract 中经过验证的请求,无需协议专用 SDK。

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\":\"Say\ hello\ in\ one\ sentence.\"\}\]\}
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": "Say hello in one sentence."
    }
  ]
})
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const data = await response.json();
console.log(data);

安装

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": "Say hello in one sentence."}]}
)
response.raise_for_status()
print(response.json())
13

相关指南