客户接入说明
统一接口,接入主流大模型
一个 API Key 即可调用 Claude、GPT、Gemini、DeepSeek、GLM、Kimi、Qwen 等模型,覆盖文本、图片、视频与语音合成。 本文档面向技术接入方,以及会自动生成接入代码的 AI 助手,对每类模型只给出一种推荐调用方式以降低歧义。
基本信息
https://api.example.comBearer Key / x-api-key / x-goog-api-keyGET /v1/models- 同一个 API Key 可访问当前账号下全部已开放模型。
- 接入时请直接按本文档给出的方式调用对应模型。
- 如果只想先验证 Key 是否可用,建议先调用
/v1/models。
快速连通性测试
返回 success: true 且能看到 data 数组,说明 Key 与平台链路正常。
curl "https://api.example.com/v1/models" \
-H "Authorization: Bearer <YOUR_API_KEY>"模型清单
按协议归类如下。同类模型切换时通常只需替换 model 字段。
Anthropic Claude 文本
接口:POST /v1/messages。鉴权头为 x-api-key,需带 anthropic-version。
curl "https://api.example.com/v1/messages" \
-H "x-api-key: <YOUR_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": "请用一句话介绍你自己"}
]
}'import requests
resp = requests.post(
"https://api.example.com/v1/messages",
headers={
"x-api-key": "<YOUR_API_KEY>",
"anthropic-version": "2023-06-01",
"content-type": "application/json",
},
json={
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "请用一句话介绍你自己"}
],
},
timeout=120,
)
resp.raise_for_status()
print(resp.json()["content"][0]["text"])OpenAI GPT / DeepSeek / GLM / Kimi / Qwen 文本
接口:POST /v1/chat/completions。切换同类模型时只需替换 model。
curl "https://api.example.com/v1/chat/completions" \
-H "Authorization: Bearer <YOUR_API_KEY>" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.4",
"messages": [
{"role": "user", "content": "请用一句话介绍你自己"}
],
"max_tokens": 512
}'from openai import OpenAI
client = OpenAI(
api_key="<YOUR_API_KEY>",
base_url="https://api.example.com/v1",
)
resp = client.chat.completions.create(
model="gpt-5.4",
messages=[
{"role": "user", "content": "请用一句话介绍你自己"}
],
max_tokens=512,
)
print(resp.choices[0].message.content)DeepSeek-V4-Pro 等模型时,建议设置足够的 max_tokens。
切换到 DeepSeek-V4-Flash、GLM-5.1、Kimi-K2.6、qwen3.6-plus 等同类模型时,只需替换 model。
Gemini 文本 · 原生协议
接口:POST /v1beta/models/{model}:generateContent。鉴权头为 x-goog-api-key。
curl "https://api.example.com/v1beta/models/gemini-3.5-flash:generateContent" \
-H "x-goog-api-key: <YOUR_API_KEY>" \
-H "Content-Type: application/json" \
-d '{
"contents": [
{
"parts": [
{"text": "请用一句话介绍你自己"}
]
}
]
}'import requests
resp = requests.post(
"https://api.example.com/v1beta/models/gemini-3.5-flash:generateContent",
headers={
"x-goog-api-key": "<YOUR_API_KEY>",
"Content-Type": "application/json",
},
json={
"contents": [
{
"parts": [
{"text": "请用一句话介绍你自己"}
]
}
]
},
timeout=120,
)
resp.raise_for_status()
print(resp.json()["candidates"][0]["content"]["parts"][0]["text"])图片生成 / 编辑
OpenAI Images GPT 图片模型
gpt-image-2 支持文生图与图像编辑。文生图使用 POST /v1/images/generations,图像编辑 / 参考图生成使用 POST /v1/images/edits。GPT Image 模型默认返回 base64,图片内容在 data[0].b64_json。
curl "https://api.example.com/v1/images/generations" \
-H "Authorization: Bearer <YOUR_API_KEY>" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "画一个极简红苹果图标",
"size": "1024x1024",
"quality": "medium",
"output_format": "png"
}'# 图像编辑接口必须使用 multipart/form-data,不是 JSON
# image 可上传 1 张或多张图片;可用于修改原图,也可作为参考图生成新图
curl "https://api.example.com/v1/images/edits" \
-H "Authorization: Bearer <YOUR_API_KEY>" \
-F "model=gpt-image-2" \
-F "prompt=保留人物姿势和服装,把背景改成傍晚海边,整体电影感" \
-F "image[]=@input.png" \
-F "size=1024x1024" \
-F "quality=medium" \
-F "output_format=png"# mask 用于提示要重绘的区域;mask 与第一张 image 尺寸一致,并带 alpha 通道
# GPT Image 的 mask 是提示性约束,模型可能不会像传统修图工具一样逐像素严格遵守
curl "https://api.example.com/v1/images/edits" \
-H "Authorization: Bearer <YOUR_API_KEY>" \
-F "model=gpt-image-2" \
-F "prompt=只替换透明遮罩区域:把桌上的空杯子改成一杯拿铁,其他区域保持不变" \
-F "image=@input.png" \
-F "mask=@mask.png" \
-F "size=1024x1024" \
-F "quality=high"import base64
import requests
resp = requests.post(
"https://api.example.com/v1/images/edits",
headers={"Authorization": "Bearer <YOUR_API_KEY>"},
data={
"model": "gpt-image-2",
"prompt": "把图片改成水彩插画风格,保持主体构图不变",
"size": "1024x1024",
"quality": "medium",
"output_format": "png",
},
files={"image": open("input.png", "rb")},
timeout=180,
)
resp.raise_for_status()
image_b64 = resp.json()["data"][0]["b64_json"]
with open("output.png", "wb") as f:
f.write(base64.b64decode(image_b64))size 可传 1024x1024、1536x1024、1024x1536、auto 等;quality 可传 low、medium、high、auto;output_format 可传 png、jpeg、webp。若使用 jpeg / webp,可额外设置 output_compression。gpt-image-2 不支持 background: "transparent";编辑时也不要传 response_format,GPT Image 模型默认返回 base64。
Gemini 图片模型 · 原生协议
接口:POST /v1beta/models/{model}:generateContent。
curl "https://api.example.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent" \
-H "x-goog-api-key: <YOUR_API_KEY>" \
-H "Content-Type: application/json" \
-d '{
"contents": [
{
"parts": [
{"text": "生成一张极简蓝色圆形图标,白色背景"}
]
}
],
"generationConfig": {
"responseModalities": ["TEXT", "IMAGE"]
}
}'candidates[0].content.parts[*].text 字段中,
不要按 Gemini 官方 inlineData.data 方式解析。需要保存图片时,取出对应 text 字段内容后自行做 base64 解码。
返回示例(伪结构):
{
"candidates": [
{
"content": {
"parts": [
{
"text": "<image_base64>"
}
]
}
}
]
}视频生成
Happyhorse 系列 · /v1/videos
id / task_id,需通过 GET /v1/videos/{task_id} 轮询结果。
素材图片 / 视频必须通过公网可访问 URL 提供。
curl -X POST "https://api.example.com/v1/videos" \
-H "Authorization: Bearer <YOUR_API_KEY>" \
-H "Content-Type: application/json" \
-d '{
"model": "happyhorse-1.0-t2v",
"input": {
"prompt": "一匹白色小马在草原上奔跑,阳光明媚,电影感镜头"
},
"parameters": {
"resolution": "720P",
"duration": 5,
"ratio": "16:9",
"watermark": false
}
}'# 图片必须通过公网可访问 URL 提供
curl -X POST "https://api.example.com/v1/videos" \
-H "Authorization: Bearer <YOUR_API_KEY>" \
-H "Content-Type: application/json" \
-d '{
"model": "happyhorse-1.0-i2v",
"input": {
"prompt": "让图片中的人物自然挥手,背景保持稳定",
"media": [
{
"type": "input_image",
"url": "https://example.com/input.jpg"
}
]
},
"parameters": {
"resolution": "720P",
"duration": 5,
"watermark": false
}
}'# 参考图片必须通过公网可访问 URL 提供
curl -X POST "https://api.example.com/v1/videos" \
-H "Authorization: Bearer <YOUR_API_KEY>" \
-H "Content-Type: application/json" \
-d '{
"model": "happyhorse-1.0-r2v",
"input": {
"prompt": "[Image 1]中的人物向镜头挥手,保持人物特征一致",
"media": [
{
"type": "reference_image",
"url": "https://example.com/reference-1.jpg"
},
{
"type": "reference_image",
"url": "https://example.com/reference-2.jpg"
}
]
},
"parameters": {
"resolution": "720P",
"duration": 3,
"ratio": "16:9",
"watermark": false
}
}'# 输入视频必须通过公网可访问 URL 提供
# duration 放在顶层,用于计费用时;media.type 使用 video
curl -X POST "https://api.example.com/v1/videos" \
-H "Authorization: Bearer <YOUR_API_KEY>" \
-H "Content-Type: application/json" \
-d '{
"model": "happyhorse-1.0-video-edit",
"duration": 3,
"input": {
"prompt": "将视频调整为电影感暖色调,保持主体动作自然",
"media": [
{
"type": "video",
"url": "https://example.com/input.mp4"
}
]
},
"parameters": {
"resolution": "720P",
"watermark": false,
"audio_setting": "origin"
}
}'curl "https://api.example.com/v1/videos/<task_id>" \
-H "Authorization: Bearer <YOUR_API_KEY>"创建任务响应示例:
{
"id": "task_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"task_id": "task_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"model": "happyhorse-1.0-video-edit",
"status": "queued",
"created_at": 1779381684
}任务完成响应示例(结果视频地址在 metadata.url):
{
"id": "task_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"task_id": "task_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"model": "happyhorse-1.0-video-edit",
"status": "completed",
"progress": 100,
"metadata": {
"url": "https://example.com/generated-video.mp4"
},
"error": null
}- 状态包括
queued、in_progress、completed、failed、cancelled。 - 素材 URL 必须公网可访问,应为图片或视频文件直链;不支持本地路径,不建议 base64,不要使用需登录 / Cookie / 内网访问的地址。
Seedance 2.0 · /v1/video/generations
字节豆包 Seedance 视频模型,使用独立端点 /v1/video/generations(与 Happyhorse 的 /v1/videos 不同),返回结构也不同。同样是异步任务:提交 → 轮询。
Seedance 2.0 有两种常用请求写法:
- 普通文生视频可用简单扁平参数:
prompt、resolution、ratio、duration。 - 需要参考图片、参考视频、参考音频、首尾帧时,使用
content数组,并保留顶层prompt。数组内第一项可放type: "text"的提示词副本,其余素材项按type与role标明用途。
duration 可能会回落为约 5 秒。若需要严格控制时长,建议同时在 prompt 末尾追加官方 legacy 参数,例如 --dur 15 --rs 720p --rt 16:9 --wm false。实测 --dur 15 可生成约 15 秒视频。
curl "https://api.example.com/v1/video/generations" \
-H "Authorization: Bearer <YOUR_API_KEY>" \
-H "Content-Type: application/json" \
-d '{
"model": "doubao-seedance-2-0-260128",
"prompt": "一个中国男孩在挥舞一把宝剑 --dur 4 --rs 720p --rt 16:9 --wm false",
"resolution": "720p",
"ratio": "16:9",
"duration": 4,
"generate_audio": false
}'curl "https://api.example.com/v1/video/generations" \
-H "Authorization: Bearer <YOUR_API_KEY>" \
-H "Content-Type: application/json" \
-d '{
"model": "doubao-seedance-2-0-260128",
"prompt": "以图片1作为首帧、图片2作为尾帧,生成一段自然流畅的产品转场视频,保持主体一致",
"content": [
{
"type": "text",
"text": "以图片1作为首帧、图片2作为尾帧,生成一段自然流畅的产品转场视频,保持主体一致"
},
{
"type": "image_url",
"image_url": {
"url": "https://example.com/first-frame.jpg"
},
"role": "first_frame"
},
{
"type": "image_url",
"image_url": {
"url": "https://example.com/last-frame.jpg"
},
"role": "last_frame"
}
],
"resolution": "720p",
"ratio": "adaptive",
"duration": 4,
"generate_audio": false,
"watermark": false,
"return_last_frame": true
}'curl "https://api.example.com/v1/video/generations" \
-H "Authorization: Bearer <YOUR_API_KEY>" \
-H "Content-Type: application/json" \
-d '{
"model": "doubao-seedance-2-0-fast-260128",
"prompt": "参考图片1的人物外观、参考视频1的镜头运动、参考音频1的节奏,生成一段电影感短视频",
"content": [
{
"type": "text",
"text": "参考图片1的人物外观、参考视频1的镜头运动、参考音频1的节奏,生成一段电影感短视频"
},
{
"type": "image_url",
"image_url": {
"url": "https://example.com/reference-image.jpg"
},
"role": "reference_image"
},
{
"type": "video_url",
"video_url": {
"url": "https://example.com/reference-video.mp4"
},
"role": "reference_video"
},
{
"type": "audio_url",
"audio_url": {
"url": "https://example.com/reference-audio.mp3"
},
"role": "reference_audio"
}
],
"resolution": "720p",
"ratio": "16:9",
"duration": 4,
"generate_audio": true,
"watermark": false
}'curl "https://api.example.com/v1/video/generations/<task_id>" \
-H "Authorization: Bearer <YOUR_API_KEY>"提交任务返回 task_id,状态为 queued:
{
"id": "task_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"task_id": "task_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"status": "queued"
}轮询:生成中为 IN_PROGRESS,完成为 SUCCESS,取 data.result_url 下载(链接有效期约 7 天):
{
"data": {
"status": "SUCCESS",
"progress": "100%",
"result_url": "https://.../task.mp4?sign=..."
}
}| 参数 | 类型 | 说明 |
|---|---|---|
model | string | 模型名,见上表 |
prompt | string | 提示词;KJAPI 适配层要求保留顶层 prompt,即使同时使用 content |
content | array | 多模态输入数组。需要参考图片、视频、音频或首尾帧时使用 |
resolution | string | 480p / 720p / 1080p;快速版通常建议用 720p |
ratio | string | 16:9 / 9:16 / 1:1 / 4:3 / 3:4 / 21:9 / adaptive |
duration | int | 时长(秒)。当前链路如需严格生效,建议同时在 prompt 末尾追加 --dur N |
generate_audio | bool | 是否生成或使用音频;使用参考音频时通常设为 true |
watermark | bool | 是否添加水印 |
return_last_frame | bool | 是否在结果中返回最后一帧图片 URL |
Seedance content 素材项写法
| 用途 | type | URL 字段 | role |
|---|---|---|---|
| 提示词 | text | text | 不需要 |
| 首帧图片 | image_url | image_url.url | first_frame |
| 尾帧图片 | image_url | image_url.url | last_frame |
| 参考图片 | image_url | image_url.url | reference_image |
| 参考视频 | video_url | video_url.url | reference_video |
| 参考音频 | audio_url | audio_url.url | reference_audio |
- 图片、视频、音频都必须使用公网可访问的文件直链,不支持本地路径,不要使用需要登录、Cookie 或内网访问的地址。
- 参考素材在提示词中可用“图片1 / 视频1 / 音频1”等方式指代,顺序对应
content数组里的素材顺序。KJAPI 适配层要求顶层prompt,建议与content中的 text 保持一致。 - 只使用首尾帧时,建议标准版
doubao-seedance-2-0-260128;普通参考素材追求速度时可用快速版。 - 如需指定时长,建议在顶层
duration外,再在提示词末尾追加--dur N,例如--dur 15。
语音合成 TTS · /v1/audio/speech
以下模型统一使用该接口:qwen3-tts-flash、MiniMax/speech-2.8-turbo、MiniMax/speech-2.8-hd。返回音频二进制,需按文件流保存。
curl "https://api.example.com/v1/audio/speech" \
-H "Authorization: Bearer <YOUR_API_KEY>" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-tts-flash",
"input": "你好,这是一个测试。",
"voice": "Cherry"
}' \
--output speech.wavimport requests
resp = requests.post(
"https://api.example.com/v1/audio/speech",
headers={
"Authorization": "Bearer <YOUR_API_KEY>",
"Content-Type": "application/json",
},
json={
"model": "qwen3-tts-flash",
"input": "你好,这是一个测试。",
"voice": "Cherry",
},
timeout=180,
)
resp.raise_for_status()
with open("speech.wav", "wb") as f:
f.write(resp.content)Cherrymale-qn-qingsemale-qn-qingse- 可选参数
response_format:指定音频格式,例如mp3。 - 可选参数
speed:调整语速。
Claude thinking 使用说明
如需 Claude thinking,可在 Anthropic 接口里传入 thinking 参数。
{
"model": "claude-sonnet-4-6",
"max_tokens": 4096,
"thinking": {
"type": "enabled",
"budget_tokens": 8192
},
"messages": [
{
"role": "user",
"content": "证明根号 2 是无理数,并写出完整步骤"
}
]
}claude-sonnet-4-6可返回 thinking / signature。claude-opus-4-7如需使用 thinking,建议先单独验证业务请求。- 如果只是普通聊天,不需要传
thinking。
模型接入方式汇总
| 模型类型 | 接入方式 |
|---|---|
| Claude | Anthropic /v1/messages |
| GPT / DeepSeek / GLM / Kimi / Qwen 文本 | OpenAI /v1/chat/completions |
| Gemini 文本 | Gemini 原生 POST /v1beta/models/{model}:generateContent |
| Gemini 图像相关模型 | Gemini 原生 POST /v1beta/models/{model}:generateContent |
| 图片生成 / 编辑 | /v1/images/generations / /v1/images/edits |
| 视频生成 | /v1/videos |
| TTS | /v1/audio/speech |
接入注意事项
GET /v1/models返回的是当前 Key 可见的模型集合,接入前建议先拉取一次。- 接入时请直接采用本文档对应模型的接口与请求格式。
- Gemini 系列使用本文档中的原生接口与请求格式。
- 视频接口为异步任务接口,不能按普通文本接口理解。
- TTS 返回的是音频二进制内容,客户端要按文件流保存。