# 文生图

**这是什么**：用文字描述生成图片。

**什么时候用**：想要 GPT-Image 风格的图时用。

## 接口地址
- POST `/v1/images/generations`
- GET `/v1/images/generations/{id}`

> 本页已按统一 API 改写：生成用 `POST /v1/images/generations`（默认同步，等图片生成完再返回；加 `"async": true` 则立即返回任务，再用 `GET /v1/images/generations/{id}` 轮询），认证用 `Authorization: Bearer <key>`，错误为 OpenAI 格式。通用说明见图片指南。

本页的请求必须带 `"async": true`（或请求头 `Prefer: respond-async`）：立即返回 202 与任务对象（`status: queued`，`Retry-After: 3`），用 `GET /v1/images/generations/{id}` 轮询到 `status` 为 `completed` 或 `failed`。不带 `async` 的 gpt-image 请求走同步的 OpenAI Images，见「同步调用」。

## 请求参数
| 参数 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- |
| model | string | 是 | gpt-image-1 / gpt-image-1.5 / gpt-image-2 / gpt-image-2.5-flare / gpt-image-2.5-sunburst |
| prompt | string | 是 | 生成描述（1–4000 字符） |
| size | string | — | 推荐直接传官方像素（默认 1024x1024）。方形 1024x1024 / 2048x2048(2K)；横版 1536x1024 / 1792x1024 / 1920x1080 / 2048x1152(2K) / 3840x2160(4K)；竖版 1024x1536 / 1024x1792 / 1080x1920 / 2160x3840(4K)。比例别名 1:1 / 2:3 / 3:2 / 16:9 / 9:16 仅兼容旧客户端、不再推荐；非法/缺省回落 1024x1024 |
| quality | string | — | `low` / `medium` / `high` / `auto`，原样传给 GPT-Image 渠道。 |
| async | boolean | 是 | 本页必须为 `true`（或请求头 `Prefer: respond-async`）：立即返回 202 与任务对象，再用 GET 轮询。不带 `async` 的 gpt-image 请求走同步的 OpenAI Images。 |

`model`、`prompt`、`n`、`size`、`aspect_ratio`、`resolution`、`images`、`output_format`、`negative_prompt`、`prompt_extend`、`async` 是统一接口的通用字段，服务端按渠道换算成原生字段；表中其余字段是该渠道的原生字段，按原名放在同一个请求体里透传，并且原生字段优先于同义的统一字段。`images` 只接受 http(s) 图片 URL，不接受 base64。`response_format` 只能是 `url`。

## 请求示例
```bash
curl https://api.novapi.ai/v1/images/generations \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "gpt-image-2",
  "prompt": "a watercolor illustration of a quiet seaside town",
  "size": "1536x1024",
  "async": true
}'
```

```python
import os, requests

r = requests.post(
    "https://api.novapi.ai/v1/images/generations",
    headers={"Authorization": "Bearer " + os.environ["API_KEY"]},
    json={
        "model": "gpt-image-2",
        "prompt": "a watercolor illustration of a quiet seaside town",
        "size": "1536x1024",
        "async": True,
    },
)
print(r.json())  # poll GET /v1/images/generations/{id} until status is completed
```

```ts
const resp = await fetch("https://api.novapi.ai/v1/images/generations", {
  method: "POST",
  headers: {
    Authorization: "Bearer " + process.env.API_KEY,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "model": "gpt-image-2",
    "prompt": "a watercolor illustration of a quiet seaside town",
    "size": "1536x1024",
    "async": true
  }),
});
console.log(await resp.json()); // poll GET /v1/images/generations/{id} until status is completed
```

## 响应示例
### 响应示例（异步，202）
```json
{
  "id": "img_<task-id>",
  "object": "image.generation",
  "model": "gpt-image-2",
  "status": "queued",
  "created": <unix-seconds>,
  "completed_at": null,
  "error": null,
  "data": []
}
```

### 响应示例（轮询到完成）
```json
{
  "id": "img_<task-id>",
  "object": "image.generation",
  "model": "gpt-image-2",
  "status": "completed",
  "created": <unix-seconds>,
  "completed_at": <unix-seconds>,
  "error": null,
  "data": [
    { "url": "https://..." }
  ]
}
```

```bash
curl https://api.novapi.ai/v1/images/generations/img_<task-id> \
  -H "Authorization: Bearer $API_KEY"

# status: queued | in_progress | completed | failed
# completed: data[].url holds the image URL(s); failed: error.code and error.message
```

## 响应字段说明
| 字段 | 类型 | 说明 |
| --- | --- | --- |
| id | string | 任务 ID，以 `img_` 开头，用于轮询。 |
| object | string | 固定为 `image.generation`。 |
| model | string | 请求里的 model。 |
| status | string | `queued`、`in_progress`、`completed` 或 `failed`。同步 200 的响应一定是 `completed`。 |
| created | integer | 创建时间（Unix 秒），与 OpenAI Images 一致。 |
| completed_at | integer \| null | 完成时间；未完成为 null。 |
| error | object \| null | 失败时为 `{code, message}`，`code` 为 `image_generation_failed`。 |
| data | array | 完成后是 `[{ "url": "https://..." }]`，一张图一个元素（`n` 大于 1 或 Midjourney 的 4 张时有多个）；未完成为空数组。 |
