> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gravitex.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# GPT-Image-2 · 文生图

> GPT-Image-2 Flare / Sunburst · POST /v1/images/generations

## 简介

GPT-Image-2 是 OpenAI 的图像模型，通过 `POST /v1/images/generations` 将文本描述转为图片，适用于插画、商品素材、海报和信息图。支持灵活尺寸以及 PNG、JPEG、WebP 输出。使用 quality、size 和 background 调整生成结果；透明背景属于该模型的预览能力，需确认当前项目和渠道是否支持。

## 认证

**Authorization** `string` *required*

Bearer \$GRAVITEX\_API\_KEY

**Content-Type** `string` *required*

application/json

## 请求参数

| 参数 | 类型 | 说明 |
| - | - | - |
| `model` | string | 必填，gpt-image-2 |
| `prompt` | string | 必填，描述要生成的图片内容，最长 32,000 字符 |
| `quality` | string | auto / low / medium / high；默认 auto |
| `size` | string | auto 或 WxH，宽高为 16 的倍数；受模型尺寸和总像素预算约束 |
| `n` | integer | 1–10 |
| `background` | string | auto / opaque / transparent；透明栅格输出使用 png 或 webp |
| `output_format` | string | png / jpeg / webp；默认 png |
| `output_compression` | integer | 0–100，默认 100，仅用于 JPEG / WebP |
| `moderation` | string | auto / low；默认 auto |
| `safety_identifier` | string | 稳定的终端用户标识，建议哈希化，最长 128 字符；替代 user |
| `stream` | boolean | 默认 false；开启后返回 SSE 事件，不能按普通 JSON 响应解析 |
| `partial_images` | integer | 仅栅格流式输出，0–3，默认 0；表示期望的中间预览数，实际可能更少 |

## 请求示例

示例从环境变量 `GRAVITEX_API_KEY` 读取平台令牌，请先在当前终端设置该变量。文件上传使用 curl 的 `-F` 自动生成 Content-Type 和 boundary，不要手动把 Content-Type 固定成 multipart/form-data。

### 基础生成

```bash theme={null}
curl https://api.gravitex.ai/v1/images/generations \
  -H "Authorization: Bearer $GRAVITEX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "gpt-image-2",
  "prompt": "A clean botanical poster with precise typography",
  "quality": "medium",
  "size": "1024x1024"
}'
```

### 透明背景

```bash theme={null}
curl https://api.gravitex.ai/v1/images/generations \
  -H "Authorization: Bearer $GRAVITEX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "gpt-image-2",
  "prompt": "A single green botanical icon, isolated on a transparent background",
  "quality": "medium",
  "size": "1024x1024",
  "background": "transparent",
  "output_format": "png"
}'
```

`user` 为兼容旧请求保留，已废弃；新请求使用 safety\_identifier。非空 safety\_identifier 优先，未提供、null 或空字符串时才回退 user。

### 流式输出

```bash theme={null}
curl --no-buffer https://api.gravitex.ai/v1/images/generations \
  -H "Authorization: Bearer $GRAVITEX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "gpt-image-2",
  "prompt": "A simple green botanical illustration",
  "quality": "medium",
  "size": "1024x1024",
  "stream": true,
  "partial_images": 1
}'
```

## 限制与注意事项

auto 为默认值。显式尺寸使用 WxH，宽高均为 16 的倍数，宽高比在 1:3–3:1 之间。image-2 官方指南规定最长边不超过 3840px、总像素为 655,360–8,294,400。

quality 使用 auto / low / medium / high，默认 auto。透明背景是 image-2 的预览能力；可用时使用 background=transparent，且输出格式选 PNG 或 WebP。仅介绍栅格生成与编辑，不将 2.6 的 complexity、GIF、SVG 或 2.5 的 markup 能力直接套用。

## 响应示例

下面是非流式响应的结构示意，图片数据和 token 数仅为示例，并非实际生成结果。将 b64\_json 解码为对应格式的文件；不要将 Base64 字符串直接保存为图片。

```json theme={null}
{
  "created": 1794268800,
  "data": [
    {
      "b64_json": "<BASE64_IMAGE_DATA>"
    }
  ],
  "usage": {
    "input_tokens": 100,
    "output_tokens": 824,
    "total_tokens": 924
  }
}
```

若设置 `stream: true`，响应为 SSE 事件流；按事件读取中间预览和最终图片，不能对整个响应直接调用 JSON 解析。

## 响应与计费

本页示例为非流式调用时，图片通过 b64\_json 返回；不要传 response\_format=url 期待图片链接。输入图片和输出图片均有 token 用量；相同 token 单价不代表相同单张费用，需结合实际 usage。

返回 `data[].b64_json` 和 `usage`。图片数据须先 Base64 解码再保存。以下为 OpenAI 官方标准价，不是 GravitexAI 报价。

缓存价格是官方参考费率。官方指南说明，GPT Image 2 / 2.5 的缓存输入优惠仅适用于 Responses API 图像工具，不适用于直接调用本页 Images API（含 edits）；平台实际结算以控制台为准。

| 类型 | 美元 / 1M tokens |
| - | - |
| 文本输入 | \$5.00 |
| 缓存文本输入 | \$1.25 |
| 图片输入 | \$8.00 |
| 缓存图片输入 | \$2.00 |
| 图片输出 | \$30.00 |

## 官方参考

* [Image generation guide](https://developers.openai.com/api/docs/guides/image-generation)
* [API Reference](https://developers.openai.com/api/reference/resources/images/methods/generate)
* [GPT-Image-2](https://developers.openai.com/api/docs/models/gpt-image-2)
* [Pricing](https://developers.openai.com/api/docs/pricing)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.