BeansAI
模型定价文档
登录注册
模型定价文档
登录注册

快速接入

  • 概览

客户端

  • Claude Code
  • CC Switch
  • OpenClaw
  • Roo Code
  • OpenCode
  • Codex CLI
  • GPT Image 2
  • Seedance 2.0
  • SkyReels V4
  • Mureka Song
  • Cursor
  • Cherry Studio

参考

  • 原生 API
← 返回文档首页

GPT Image 2

通过 BeansAI 的 OpenAI 兼容 API 调用 gpt-image-2 生成和编辑图片。

概览

使用同一个 BeansAI API Key 和 base URL。纯文本生图调用 POST /images/generations;带参考图编辑调用 POST /images/edits。

大多数图片上游节点支持流式输出。生产接入时请带上 stream: true, 让代理链路持续收到进度事件,不要一直等待一个大响应。

Text to image

curl
curl https://api.beansai.dev/v1/images/generations \
  -N \
  -H "Authorization: Bearer sk-beans-..." \
  -H "Accept: text/event-stream" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-image-2",
    "prompt": "A clean product photo of a green coffee bean mascot on a white background",
    "size": "1024x1024",
    "quality": "high",
    "n": 1,
    "response_format": "b64_json",
    "stream": true
  }'

Reference image edit

参考图用 data URL 放在 images[].image_url。推荐使用 PNG、JPEG 或 WebP。

shell
IMAGE_B64="$(base64 -i reference.png | tr -d '\n')"

curl https://api.beansai.dev/v1/images/edits \
  -N \
  -H "Authorization: Bearer sk-beans-..." \
  -H "Accept: text/event-stream" \
  -H "Content-Type: application/json" \
  -d "{
    \"model\": \"gpt-image-2\",
    \"prompt\": \"Keep the same product, place it on a polished studio desk\",
    \"images\": [{ \"image_url\": \"data:image/png;base64,$IMAGE_B64\" }],
    \"size\": \"1024x1024\",
    \"quality\": \"high\",
    \"response_format\": \"b64_json\",
    \"stream\": true
  }"

Streaming

curl
curl https://api.beansai.dev/v1/images/generations \
  -N \
  -H "Authorization: Bearer sk-beans-..." \
  -H "Accept: text/event-stream" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-image-2",
    "prompt": "A cinematic poster for an AI API gateway called BeansAI",
    "size": "2016x1344",
    "quality": "high",
    "response_format": "b64_json",
    "stream": true
  }'

流式响应是 SSE。上游支持时可以先收到 *.partial_image 预览, 最终读取 *.completed 事件里的 b64_json,解码后即可保存成图片。

Pricing tiers

BeansAI 对 gpt-image-2 按“预设尺寸 + 质量档位”计费,不再使用单一固定单张价格。 请求会被规范到下表中的预设尺寸;不支持的自定义尺寸会直接返回错误。

预设尺寸lowmediumhigh
1024x1024$0.0059$0.0527$0.2107
1024x768$0.0040$0.0361$0.1445
1008x672$0.0035$0.0309$0.1234
1088x608$0.0029$0.0259$0.1035
1440x480$0.0016$0.0155$0.0620
768x1024$0.0040$0.0361$0.1445
672x1008$0.0035$0.0309$0.1234
608x1088$0.0029$0.0259$0.1035
480x1440$0.0016$0.0155$0.0620
2048x2048$0.0119$0.1070$0.4282
2048x1536$0.0074$0.0667$0.2668
2016x1344$0.0062$0.0543$0.2170
2048x1152$0.0047$0.0424$0.1695
2784x928$0.0028$0.0264$0.1056
1536x2048$0.0074$0.0667$0.2668
1344x2016$0.0062$0.0543$0.2170
1152x2048$0.0047$0.0424$0.1695
928x2784$0.0028$0.0264$0.1056
2880x2880$0.0198$0.1779$0.7116
2880x2160$0.0119$0.1066$0.4262
3456x2304$0.0132$0.1148$0.4591
3840x2160$0.0111$0.1001$0.4003
3840x1280$0.0042$0.0398$0.1593
2160x2880$0.0119$0.1066$0.4262
2304x3456$0.0132$0.1148$0.4591
2160x3840$0.0111$0.1001$0.4003
1280x3840$0.0042$0.0398$0.1593

Python

image.py
import base64
import json
import requests

res = requests.post(
    "https://api.beansai.dev/v1/images/generations",
    headers={
        "Authorization": "Bearer sk-beans-...",
        "Accept": "text/event-stream",
    },
    json={
        "model": "gpt-image-2",
        "prompt": "A minimal green coffee bean logo on a white background",
        "size": "1024x1024",
        "quality": "high",
        "response_format": "b64_json",
        "stream": True,
    },
    stream=True,
    timeout=600,
)
res.raise_for_status()

image_b64 = None
for line in res.iter_lines(decode_unicode=True):
    if not line or not line.startswith("data:"):
        continue
    payload = line[5:].strip()
    if payload == "[DONE]":
        break
    event = json.loads(payload)
    if str(event.get("type", "")).endswith(".completed"):
        image_b64 = event.get("b64_json")
        break

if not image_b64:
    raise RuntimeError("No completed image event returned")

with open("beansai-image.png", "wb") as f:
    f.write(base64.b64decode(image_b64))

JavaScript

image.mjs
import fs from "node:fs";

const res = await fetch("https://api.beansai.dev/v1/images/generations", {
  method: "POST",
  headers: {
    Authorization: "Bearer sk-beans-...",
    Accept: "text/event-stream",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "gpt-image-2",
    prompt: "A friendly AI dashboard illustration with green accents",
    size: "1024x1024",
    quality: "high",
    response_format: "b64_json",
    stream: true,
  }),
});

if (!res.ok) throw new Error(await res.text());
if (!res.body) throw new Error("No response body");

const decoder = new TextDecoder();
const reader = res.body.getReader();
let buffer = "";
let image = "";

function processBlock(block) {
  const data = block
    .split(/\r?\n/)
    .filter((line) => line.startsWith("data:"))
    .map((line) => line.slice(5).trimStart())
    .join("\n")
    .trim();
  if (!data || data === "[DONE]") return;
  const event = JSON.parse(data);
  if (String(event.type || "").endsWith(".completed")) {
    image = event.b64_json || "";
  }
}

while (!image) {
  const { done, value } = await reader.read();
  if (done) break;
  buffer += decoder.decode(value, { stream: true });
  const blocks = buffer.split(/\r?\n\r?\n/);
  buffer = blocks.pop() || "";
  for (const block of blocks) processBlock(block);
}

if (!image && buffer.trim()) processBlock(buffer);
if (!image) throw new Error("No completed image event returned");
fs.writeFileSync("beansai-image.png", Buffer.from(image, "base64"));

使用技巧

  • 需要自己保存图片时,使用 response_format: "b64_json"。
  • 常用尺寸是 1024x1024、2048x1152 和 3840x2160。想先调提示词,可以直接用控制台 Playground。
  • 图片接入默认加上 stream: true。它可以减少代理空等超时, 最终图片仍会在 completed SSE 事件里返回。