# 文生图

**这是什么**：用 Qwen 图片模型根据文字描述生成图片。

**什么时候用**：想要通义千问/通义万相风格的高质量图片时用。

## 接口地址
- 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 格式。通用说明见图片指南。

同步（默认）：服务端等到图片生成完再返回 200 与 `data[].url`；超过 110 秒仍未完成则返回 202 与任务对象和 `Location` 头，任务继续运行，不要重新提交（不会重复计费），改用 `GET /v1/images/generations/{id}` 取结果。异步（`"async": true` 或请求头 `Prefer: respond-async`）：立即返回 202 与任务对象（`status: queued`，`Retry-After: 3`），用 GET 轮询到 `status` 为 `completed` 或 `failed`。

## 请求参数
| 参数 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- |
| model | string | 是 | qwen-image / qwen-image-plus / qwen-image-2.0 / qwen-image-2.0-pro / qwen-image-max / qwen-image-plus-2026-01-09 / wan2.7-image / wan2.7-image-pro / wan2.5-t2i-preview |
| prompt | string | 是 | 图片描述 |
| aspect_ratio | string | — | 输出比例：1:1 / 16:9 / 9:16 / 4:3 / 3:4 / 2:3 / 3:2 / 21:9 |
| n | integer | — | 生成张数，默认 1（影响计费） |
| negative_prompt | string | — | 负面提示词 |
| prompt_extend | boolean | — | 是否智能扩展提示词，默认 true |
| webhookUrl | string | — | 完成回调地址 |
| size | string | — | `宽x高`（如 `1024x1024`）：换算成该渠道接受的最接近的宽高比；同时给了 `aspect_ratio` 时以 `aspect_ratio` 为准。nano / gemini 还接受 `auto`。 |
| async | boolean | — | `true`：立即返回 202 与任务对象，再用 GET 轮询（也可用请求头 `Prefer: respond-async`）。缺省为同步。 |

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

## 请求示例
### 同步请求（OpenAI SDK）

```bash
curl https://api.novapi.ai/v1/images/generations \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "qwen-image-max",
  "prompt": "赛博朋克风格的雨夜街道，霓虹反光，电影质感",
  "aspect_ratio": "16:9",
  "n": 1
}'
```

```python
from openai import OpenAI

client = OpenAI(base_url="https://api.novapi.ai/v1", api_key="sk-...")

img = client.images.generate(
    model="qwen-image-max",
    prompt="赛博朋克风格的雨夜街道，霓虹反光，电影质感",
    n=1,
    extra_body={
        "aspect_ratio": "16:9",
    },
)
print(img.data[0].url)
```

```ts
import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://api.novapi.ai/v1", apiKey: process.env.API_KEY });

const img = await client.images.generate({
  "model": "qwen-image-max",
  "prompt": "赛博朋克风格的雨夜街道，霓虹反光，电影质感",
  "n": 1,
  "aspect_ratio": "16:9"
} as OpenAI.ImageGenerateParams);
console.log(img.data?.[0]?.url);
```

### 异步请求与轮询

```bash
curl https://api.novapi.ai/v1/images/generations \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "qwen-image-max",
  "prompt": "赛博朋克风格的雨夜街道，霓虹反光，电影质感",
  "aspect_ratio": "16:9",
  "n": 1,
  "async": true
}'
```

## 响应示例
### 响应示例（同步，200）
```json
{
  "id": "img_<task-id>",
  "object": "image.generation",
  "model": "qwen-image-max",
  "status": "completed",
  "created": <unix-seconds>,
  "completed_at": <unix-seconds>,
  "error": null,
  "data": [
    { "url": "https://..." }
  ]
}
```

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

```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 张时有多个）；未完成为空数组。 |
