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

# Flux 2 API：文生图与图像编辑

> Black Forest Labs 图像生成 API，用于文生图和图生图工作流程

## 概览

Flux 2 是 Black Forest Labs 的图像模型，支持高质量文生图，也可以参考已有图片进行编辑。在 APIXO Playground 试用后，可以按本页示例调用 API。

| 功能 | 值 |
| - | - |
| 模型 ID | `flux-2` |
| 模式 | `pro-text-to-image`, `pro-image-to-image`, `flex-text-to-image`, `flex-image-to-image` |
| 提示词长度 | 3 至 5000 个字符 |
| 参考图片 | 1-8 个图生图模式的图片 URL |
| 尺寸 | 每侧 `width*height`、`64-2048` |
| 纵横比 | `1:1`、`4:3`、`3:4`、`3:2`、`2:3`、`16:9`、`9:16`、`auto`（`auto` 仅适用于与 `image_urls` 的图生图） |
| 分辨率档位 | `1K`, `2K` |

## 接口与身份验证

API 基础地址：

```text theme={null}
https://api.apixo.ai/api/v1
```

| 方法 | 端点 | 用途 |
| - | - | - |
| `POST` | `/generateTask/flux-2` | 提交生成任务 |
| `GET` | `/statusTask/flux-2?taskId={taskId}` | 查询任务状态并获取结果 |

每个请求都必须携带 APIXO API 密钥：

```http theme={null}
Authorization: Bearer YOUR_API_KEY
```

提交任务时，还需要设置以下请求头：

```http theme={null}
Content-Type: application/json
```

## 异步请求示例

以下示例提交一个最简单的文生图任务，响应中会返回 `taskId`。

```bash theme={null}
curl -X POST "https://api.apixo.ai/api/v1/generateTask/flux-2" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "request_type": "async",
    "input": {
      "mode": "pro-text-to-image",
      "prompt": "a cozy cyberpunk cafe with neon signs and rain on the windows",
      "size": "1000*700",
      "prompt_upsampling": true
    }
  }'
```

成功响应：

```json theme={null}
{
  "code": 200,
  "message": "success",
  "data": {
    "taskId": "task_12345678"
  }
}
```

保存 `taskId`，后续轮询结果时需要用到它。

## 轮询结果

```bash theme={null}
curl -X GET "https://api.apixo.ai/api/v1/statusTask/flux-2?taskId=task_12345678" \
  -H "Authorization: Bearer YOUR_API_KEY"
```

任务处理中的响应示例：

```json theme={null}
{
  "code": 200,
  "message": "success",
  "data": {
    "taskId": "task_12345678",
    "state": "processing",
    "createTime": 1767965610929
  }
}
```

成功响应：

```json theme={null}
{
  "code": 200,
  "message": "success",
  "data": {
    "taskId": "task_12345678",
    "state": "success",
    "resultJson": "{\"resultUrls\":[\"https://file.apixo.ai/xxx.png\"]}",
    "createTime": 1767965610929,
    "completeTime": 1767965652317,
    "costTime": 41388
  }
}
```

任务失败时的响应示例：

```json theme={null}
{
  "code": 200,
  "message": "success",
  "data": {
    "taskId": "task_12345678",
    "state": "failed",
    "failCode": "SensitiveContent",
    "failMsg": "Content was rejected by safety checks, please adjust the prompt",
    "createTime": 1767965610929,
    "completeTime": 1767965620132
  }
}
```

等 `state` 变为 `success` 后，再解析 `resultJson`：

```javascript theme={null}
const payload = JSON.parse(data.resultJson);
const imageUrls = payload.resultUrls;
```

## 请求体

### 文生图

```json theme={null}
{
  "request_type": "async",
  "input": {
    "mode": "pro-text-to-image",
    "prompt": "a minimal poster for a jazz night, with crisp readable typography",
    "size": "1024*1365",
    "prompt_upsampling": true
  }
}
```

### 图生图

```json theme={null}
{
  "request_type": "async",
  "input": {
    "mode": "flex-image-to-image",
    "prompt": "turn this product photo into a polished studio campaign image",
    "image_urls": [
      "https://example.com/source.png"
    ],
    "aspect_ratio": "1:1",
    "resolution": "2K",
    "size": "1024*1024",
    "use_preview": false
  }
}
```

## 参数

**request\_type** `string` *required* (default: async)

结果获取方式。选择 `async` 时，通过 `statusTask` 轮询结果；选择 `callback` 时，通过 Webhook 接收结果。

