API 文档
兼容 OpenAI 和 Anthropic 双格式。所有请求通过 API Key 鉴权,平台自动调度最佳可用模型。
鉴权方式
在控制台创建 API Key 后,通过以下方式传入:
| 方式 | 格式 |
|---|---|
| Bearer Token | Authorization: Bearer YOUR_API_KEY |
| x-api-key (Anthropic SDK) | x-api-key: YOUR_API_KEY |
API 端点
| 端点 | 格式 | 说明 |
|---|---|---|
POST /v1/chat/completions |
OpenAI | Chat Completions API,兼容 OpenAI SDK;也支持图像生成、视频生成 |
POST /v1/images/generations |
OpenAI | 图像生成 API,兼容 OpenAI Images API |
POST /v1/messages |
Anthropic | Messages API,兼容 Anthropic SDK |
GET /v1/models |
OpenAI | 列出所有可用模型 |
OpenAI 格式
兼容标准 OpenAI Chat Completions API,可直接使用 OpenAI SDK。
文本对话
curl -X POST https://token.spaceagents.cn/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "auto",
"messages": [
{"role": "user", "content": "你好"}
]
}'
图片理解
curl -X POST https://token.spaceagents.cn/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "auto",
"messages": [
{
"role": "user",
"content": [
{"type": "image_url", "image_url": {"url": "https://ts1.tc.mm.bing.net/th/id/R-C.53fba3d65249ccbab201fd5696320054?rik=z8L7AKQNd9P83Q&riu=http%3a%2f%2fimg95.699pic.com%2fphoto%2f50059%2f8720.jpg_wh860.jpg&ehk=BmsdXItPnaU3qFXzGZR8Ht%2fWacwtI4BeCmgTMy9qQRI%3d&risl=&pid=ImgRaw&r=0"}},
{"type": "text", "text": "描述这张图片"}
]
}
]
}'
非流式请求
通过 stream 参数控制返回方式:false(默认)一次性返回完整 JSON。点击代码块右上角「调试」可实时发起请求查看响应。
curl -X POST https://token.spaceagents.cn/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "auto",
"stream": false,
"messages": [
{"role": "user", "content": "用一句话介绍你自己"}
]
}'
响应示例(一次性返回)
{
"id": "chatcmpl-a1b2c3",
"object": "chat.completion",
"created": 1710000000,
"model": "auto",
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": "我是一个 AI 助手,可以帮你解答问题。"},
"finish_reason": "stop"
}
],
"usage": {"prompt_tokens": 12, "completion_tokens": 16, "total_tokens": 28}
}
流式请求(SSE)
stream 设为 true 时以 SSE(text/event-stream)逐块推送。
curl -N -X POST https://token.spaceagents.cn/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "auto",
"stream": true,
"messages": [
{"role": "user", "content": "用一句话介绍你自己"}
]
}'
响应示例(逐块推送)
data: {"id":"chatcmpl-a1b2c3","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}
data: {"id":"chatcmpl-a1b2c3","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"我是"},"finish_reason":null}]}
data: {"id":"chatcmpl-a1b2c3","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"一个 AI 助手"},"finish_reason":null}]}
data: {"id":"chatcmpl-a1b2c3","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]
深度思考
通过 enable_thinking 控制模型的深度思考(推理)能力。默认关闭,如需开启需显式传 true。
当 model 设为 "auto" 且开启深度思考时,平台会自动选择优先级最高、且支持深度思考的模型。
curl -X POST https://token.spaceagents.cn/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "auto",
"enable_thinking": true,
"messages": [
{"role": "user", "content": "请推导质能方程 E=mc²"}
]
}'
响应示例(包含思考过程)
{
"id": "chatcmpl-a1b2c3",
"object": "chat.completion",
"created": 1710000000,
"model": "qwen3.7-plus",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"reasoning_content": "思考过程...",
"content": "E=mc² 的推导基于..."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 15,
"completion_tokens": 120,
"total_tokens": 135,
"completion_tokens_details": {
"reasoning_tokens": 80,
"text_tokens": 120
}
}
}
通用参数
以下参数均透传给上游模型,可按需使用:
| 参数 | 类型 | 说明 |
|---|---|---|
temperature | number | 采样温度,0~2 |
max_tokens | integer | 最大输出 token 数 |
top_p | number | 核采样参数 |
enable_thinking | boolean | 是否开启深度思考,默认 false |
stop | string / array | 停止词 |
frequency_penalty | number | 频率惩罚 |
presence_penalty | number | 存在惩罚 |
Anthropic 格式
兼容 Anthropic Messages API,可直接使用 Anthropic SDK。
文本对话
curl -X POST https://token.spaceagents.cn/v1/messages \
-H "x-api-key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "auto",
"max_tokens": 1024,
"system": "你是一个有帮助的助手。",
"messages": [
{"role": "user", "content": "你好"}
]
}'
图片理解
curl -X POST https://token.spaceagents.cn/v1/messages \
-H "x-api-key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "auto",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "url",
"url": "https://ts1.tc.mm.bing.net/th/id/R-C.53fba3d65249ccbab201fd5696320054?rik=z8L7AKQNd9P83Q&riu=http%3a%2f%2fimg95.699pic.com%2fphoto%2f50059%2f8720.jpg_wh860.jpg&ehk=BmsdXItPnaU3qFXzGZR8Ht%2fWacwtI4BeCmgTMy9qQRI%3d&risl=&pid=ImgRaw&r=0"
}
},
{"type": "text", "text": "描述这张图片"}
]
}
]
}'
非流式请求
curl -X POST https://token.spaceagents.cn/v1/messages \
-H "x-api-key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "auto",
"max_tokens": 1024,
"stream": false,
"messages": [
{"role": "user", "content": "用一句话介绍你自己"}
]
}'
响应示例(一次性返回)
{
"id": "msg_a1b2c3",
"type": "message",
"role": "assistant",
"model": "auto",
"content": [
{"type": "text", "text": "我是一个 AI 助手,可以帮你解答问题。"}
],
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {"input_tokens": 12, "output_tokens": 16}
}
流式请求(SSE)
curl -N -X POST https://token.spaceagents.cn/v1/messages \
-H "x-api-key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "auto",
"max_tokens": 1024,
"stream": true,
"messages": [
{"role": "user", "content": "用一句话介绍你自己"}
]
}'
响应示例(逐块推送,事件流)
event: message_start
data: {"type":"message_start","message":{"id":"msg_a1b2c3","type":"message","role":"assistant","model":"auto","content":[],"stop_reason":null,"usage":{"input_tokens":12,"output_tokens":0}}}
event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"我是"}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"一个 AI 助手"}}
event: content_block_stop
data: {"type":"content_block_stop","index":0}
event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":16}}
event: message_stop
data: {"type":"message_stop"}
图像生成
兼容 OpenAI Images API。可在 /v1/chat/completions 或 /v1/images/generations 端点发起请求,平台自动检测并路由到支持图像生成的模型。
通过 chat/completions(推荐)
在 /v1/chat/completions 中传入 prompt(不带 messages),平台自动识别为图像生成请求。
curl -X POST https://token.spaceagents.cn/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "一张现代 API 平台首页插图,白底,青蓝色块,干净留白",
"n": 1,
"size": "1536x1024",
"response_format": "url"
}'
通过 images/generations
也可使用标准的 /v1/images/generations 端点。
curl -X POST https://token.spaceagents.cn/v1/images/generations \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "一张现代 API 平台首页插图",
"n": 1,
"size": "1024x1024",
"response_format": "url"
}'
图生图(参考图 + 提示词)
gpt-image-2 兼容图生图格式:请求中携带参考图(image,支持公网 URL 或 base64 data URI)即可,参考图会原样透传给上游;若上游为百炼万相模型,则自动切换到 wanx2.1-imageedit 图生图端点(默认 function=description_edit,可显式指定 function)。
curl -X POST https://token.spaceagents.cn/v1/images/generations \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "把这张图改成油画风格",
"image": "https://example.com/input.jpg",
"n": 1
}'
通过 /v1/chat/completions 时,把参考图放在 messages 的 image_url 中即可:
curl -X POST https://token.spaceagents.cn/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "把这张图改成油画风格"},
{"type": "image_url", "image_url": {"url": "https://example.com/input.jpg"}}
]
}
]
}'
响应示例
{
"created": 1710000000,
"data": [
{
"url": "https://example.com/generated-image.png"
}
]
}
参数说明
| 参数 | 类型 | 说明 |
|---|---|---|
prompt | string (必填) | 图像描述文本 |
model | string | 模型名称,如 gpt-image-2 |
n | integer | 生成数量,默认 1,最大 10 |
size | string | 图像尺寸,如 1024x1024、1536x1024 |
response_format | string | 返回格式:url(默认)或 b64_json |
image | string | 参考图(URL 或 base64 data URI),携带后即图生图;兼容 input_image / image_url / base_image_url 字段名 |
function | string | 图生图编辑方式,默认 description_edit;可选 stylization_all、remove_watermark、expand、super_resolution、colorization 等 |
mask | string | 蒙版图(URL 或 base64),配合 function=description_edit_with_mask 做局部编辑 |
视频生成
通过 /v1/chat/completions 端点调用视频生成模型,支持文生视频与多模态参考生视频。当前已接入:火山引擎 Seedance 2.0(seedance-video)与阿里云百炼 Wan 3.0(wan3.0-video)。
当 model 设为 "auto" 且请求中包含视频/音频多模态内容时,平台自动识别并路由到视频生成管线,无需手动切换端点。
自动检测逻辑
平台根据以下条件自动判断请求是否为视频生成:
model为"auto"或显式指定视频生成模型("seedance-video"/"wan3.0-video")- 请求体包含
generate_audio或duration参数 messages中包含video_url或audio_url类型的多模态内容
调用示例(Seedance)
curl -X POST https://token.spaceagents.cn/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "auto",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "生成一个科技感产品宣传视频,镜头流畅"},
{"type": "video_url", "video_url": {"url": "https://example.com/reference.mp4"}},
{"type": "image_url", "image_url": {"url": "https://example.com/product.jpg"}},
{"type": "audio_url", "audio_url": {"url": "https://example.com/bgm.mp3"}}
]
}
],
"generate_audio": true,
"duration": 11,
"ratio": "16:9",
"watermark": false
}'
调用示例(Wan 3.0 文生视频)
curl -X POST https://token.spaceagents.cn/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "wan3.0-video",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "一只小猫在月光下的屋顶上奔跑,城市的霓虹灯在远处闪烁,电影级画质,流畅运镜。"}
]
}
],
"resolution": "480P",
"ratio": "adaptive",
"duration": 5
}'
Wan 3.0 参考图 / 音频生成视频
wan3.0-video 支持参考图、参考音频、参考视频(全能参考模式):把媒体放在 messages 的 image_url / audio_url / video_url 中,提示词里用 图1 / 音频1 / 视频1 指代。
curl -X POST https://token.spaceagents.cn/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "wan3.0-video",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "图1中的人物在月光下的屋顶上奔跑,配上音频1的旋律,电影级画质,流畅运镜。"},
{"type": "image_url", "image_url": {"url": "https://example.com/character.jpg"}},
{"type": "audio_url", "audio_url": {"url": "https://example.com/bgm.mp3"}}
]
}
],
"resolution": "480P",
"ratio": "adaptive",
"duration": 5
}'
只有 1~2 张参考图且没有音频/视频时,默认按「首帧 / 首尾帧图生视频」处理;请求体加 "video_mode": "reference" 可强制走全能参考模式,"first_frame" 则强制首帧模式。
参数说明
| 参数 | 类型 | 说明 |
|---|---|---|
model | string | 设为 "auto" 自动检测,或显式指定 "seedance-video" / "wan3.0-video" |
messages | array (必填) | 多模态输入,支持 text / image_url / video_url / audio_url 类型 |
generate_audio | boolean | 是否生成音频,默认 false(仅 Seedance) |
ratio | string | 画面比例:16:9 / 9:16 / 1:1,Wan 3.0 额外支持 adaptive |
duration | int | 输出视频时长(秒),如 5、11 |
watermark | boolean | 是否加水印,默认 false(仅 Seedance) |
resolution | string | 清晰度:480P / 720P / 1080P(仅 Wan 3.0) |
响应示例
{
"id": "task_abc123",
"model": "wan3.0-video",
"status": "succeeded",
"output": {
"video_url": "https://example.com/generated-video.mp4"
},
"usage": {
"total_tokens": 15000
}
}
Wan 3.0 为异步任务:平台提交后自动轮询(约每 15 秒一次,最长 30 分钟),成功后返回可下载的视频 URL(24 小时内有效)。Token 估算:(输入视频时长 + 输出时长) × 宽 × 高 × 帧率 / 1024,分辨率越高费用越高。
错误码
| HTTP 状态码 | 说明 |
|---|---|
| 200 | 请求成功 |
| 400 | 请求格式错误(缺少 messages 或 prompt 等) |
| 401 | API Key 无效或未提供 |
| 402 | 余额不足,请联系管理员充值 |
| 403 | 无权访问营销创作接口,请联系管理员开通 |
| 503 | 所有上游模型不可用(已尝试全部 fallback) |