什么是 KoCodeImg?
KoCodeImg 是一个 AI 图片生成平台,提供 OpenAI / Anthropic / Google 等多平台兼容的统一 API 接口。 你只需一个 API Key,即可通过标准格式调用不同平台的图片生成模型。
统一入口: 一个 API Key 接入所有平台
兼容格式: 完全兼容 OpenAI / Google / Anthropic SDK
快速开始
获取 API Key
在 账号设置 页面创建 API Key,格式为 sk-koci_xxxx
选择平台与模型
根据需求选择 OpenAI / Anthropic / Google 兼容接口,使用对应 SDK 或 HTTP 调用
发送请求
将 SDK 的 baseURL 指向本平台,传入 API Key 即可
认证方式
支持以下三种认证方式:
Authorization: Bearer <你的 API Key>(请求头)X-API-Key: <你的 API Key>(请求头)?key=<你的 API Key>(URL 参数)
安全提示: 请勿在客户端代码或公开仓库中暴露 API Key。如果泄露,请立即在设置中删除并重新生成。
OpenAI 图片生成接口
OpenAI 官方文档: https://platform.openai.com/docs/api-reference/images
POST /{platform}/v1/images/generations请求参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| model | string | 是 | 模型 ID,如 image-2、image-2-hd |
| prompt | string | 是 | 图片描述文本,最大 4000 字符 |
| n | integer | 否 | 生成数量,默认 1,当前仅支持 1 |
| size | string | 否 | 图片尺寸: 1024x1024、1792x1024、1024x1792 |
| quality | string | 否 | 质量: standard 或 hd |
请求示例:
curl -X POST https://your-domain.com/openai/v1/images/generations \
-H "Authorization: Bearer sk-koci_your_api_key" \
-H "Content-Type: application/json" \
-d '{
"model": "image-2",
"prompt": "一只穿着宇航服的猫在月球上漫步",
"n": 1,
"size": "1024x1024",
"quality": "hd"
}'Node.js (OpenAI SDK):
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "sk-koci_your_api_key",
baseURL: "https://your-domain.com/openai/v1",
});
const response = await client.images.generate({
model: "image-2",
prompt: "一只穿着宇航服的猫在月球上漫步",
n: 1,
size: "1024x1024",
quality: "hd",
});
console.log(response.data[0].url);Python (OpenAI SDK):
from openai import OpenAI
client = OpenAI(
api_key="sk-koci_your_api_key",
base_url="https://your-domain.com/openai/v1",
)
response = client.images.generate(
model="image-2",
prompt="一只穿着宇航服的猫在月球上漫步",
n=1,
size="1024x1024",
quality="hd",
)
print(response.data[0].url)响应示例:
{
"created": 1716700000,
"data": [
{
"url": "https://your-domain.com/storage/images/xxx.png",
"revised_prompt": "一只穿着宇航服的猫在月球上漫步..."
}
]
}OpenAI 图生图接口
基于参考图片生成新图片,支持风格迁移、局部修改等场景。兼容 OpenAI Images Edit API 格式。
POST /{platform}/v1/images/edits请求参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| image | file | 是 | 参考图片文件 (PNG/JPG, 最大 4MB) |
| prompt | string | 是 | 描述期望的修改或生成效果 |
| model | string | 否 | 模型 ID,如 image-2、image-2-hd |
| n | integer | 否 | 生成数量,默认 1 |
| size | string | 否 | 输出尺寸: 1024x1024、1792x1024、1024x1792 |
| mask | file | 否 | 遮罩图片,透明区域为需要编辑的部分 (PNG) |
cURL (multipart/form-data):
curl -X POST https://your-domain.com/openai/v1/images/edits \
-H "Authorization: Bearer sk-koci_your_api_key" \
-F "image=@reference.png" \
-F "prompt=将背景改为星空,保留主体人物" \
-F "model=image-2-hd" \
-F "size=1024x1024"Node.js (OpenAI SDK):
import OpenAI from "openai";
import fs from "fs";
const client = new OpenAI({
apiKey: "sk-koci_your_api_key",
baseURL: "https://your-domain.com/openai/v1",
});
const response = await client.images.edit({
model: "image-2-hd",
image: fs.createReadStream("reference.png"),
prompt: "将背景改为星空,保留主体人物",
size: "1024x1024",
});
console.log(response.data[0].url);Python (OpenAI SDK):
from openai import OpenAI
client = OpenAI(
api_key="sk-koci_your_api_key",
base_url="https://your-domain.com/openai/v1",
)
response = client.images.edit(
model="image-2-hd",
image=open("reference.png", "rb"),
prompt="将背景改为星空,保留主体人物",
size="1024x1024",
)
print(response.data[0].url)带遮罩局部编辑:
response = client.images.edit(
model="image-2-hd",
image=open("photo.png", "rb"),
mask=open("mask.png", "rb"), # 透明区域 = 需要重绘的部分
prompt="在空白区域添加一只金色的猫",
size="1024x1024",
)
print(response.data[0].url)响应示例:
{
"created": 1716700000,
"data": [
{
"url": "https://your-domain.com/storage/images/edited_xxx.png",
"revised_prompt": "将背景改为星空,保留主体人物..."
}
]
}提示: 支持基于参考图的风格迁移与局部编辑(带遮罩时透明区域为重绘部分)。
OpenAI 可用模型
| 模型名称 | 模型 ID | 说明 |
|---|---|---|
| 标准 | image-2 | 快速生成,适合日常使用 |
| 高清 | image-2-hd | 高清画质,细节更丰富 |
| 超清 | image-2-ultra | 最高画质,适合商业用途 |
Anthropic 图片生成接口
Anthropic 官方文档: https://docs.anthropic.com
POST /{platform}/v1/messages请求参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| model | string | 是 | 模型 ID,如 claude-sonnet-4-20250514 |
| max_tokens | integer | 是 | 最大输出 token 数 |
| messages | array | 是 | 对话消息列表,包含 role 和 content |
Python (Anthropic SDK):
import anthropic
client = anthropic.Anthropic(
api_key="sk-koci_your_api_key",
base_url="https://your-domain.com/anthropic/v1",
)
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[
{"role": "user", "content": "生成一张日落海滩的图片"}
],
)
print(message.content)Google 图片生成接口
Gemini 官方文档: https://ai.google.dev/gemini-api/docs
POST /{platform}/v1beta/models/{model}:generateContent请求参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| contents | array | 是 | 对话内容列表,包含角色和部件 |
| generationConfig | object | 否 | 生成配置,包含 responseModalities 等 |
Python (Google GenAI SDK):
from google import genai
client = genai.Client(
api_key="sk-koci_your_api_key",
http_options={"base_url": "https://your-domain.com/google/v1beta"},
)
response = client.models.generate_content(
model="gemini-2.0-flash-exp",
contents="生成一张赛博朋克城市的图片",
config=genai.types.GenerateContentConfig(
response_modalities=["TEXT", "IMAGE"],
),
)
for part in response.candidates[0].content.parts:
if part.inline_data:
with open("output.png", "wb") as f:
f.write(part.inline_data.data)cURL:
curl -X POST "https://your-domain.com/google/v1beta/models/gemini-2.0-flash-exp:generateContent" \
-H "Authorization: Bearer sk-koci_your_api_key" \
-H "Content-Type: application/json" \
-d '{
"contents": [{
"parts": [{"text": "生成一张赛博朋克城市的图片"}]
}],
"generationConfig": {
"responseModalities": ["TEXT", "IMAGE"]
}
}'Antigravity 图片生成接口
基于 Google Gemini generateContent API,支持 1K / 2K / 4K 多分辨率和 5 种宽高比。
POST /{platform}/v1beta/models/{model}:generateContent请求参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| contents | array | 是 | 对话内容,包含 text 部件 (文生图) 或 inlineData + text (图生图) |
| generationConfig.responseModalities | array | 是 | 固定为 ["IMAGE", "TEXT"] |
| generationConfig.imageConfig.imageSize | string | 否 | 分辨率档位: 1K (默认)、2K、4K |
| generationConfig.imageConfig.aspectRatio | string | 否 | 宽高比: 1:1 (默认)、16:9、9:16、4:3、3:4 |
cURL:
curl -X POST "https://your-domain.com/antigravity/v1beta/models/gemini-3.1-flash-image:generateContent" \
-H "Authorization: Bearer sk-koci_your_api_key" \
-H "Content-Type: application/json" \
-d '{
"contents": [{
"role": "user",
"parts": [{"text": "一只穿着宇航服的猫在月球上漫步"}]
}],
"generationConfig": {
"responseModalities": ["IMAGE", "TEXT"],
"imageConfig": {
"imageSize": "2K",
"aspectRatio": "16:9"
}
}
}'Python:
import httpx, base64
resp = httpx.post(
"https://your-domain.com/antigravity/v1beta/models/gemini-3.1-flash-image:generateContent",
headers={
"Authorization": "Bearer sk-koci_your_api_key",
"Content-Type": "application/json",
},
json={
"contents": [{
"role": "user",
"parts": [{"text": "一只穿着宇航服的猫在月球上漫步"}],
}],
"generationConfig": {
"responseModalities": ["IMAGE", "TEXT"],
"imageConfig": {"imageSize": "2K", "aspectRatio": "16:9"},
},
},
timeout=600,
)
data = resp.json()
for part in data["candidates"][0]["content"]["parts"]:
if "inlineData" in part:
img = base64.b64decode(part["inlineData"]["data"])
with open("output.jpg", "wb") as f:
f.write(img)
print(f"saved {len(img)} bytes")
elif "text" in part:
print(part["text"])Node.js:
const resp = await fetch(
"https://your-domain.com/antigravity/v1beta/models/gemini-3.1-flash-image:generateContent",
{
method: "POST",
headers: {
"Authorization": "Bearer sk-koci_your_api_key",
"Content-Type": "application/json",
},
body: JSON.stringify({
contents: [{
role: "user",
parts: [{ text: "一只穿着宇航服的猫在月球上漫步" }],
}],
generationConfig: {
responseModalities: ["IMAGE", "TEXT"],
imageConfig: { imageSize: "2K", aspectRatio: "16:9" },
},
}),
}
);
const data = await resp.json();
for (const part of data.candidates[0].content.parts) {
if (part.inlineData) {
const buf = Buffer.from(part.inlineData.data, "base64");
require("fs").writeFileSync("output.jpg", buf);
console.log(`saved ${buf.length} bytes`);
}
}响应示例:
{
"candidates": [{
"content": {
"parts": [
{
"inlineData": {
"mimeType": "image/jpeg",
"data": "/9j/4AAQ... (base64)"
}
},
{
"text": "图片描述文本"
}
]
}
}]
}注意: 4K 分辨率图片较大,请求可能需要较长时间 (最多 10 分钟),请设置足够的超时时间。图片以 base64 返回在响应体中,非 URL。
Antigravity 图生图接口
在 contents.parts 中传入参考图的 base64 数据,配合文本提示词实现图生图。
请求体结构:
{
"contents": [{
"role": "user",
"parts": [
{
"inlineData": {
"mimeType": "image/png",
"data": "<参考图 base64>"
}
},
{
"text": "将画面风格转为赛博朋克"
}
]
}],
"generationConfig": {
"responseModalities": ["IMAGE", "TEXT"],
"imageConfig": {
"imageSize": "2K",
"aspectRatio": "1:1"
}
}
}Python 图生图:
import httpx, base64
# 读取参考图并编码
with open("reference.png", "rb") as f:
ref_b64 = base64.b64encode(f.read()).decode()
resp = httpx.post(
"https://your-domain.com/antigravity/v1beta/models/gemini-3.1-flash-image:generateContent",
headers={
"Authorization": "Bearer sk-koci_your_api_key",
"Content-Type": "application/json",
},
json={
"contents": [{
"role": "user",
"parts": [
{"inlineData": {"mimeType": "image/png", "data": ref_b64}},
{"text": "将画面风格转为赛博朋克,保留构图"},
],
}],
"generationConfig": {
"responseModalities": ["IMAGE", "TEXT"],
"imageConfig": {"imageSize": "2K"},
},
},
timeout=600,
)
data = resp.json()
for part in data["candidates"][0]["content"]["parts"]:
if "inlineData" in part:
img = base64.b64decode(part["inlineData"]["data"])
with open("output.jpg", "wb") as f:
f.write(img)提示: 支持多张参考图,在 parts 数组中依次添加多个 inlineData 部件,最后一个 part 为文本提示词。
可用模型与参数
模型列表:
| 模型名称 | 模型 ID | 说明 |
|---|---|---|
| Gemini Flash | gemini-3.1-flash-image | 速度快,适合日常生成 |
| Gemini Pro | gemini-3-pro-image | 质量高,细节更丰富 |
分辨率档位 (imageSize):
| 档位 | 值 | 说明 |
|---|---|---|
| 标准 | 1K | 默认分辨率,速度最快 |
| 高清 | 2K | 2 倍分辨率,适合打印 |
| 超清 | 4K | 4 倍分辨率,文件较大,耗时较长 |
宽高比 (aspectRatio):
| 比例 | 适用场景 |
|---|---|
| 1:1 | 头像、图标、社交媒体封面 (默认) |
| 16:9 | 横屏壁纸、视频封面、Banner |
| 9:16 | 手机壁纸、竖屏海报、Story |
| 4:3 | 传统照片、PPT 插图 |
| 3:4 | 竖版照片、书籍封面 |
SSE 流式生成
原生接口返回 Server-Sent Events 流,支持实时进度回传。适合需要展示生成进度的场景。
POST https://your-domain.com/api/generate/cURL:
curl -X POST https://your-domain.com/api/generate/ \
-H "Authorization: Bearer sk-koci_your_api_key" \
-H "Content-Type: application/json" \
-d '{
"conversation_id": "your-conversation-id",
"prompt": "一只穿着宇航服的猫在月球上漫步",
"style": "realistic",
"size": "1:1",
"model": "standard"
}'SSE 事件流:
event: status
data: {"status": "queued", "message_id": "550e8400-..."}
event: progress
data: {"status": "processing", "progress": 30}
event: progress
data: {"status": "processing", "progress": 70}
event: complete
data: {"status": "complete", "image_url": "https://...", "thumbnail_url": "https://..."}Python (SSE Client):
import requests
import sseclient
resp = requests.post(
"https://your-domain.com/api/generate/",
headers={
"Authorization": "Bearer sk-koci_your_api_key",
"Content-Type": "application/json",
"Accept": "text/event-stream",
},
json={
"conversation_id": "your-conversation-id",
"prompt": "一只穿着宇航服的猫在月球上漫步",
"style": "realistic",
"size": "1:1",
"model": "standard",
},
stream=True,
)
client = sseclient.SSEClient(resp)
for event in client.events():
print(f"[{event.event}] {event.data}")图生图
原生图生图接口,上传参考图并描述修改意图,支持 SSE 进度流。
POST https://your-domain.com/api/generate/img2img请求参数 (multipart/form-data):
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| image | file | 是 | 参考图片 (PNG/JPG, 最大 4MB) |
| prompt | string | 是 | 描述修改意图 |
| conversation_id | string | 是 | 对话 ID |
| strength | number | 否 | 变化强度 0.0-1.0,默认 0.7 (越大越偏离原图) |
| style | string | 否 | 风格: realistic, anime, watercolor 等 |
| size | string | 否 | 输出比例: 1:1, 16:9, 9:16 |
| model | string | 否 | 模型: standard, hd, ultra |
| mask | file | 否 | 遮罩图 (PNG),透明区域为编辑区 |
cURL:
curl -X POST https://your-domain.com/api/generate/img2img \
-H "Authorization: Bearer sk-koci_your_api_key" \
-F "image=@reference.png" \
-F "prompt=将画面风格转为赛博朋克" \
-F "conversation_id=your-conversation-id" \
-F "strength=0.75" \
-F "style=cyberpunk" \
-F "model=hd"Python:
import requests
import sseclient
resp = requests.post(
"https://your-domain.com/api/generate/img2img",
headers={
"Authorization": "Bearer sk-koci_your_api_key",
"Accept": "text/event-stream",
},
files={"image": open("reference.png", "rb")},
data={
"prompt": "将画面风格转为赛博朋克",
"conversation_id": "your-conversation-id",
"strength": "0.75",
"style": "cyberpunk",
"model": "hd",
},
stream=True,
)
client = sseclient.SSEClient(resp)
for event in client.events():
print(f"[{event.event}] {event.data}")SSE 事件流 (与文生图相同):
event: status
data: {"status": "queued", "message_id": "550e8400-..."}
event: progress
data: {"status": "processing", "progress": 40}
event: complete
data: {"status": "complete", "image_url": "https://...", "thumbnail_url": "https://..."}请求参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| conversation_id | string | 是 | 对话 ID,通过 POST /api/conversations/ 创建 |
| prompt | string | 是 | 图片描述文本 |
| style | string | 否 | 风格: realistic, anime, watercolor, oil-painting 等 |
| size | string | 否 | 比例: 1:1, 16:9, 9:16 |
| model | string | 否 | 模型: standard、hd、ultra |
视频生成接口
视频生成为异步接口:提交任务后立即返回 task_id,再通过 SSE 实时获取进度,完成后从事件中取得视频地址。请在请求头携带登录后获取的访问令牌。
提交任务:
POST https://your-domain.com/api/videos请求参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| model | string | 是 | 视频模型名,如 veo-3-1、veo-omni-flash |
| prompt | string | 是 | 视频画面描述 |
| mode | string | 否 | text2video(文生) / image2video(图生) / video_edit(视频编辑) |
| size | string | 否 | 画面比例: 16:9、9:16、1:1 |
| reference_images | string[] | 否 | 参考图/首尾帧公网 URL(图生视频,最多 9 张) |
| input_video_url | string | 否 | 输入视频 URL(视频编辑模式) |
cURL:
curl -X POST https://your-domain.com/api/videos \
-H "Authorization: Bearer <访问令牌>" \
-H "Content-Type: application/json" \
-d '{"model": "veo-3-1", "prompt": "日出时分宁静的山间湖泊,电影感航拍", "mode": "text2video", "size": "16:9"}'提交响应:
{
"status": "queued",
"task_id": "v_xxxxxxxx",
"video_id": "v_xxxxxxxx"
}轮询进度 (SSE):
GET https://your-domain.com/api/videos/{task_id}/streamSSE 事件流:
data: {"status": "processing", "progress": 30}
data: {"status": "complete", "progress": 100, "video_url": "https://.../video.mp4", "video_id": "v_xxxxxxxx"}视频可用模型
| 模型 | 能力 | 时长 |
|---|---|---|
| veo-3-1 | 文生 / 图生(首尾帧、多图参考≤9) | 8 秒 |
| veo-omni-flash | 文生 / 图生(首尾帧、多图参考≤9) | 10 秒 |
图生视频需提供公网可访问的参考图 URL;视频时长由模型固定。生成完成后视频会转存至平台存储,长期有效。
错误码
| 状态码 | 含义 | 处理建议 |
|---|---|---|
| 401 | API Key 无效或已过期 | 检查 Key 是否正确,是否已被删除 |
| 402 | 积分不足 | 前往平台充值或签到获取积分 |
| 429 | 请求过于频繁 | 降低请求频率,参考速率限制 |
| 500 | 服务内部错误 | 稍后重试,如持续出现请联系支持 |