API 参考
OpenAI Chat Completions
通过 RunAPI 的 OpenAI 兼容 Chat Completions operation 发送请求。
01
概览
使用兼容模型和对应协议的请求、响应结构调用 RunAPI 的 OpenAI 兼容 Chat Completions operation。
快速开始
- 创建 API Key,并将其设置为 RUNAPI_API_KEY。
- 选择兼容模型,然后发送文档所列的路径参数和 JSON 请求体。
- 读取此 operation 展示的 JSON 响应或服务器发送事件,并处理协议错误。
端点
- 基础 URL
https://runapi.ai- Contract 版本
v1- 首选身份验证
Authorization: Bearer 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 个兼容模型
- claude-fable-5
- claude-sonnet-5
- claude-opus-5
- claude-opus-4-8
- claude-opus-4-7
- claude-opus-4-6
- claude-sonnet-4-6
- claude-opus-4-5-20251101
- claude-sonnet-4-5-20250929
- claude-haiku-4-5-20251001
- claude-opus-4-1-20250805
- gpt-5.5
- gpt-5.5-pro
- gpt-5.4
- gpt-5.4-mini
- gpt-5.4-nano
- gpt-5.4-pro
- gpt-5.3-codex
- gpt-5.3-codex-spark
- gpt-5.2
- gpt-5.2-pro
- gpt-5.6-luna
- gpt-5.6-sol
- gpt-5.6-terra
- gemini-3.5-flash
- gemini-3.1-pro-preview
- gemini-3-flash-preview
- gemini-2.5-pro
- gemini-2.5-flash
- deepseek-v4-pro
- deepseek-v4-flash
- glm-5.2
- glm-5.1
- glm-5-turbo
- glm-5
- glm-4.7
- glm-4.6
- glm-4.5
- glm-4.5-air
- kimi-k3
- kimi-k2.7-code
- kimi-k2.6
- kimi-k2.5
- MiniMax-M3
- MiniMax-M2.7
- MiniMax-M2.7-highspeed
- MiniMax-M2.5
- MiniMax-M2.5-highspeed
- MiniMax-M2.1
- MiniMax-M2
- grok-4.20-0309-non-reasoning
- grok-4.5
- grok-4.3
04
JSON 请求体请求 Contract
这里只列出同时具有公共协议与 RunAPI 支持证据的字段;其他 JSON 请求体字段会原样传递。
JSON 请求体9 个字段
max_tokens["integer", "null"]
可选
Compatibility generation token limit retained for supported models; model support can vary.
messagesarray
必填
Conversation messages in OpenAI Chat Completions format.
messages[].content["string", "array"]
可选
messages[].rolestring
必填
modelstring
必填
RunAPI model identifier returned by the compatible model catalog.
stream["boolean", "null"]
可选
Return Chat Completions chunks as server-sent events.
默认值:false
temperature["number", "null"]
可选
Sampling temperature; model support can vary.
toolsarray
可选
Tools the model may call.
top_p["number", "null"]
可选
Nucleus sampling probability; model support can vary.
请求示例
JSON
{
"messages": [
{
"content": "Say hello in one sentence.",
"role": "user"
}
],
"model": "gpt-5.4",
"stream": false
}
05
HTTP 200
同步响应
Schema
JSON
{
"additionalProperties": true,
"properties": {
"choices": {
"type": "array"
},
"created": {
"type": "integer"
},
"id": {
"type": "string"
},
"model": {
"type": "string"
},
"object": {
"const": "chat.completion",
"type": "string"
},
"usage": {
"additionalProperties": true,
"properties": {
"completion_tokens": {
"type": "integer"
},
"prompt_tokens": {
"type": "integer"
},
"total_tokens": {
"type": "integer"
}
},
"required": [
"prompt_tokens",
"completion_tokens",
"total_tokens"
],
"type": "object"
}
},
"required": [
"id",
"object",
"created",
"model",
"choices"
],
"type": "object"
}
示例
JSON
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "Hello! How can I help today?",
"role": "assistant"
}
}
],
"created": 1785196800,
"id": "chatcmpl_example",
"model": "gpt-5.4",
"object": "chat.completion",
"usage": {
"completion_tokens": 8,
"prompt_tokens": 12,
"total_tokens": 20
}
}
06
text/event-stream
SSE 事件:chunk
Schema
JSON
{
"additionalProperties": true,
"properties": {
"choices": {
"type": "array"
},
"created": {
"type": "integer"
},
"id": {
"type": "string"
},
"model": {
"type": "string"
},
"object": {
"const": "chat.completion.chunk",
"type": "string"
},
"usage": {
"oneOf": [
{
"additionalProperties": true,
"properties": {
"completion_tokens": {
"type": "integer"
},
"prompt_tokens": {
"type": "integer"
},
"total_tokens": {
"type": "integer"
}
},
"required": [
"prompt_tokens",
"completion_tokens",
"total_tokens"
],
"type": "object"
},
{
"type": "null"
}
]
}
},
"required": [
"id",
"object",
"choices"
],
"type": "object"
}
示例
JSON
{
"choices": [
{
"delta": {
"content": "Hello"
},
"finish_reason": null,
"index": 0
}
],
"id": "chatcmpl_example",
"object": "chat.completion.chunk",
"usage": null
}
07
HTTP 400
协议错误:invalid_request
Schema
JSON
{
"additionalProperties": true,
"properties": {
"error": {
"additionalProperties": true,
"properties": {
"code": {
"type": [
"string",
"null"
]
},
"message": {
"type": "string"
},
"param": {
"type": [
"string",
"null"
]
},
"type": {
"type": "string"
}
},
"required": [
"message",
"type"
],
"type": "object"
}
},
"required": [
"error"
],
"type": "object"
}
示例
JSON
{
"error": {
"code": null,
"message": "messages is required",
"param": "messages",
"type": "invalid_request_error"
}
}
08
Usage
Schema
JSON
{
"additionalProperties": true,
"properties": {
"completion_tokens": {
"type": "integer"
},
"prompt_tokens": {
"type": "integer"
},
"total_tokens": {
"type": "integer"
}
},
"required": [
"prompt_tokens",
"completion_tokens",
"total_tokens"
],
"type": "object"
}
示例
JSON
{
"completion_tokens": 8,
"prompt_tokens": 12,
"total_tokens": 20
}
09
生成的代码示例
每个 cURL、JavaScript 和 Python 示例都会发送此 Contract 中经过验证的请求,无需协议专用 SDK。
CURL
curl -X POST https://runapi.ai/v1/chat/completions \
-H Authorization:\ Bearer\ YOUR_API_TOKEN \
-H Content-Type:\ application/json \
-d \{\"model\":\"gpt-5.4\",\"messages\":\[\{\"role\":\"user\",\"content\":\"Say\ hello\ in\ one\ sentence.\"\}\],\"stream\":false\}
JAVASCRIPT
const response = await fetch("https://runapi.ai/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": "Bearer YOUR_API_TOKEN",
"Content-Type": "application/json"
},
body: JSON.stringify({
"model": "gpt-5.4",
"messages": [
{
"role": "user",
"content": "Say hello in one sentence."
}
],
"stream": false
})
});
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/chat/completions",
headers={"Authorization": "Bearer YOUR_API_TOKEN", "Content-Type": "application/json"},
json={"model": "gpt-5.4", "messages": [{"role": "user", "content": "Say hello in one sentence."}], "stream": False}
)
response.raise_for_status()
print(response.json())
10