生成图像

用任意图像模型,根据提示词生成图像。

POST

发送提示词和图像模型,以 base64 形式拿回图像。从 GET /images/models 中选择模型和它的字段:每个模型接受的字段各不相同,模型没有列出的字段会在预留任何费用之前被拒绝。

调用会按最贵服务商的价格,为请求的每张图像预留费用。设置 resolution 和 quality 可以减少预留。费用、参考图和流式输出见图像。

用 Authorization: Bearer binf_... 或 x-api-key 发送你的密钥。

请求体

这里列出的是大多数调用会用到的字段。OpenAI 格式的其他字段都会按原样转发。

  • modelstring必填

    来自 GET /images/models 的图像模型 ID,比如 openai/gpt-image-2。

  • promptstring必填

    要画的内容。

  • ninteger

    生成多少张图像,默认 1 张。每个模型有自己的上限,最多 10 张:见 GET /images/models 中的 parameters.n。调用会为每张图像预留费用。

  • resolution"512" | "1K" | "2K" | "4K"

    尺寸档位,适用于列出了该字段的模型。不传时,调用按模型提供的最大档位预留。

  • aspect_ratiostring

    比如 "1:1" 或 "16:9",取值来自模型列出的值。

  • qualitystring

    low、medium、high 及更高,适用于列出了该字段的模型。质量越低,预留和费用越少。不传时,调用按模型提供的最高质量预留。

  • sizestring

    档位,比如 "2K";或像素,比如 "2048x2048"。

  • output_formatstring

    png、jpeg、webp 或 svg,适用于提供多种格式的模型。

  • backgroundstring

    transparent 或 opaque,适用于列出了该字段的模型。

  • input_referencesobject[]

    要编辑或参考的图像,格式为 { "type": "image_url", "image_url": { "url": "https://..." } },数量不超过模型的上限。最好使用链接:请求体最大 4 MB。

  • streamboolean

    以服务器发送事件的形式,先发送生成中的部分图像,再发送完成的图像。每次调用只能生成一张图像,且仅限 streaming: true 的模型。其他模型照常返回完整图像。

  • seedinteger

    在列出了该字段的模型上,相同的 seed 和提示词会生成相同的图像。

  • response_format"b64_json" | "url"

    url 会保存图像,并为每张图像返回一个有效期 7 天的链接,而不是 base64:无论图像多大,返回内容都很小。不能与 stream 同时使用。

返回

  • createdinteger

    生成时间,以 Unix 秒表示。

  • dataobject[]

    图像,按顺序排列。

    • b64_jsonstring

      以 base64 表示的图像。使用 response_format: "url" 时不返回。

    • urlstring

      图像的链接,使用 response_format: "url" 时返回。

    • expires_atstring

      链接失效的时间,使用 url 时返回。

    • media_typestring

      图像类型,比如 image/png。

  • usageobject

    Token 数和 cost:本次调用的扣费金额,单位为美元。正是从余额中扣除的金额。

响应头

  • x-binference-call-idstring

    这次调用在你调用记录页面上的 ID。联系我们时请提供它。

  • x-generation-idstring

    模型服务商为这次回答生成的 ID。

  • retry-afterinteger

    出现 429 或 503 时:重新发送前需要等待的整秒数。

  • retry-after-msinteger

    同样的等待时间,以毫秒计。OpenAI 和 Anthropic SDK 会读取它。

请求
curl https://binference.io/api/v1/images \  -H "Authorization: Bearer $BINF_API_KEY" \  -H "Content-Type: application/json" \  -d '{    "model": "openai/gpt-image-2",    "prompt": "A small red lighthouse on a rock at dusk, flat illustration",    "quality": "medium",    "aspect_ratio": "1:1"  }'
返回
{  "created": 1790809771,  "data": [    {      "b64_json": "iVBORw0KGgoAAAANSUhEUgAAB...",      "media_type": "image/png"    }  ],  "usage": {    "prompt_tokens": 15,    "completion_tokens": 1584,    "total_tokens": 1599,    "cost": 0.057114  }}
流式输出(stream: true)
data: {"type":"image_generation.partial_image","partial_image_index":0,"b64_json":"iVBORw0KGgo..."}data: {"type":"image_generation.completed","b64_json":"iVBORw0KGgo...","media_type":"image/png","created":1790809864,"usage":{"prompt_tokens":15,"completion_tokens":1584,"total_tokens":1599,"cost":0.057114}}data: [DONE]