**callback\_url** `string`

当 `request_type` 为 `callback` 时必填。请提供公网可访问的 HTTPS 地址，用于接收任务的最终结果。详见 [Webhook](https://apixo.ai/docs/zh/api-reference/webhooks.md)。

**input** `object` *required*

Flux 2 的输入参数。

**属性**

**mode** `string` *required*

生成模式。可选值为 `pro-text-to-image`、`pro-image-to-image`、`flex-text-to-image`、`flex-image-to-image`。

**prompt** `string` *required*

用文字描述要生成的图像或要做的修改。长度须为 3 至 5000 个字符。

**image\_urls** `string[]`

参考图像 URL。 `pro-image-to-image` 和 `flex-image-to-image` 需要；不用于文生图模式。必须包含 `1-8` 个公共、可直接访问、非空 URL。支持格式：`JPG`、`PNG`、`WebP`；每个文件必须是 `<=10MB`。

**size** `string`

`width*height` 格式的可选输出尺寸。每条边必须在 `64` 到 `2048` 之间。若同时提供 `size`、`resolution` 和 `aspect_ratio`，则以 `resolution` 和 `aspect_ratio` 为准。

**aspect\_ratio** `string`

输出纵横比。支持的值：`1:1`、`4:3`、`3:4`、`3:2`、`2:3`、`16:9`、`9:16`、`auto`。`auto` 仅在提供 `image_urls` 时用于图生图模式，并采用第一个输入图像的纵横比。若未提供 `size`，则为必需项。

**resolution** `string`

Output分辨率层级。支持的值：`1K`、`2K`。值区分大小写。除非提供 `size`，否则为必需。如果与 `size` 一起提供，则 `resolution` 优先。

**prompt\_upsampling** `boolean`

为支持的工作流程启用提示上采样功能。

**use\_preview** `boolean`

对支持的工作流程使用预览模式。

### 模式选项

| 模式 | 使用案例 |
| - | - |
| `pro-text-to-image` | 专业文生图生成 |
| `pro-image-to-image` | 使用参考图像进行专业图生图编辑 |
| `flex-text-to-image` | Flex 文生图生成 |
| `flex-image-to-image` | 使用参考图像进行 Flex 图生图编辑 |

## 响应格式

### 提交任务时的响应

任务受理后，`POST /generateTask/flux-2` 会返回任务 ID：

**code** `integer`

API 状态码。`200` 表示任务已受理。

**message** `string`

状态说明。

**data.taskId** `string`

任务的唯一 ID，用于查询任务状态。

### 状态响应字段

**taskId** `string`

任务的唯一标识符。

**state** `string`

当前任务状态，可能的值为 `processing`、`success`、`failed`。

**resultJson** `string`

包含生成图片 URL 的 JSON 字符串。当 `state` 为 `success` 时返回。

**failCode** `string`

供程序处理的错误码，仅在 `state` 为 `failed` 时返回。

**failMsg** `string`

失败原因说明，仅在 `state` 为 `failed` 时返回。

**createTime** `integer`

任务创建时的 Unix 时间戳，单位为毫秒。

**completeTime** `integer`

任务完成时的 Unix 时间戳，单位为毫秒。任务完成后返回。

**costTime** `integer`

处理耗时，单位为毫秒。任务成功后返回。

## Webhook 回调模式

如果希望服务端自动收到最终结果，可以使用回调模式，无需轮询。

```bash theme={null}
curl -X POST "https://api.apixo.ai/api/v1/generateTask/flux-2" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "request_type": "callback",
    "callback_url": "https://your-server.com/webhooks/apixo",
    "input": {
      "mode": "pro-text-to-image",
      "prompt": "a premium coffee bag label with exact readable brand typography",
      "aspect_ratio": "1:1",
      "resolution": "2K"
    }
  }'
```

Webhook 接收要求和重试规则见 [Webhook 文档](https://apixo.ai/docs/zh/api-reference/webhooks.md)。

## 计费

新提交的非 LLM 请求按 **1 积分 = `$0.001`** 计费。先按用量和附加项目计算总额，再应用账号折扣，最后将请求总额向上取整到整数积分。如果约定了固定单价，则按该单价的原始精度计算，不再使用标准价或另行打折；最终积分仍向上取整。详见[积分换算与取整](https://apixo.ai/docs/zh/pricing.md#credit-conversion-and-rounding)。

Flux 2 按生成的图像计费。在任一受支持的分辨率下，`pro-image-to-image` 成本为 `$0.06 / image`。其他模式使用以下价格。

| 模式层 | 分辨率 | APIXO 价格 |
| - | - | - |
| `pro-text-to-image` | `1K` | `$0.04 / image` |
| `pro-text-to-image` | `2K` | `$0.05 / image` |
| `pro-image-to-image` | `1K`或`2K` | `$0.06 / image` |
| `flex-*` | `1K` | `$0.10 / image` |
| `flex-*` | `2K` | `$0.16 / image` |

最新的美元费率见 [API 价目表](https://apixo.ai/pricing/api)。

## 延迟与轮询

实际延迟可能因提示复杂性、参考图像计数、提供者路由和当前队列负载而异。

| 通常生成耗时 | 建议首次查询时间 | 轮询间隔 |
| - | - | - |
| 大约30秒 | 任务创建后 20 秒 | 3-10 秒 |

> [!TIP]
>
> 生产环境并发量较高时，建议使用回调，以免频繁轮询。

请求频率和并发上限因账号、API 密钥和路由而异。收到 `429` 时，请降低请求频率，逐步延长重试间隔。账号级限制见[系统 API](https://apixo.ai/docs/zh/api-reference/system.md)。

## 错误与排查

### HTTP 错误

| 代码 | 含义 | 处理方式 |
| - | - | - |
| `400` | 请求体无效、参数缺失、值不受支持、提示长度问题或图像无法访问 URL | 在重试前修复请求 |
| `401` | 缺少 API 密钥，或密钥无效 | 检查 `Authorization` 请求头 |
| `402` | 余额或配额不足 | 充值或更换账号或API密钥 |
| `403` | 密钥或路由无法访问模型，或者内容被拒绝 | 检查权限并调整提示或图像 |
| `429` | 已达到速率或并发限制 | 按指数退避策略重试 |
| `500` | 服务器错误或未知任务失败 | 逐步延长间隔后重试 |
| `502` | 服务错误 | 逐步延长间隔后重试 |
| `504` | 服务超时 | 重试；耗时较长的任务可改用回调模式 |

### 任务失败错误码

| 错误码 | 含义 | 处理方式 |
| - | - | - |
| `SensitiveContent` | 提示、输入图像或输出被安全检查标记为不安全 | 更改提示或参考图像 |
| `ProhibitedContentDetected` | 安全检查因禁止内容而拒绝任务 | 调整提示或输入图像 |
| `ImageNotAccess` | 无法获取参考图像 URL | 使用公开的直接图像 URL |
| `PromptInvalid` | 提示词无效或被拒绝 | 重写提示 |
| `MissingParameter` | 缺少一个或多个必需参数 | 检查`mode`、`prompt`、`aspect_ratio`、`resolution`和`image_urls` |
| `RateLimited` | 请求频率达到上限 | 逐步延长间隔后重试 |
| `Timeout` | 任务超时 | 重试、降低输入复杂度或使用回调模式 |
| `Unknown error` | 任务因未知原因失败 | 重试后退或联系支持人员（如果问题仍然存在） |

完整列表见[错误码](https://apixo.ai/docs/zh/api-reference/errors.md)。

## MCP 集成

[完整模型 Schema](https://apixo.ai/docs/models/schemas/image/flux-2.json) 中的 `mcp.result` 指定了结果的解析方式，`mcp.attachments` 列出支持绑定文件的字段。这些元数据供客户端集成使用，无需作为生成参数提交。附件字段为 `image_urls`。请保留用户指定的文件顺序。API 负责校验模型参数。

## 相关链接

* [生成 API 概览](https://apixo.ai/docs/zh/models.md)
* [图像模型](https://apixo.ai/docs/zh/models/image)
* [提交生成任务](https://apixo.ai/docs/zh/api-reference/generate-task.md)
* [查询任务状态](https://apixo.ai/docs/zh/api-reference/status-task.md)
* [Webhook](https://apixo.ai/docs/zh/api-reference/webhooks.md)
* [错误码](https://apixo.ai/docs/zh/api-reference/errors.md)
* [参数规范](https://apixo.ai/docs/zh/api-reference/parameters.md)
* [路由策略](https://apixo.ai/docs/zh/concepts/routing-strategies.md)
* [在 APIXO Playground 尝试 Flux 2](https://apixo.ai/models/flux-2)
* [API定价](https://apixo.ai/pricing/api)


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