通过 TokenHub 统一 API 使用 OpenAI 的最新图像生成 model。完全兼容 OpenAI,支持同步返回结果(无需轮询)、最高 4K 的多种尺寸、质量等级以及 PNG/JPEG/WebP 输出格式。支持文本生成图像(t2i)和图像编辑(i2i、多图合成与 inpainting)。本指南将带你完成完整集成。
th-xxxxxxxxxxxx...)https://tokenhub.store/api/v1所有请求都需要在请求头中携带 API Key:
Authorization: Bearer th-your-api-keyPOST/images/generationsText-to-image。根据文本提示生成图像。
POST/images/editsImage-to-image 编辑。支持单图编辑、多图合成,以及可选的带 mask 的 inpainting。
| 档位 | 价格 | 分辨率范围 |
|---|---|---|
| 1K | $0.125 / image | 最长边 ≤ 1536 px(例如 1024×1024、1024×1536) |
| 2K | $0.250 / image | 最长边 ≤ 2048 px(例如 2048×2048) |
| 4K | $0.500 / image | 最长边 > 2048 px(例如 4096×4096) |
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| model | string | 必填 | — | Model ID。使用 "openai/gpt-image-2"(或简写 "gpt-image-2")。 |
| prompt | string | 必填 | — | 描述要生成图像的文本提示词。 |
| n | integer | 可选 | 1 | 要生成的图像数量(1–10)。 |
| size | string | 可选 | 1024x1024 | 输出尺寸。可选:1024x1024、1536x1024、1024x1536、2048x2048、2048x1152、3840x2160、2160x3840、auto。 |
| quality | string | 可选 | auto | 生成质量。可选:auto、low、medium、high。更高质量会消耗更多 Tokens。 |
| format | string | 可选 | png | 输出图像格式。可选:png、jpeg、webp。 |
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| image | file / string | array | 必填 | — | 要编辑的输入图像。支持通过 multipart 传入文件,或在 JSON 中使用 URL / base64 data URI。可提供多张图像以合成为一张输出。 |
| mask | file / string | 可选 | — | 可选的局部重绘遮罩。透明区域会被重新生成;不透明区域会保留。 |
| input_fidelity | string | 可选 | low | 保留输入内容的程度。可选:low、high。使用 high 以保留身份/布局;使用 low 可实现更大胆的变换。 |
| output_format | string | 可选 | png | 编辑接口中 'format' 的别名。可选:png、jpeg、webp。 |
| output_compression | integer | 可选 | 100 | 压缩级别 0–100(仅适用于 jpeg / webp)。 |
curl -X POST https://tokenhub.store/api/v1/images/generations \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-image-2",
"prompt": "A serene Japanese garden at sunset, koi pond reflections, cherry blossoms, soft cinematic lighting, ultra-detailed"
}'响应:
{
"created": 1740000000,
"data": [
{
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
}
]
}# pip install openai
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://tokenhub.store/api/v1",
)
result = client.images.generate(
model="openai/gpt-image-2",
prompt="A majestic dragon flying over snowy mountains at dawn, epic fantasy art, ultra-detailed",
n=1,
size="1536x1024",
quality="high",
)
image_url = result.data[0].url
print("Image URL:", image_url[:80], "...")// npm install openai
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "YOUR_API_KEY",
baseURL: "https://tokenhub.store/api/v1",
});
const result = await client.images.generate({
model: "openai/gpt-image-2",
prompt: "A majestic dragon flying over snowy mountains at dawn, epic fantasy art, ultra-detailed",
n: 1,
size: "1536x1024",
quality: "high",
});
console.log("Image URL:", result.data[0].url.slice(0, 80), "...");© 2026 TokenHub · support@tokenhub.store