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

# 生成图像

> 为已连接账户生成图像。采用与网页应用相同的媒体限额和保护措施。请保存返回的 generationId，并使用 images_download 获取图像。重试同一请求时请复用 requestId。

为已连接账户生成图像。采用与网页应用相同的媒体限额和保护措施。请保存返回的 generationId，并使用 images\_download 获取图像。重试同一请求时请复用 requestId。

```http theme={null}
POST https://api.voicecheap.ai/v1/actions/images_generate
```

## 身份验证和权限

<ParamField header="x-api-key" type="string" required>
  在此请求头中发送 VoiceCheap API 密钥。也支持通过 Authorization: Bearer 发送同一 API 密钥。OAuth 访问令牌仅适用于 MCP，此 REST 端点不接受 OAuth
  访问令牌。
</ParamField>

所需权限: `images:write`.

需要有效的付费订阅。权限不会改变团队角色、资源归属、媒体额度或套餐限制。

## 参数

<ParamField body="requestId" type="string">
  在传输层可选：如果省略，服务器会在验证前注入标识符。请自行提供稳定的 UUID v4，以便在响应丢失后安全恢复。

  格式: "uuid". 匹配模式: `^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$`.
</ParamField>

<ParamField body="prompt" type="string" required>
  文本值。

  最小长度: 10. 最大长度: 1200.
</ParamField>

<ParamField body="aspectRatio" type="string">
  文本值。

  允许的值: "square", "landscape", "portrait". 默认值: "square".
</ParamField>

<ParamField body="referenceImageKey" type="string">
  uploads/\{yourUserId}/ 下已上传的 JPG、PNG 或 WebP 对象键。不接受远程 URL。

  最小长度: 1. 最大长度: 512.
</ParamField>

<Accordion title="完整输入架构">
  以下 JSON Schema 保留了发现接口公开的确切字段名、嵌套类型、默认值、范围和可接受值。其他跨字段规则列于操作说明中。

  ```json theme={null}
  {
    "type": "object",
    "properties": {
      "requestId": {
        "type": "string",
        "format": "uuid",
        "pattern": "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$",
        "description": "UUID v4 for this action. Reuse the same ID with identical arguments on retries."
      },
      "prompt": {
        "type": "string",
        "minLength": 10,
        "maxLength": 1200
      },
      "aspectRatio": {
        "default": "square",
        "type": "string",
        "enum": ["square", "landscape", "portrait"]
      },
      "referenceImageKey": {
        "type": "string",
        "minLength": 1,
        "maxLength": 512,
        "description": "An uploaded JPG, PNG or WebP object key under uploads/{yourUserId}/. Remote URLs are not accepted."
      }
    },
    "required": ["prompt"],
    "additionalProperties": false
  }
  ```
</Accordion>

## 幂等性

为每次修改操作在 requestId 或 Idempotency-Key HTTP 请求头中提供 UUID v4。网络故障后，请使用相同标识符和完全相同的参数重试。如果同时提供这两个值，它们必须一致。重放已保存的请求不会重复执行操作，也不会再次扣除月度调用次数。失败或中断的请求不会自动重新执行。

对于每次修改操作，如果重试时发现同一请求仍在运行，即使首次执行通常为同步操作，也可能返回 HTTP 202。如果执行已完成但 resultStored 为 false，重试会返回 HTTP 200 和已保存的请求状态，而非原始结果。请通过对应领域的历史记录、结果或下载操作获取输出。

## 请求示例

请将示例资源标识符和媒体键替换为账户返回的值。重试同一次修改操作时，请保持请求标识符不变。

<RequestExample>
  ```bash cURL theme={null}
  curl --request POST 'https://api.voicecheap.ai/v1/actions/images_generate' \
    --header "x-api-key: $VOICECHEAP_API_KEY" \
    --header 'Content-Type: application/json' \
    --data '{"requestId":"61becb70-00d6-4de0-9eb1-05829a0d851c","prompt":"A peaceful coastal village at sunrise, with warm natural light.","aspectRatio":"square","referenceImageKey":"uploads/example-user/2502829e-3fde-4aad-b3c7-ce0f779fa2e6-reference.png"}'
  ```

  ```javascript JavaScript theme={null}
  const input = {
    requestId: '61becb70-00d6-4de0-9eb1-05829a0d851c',
    prompt: 'A peaceful coastal village at sunrise, with warm natural light.',
    aspectRatio: 'square',
    referenceImageKey: 'uploads/example-user/2502829e-3fde-4aad-b3c7-ce0f779fa2e6-reference.png',
  };
  const response = await fetch('https://api.voicecheap.ai/v1/actions/images_generate', {
    method: 'POST',
    headers: {
      'x-api-key': process.env.VOICECHEAP_API_KEY,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(input),
  });
  const body = await response.json();
  if (!response.ok) throw new Error(body.code + ': ' + body.message);
  console.log(body);
  ```

  ```python Python theme={null}
  import os
  import requests

  response = requests.post(
      "https://api.voicecheap.ai/v1/actions/images_generate",
      headers={"x-api-key": os.environ["VOICECHEAP_API_KEY"]},
      json={"requestId": "61becb70-00d6-4de0-9eb1-05829a0d851c", "prompt": "A peaceful coastal village at sunrise, with warm natural light.", "aspectRatio": "square", "referenceImageKey": "uploads/example-user/2502829e-3fde-4aad-b3c7-ce0f779fa2e6-reference.png"},
      timeout=120,
  )
  body = response.json()
  if not response.ok:
      raise RuntimeError(f"{body.get('code')}: {body.get('message')}")
  print(body)
  ```
</RequestExample>

## 响应

此操作在运行期间返回 HTTP 202。请保存 requestId，并使用该标识符和原始操作名称轮询 action\_request\_get。轮询状态包括 running、completed、failed 和 interrupted。已完成的结果可能指向独立的媒体任务；请继续使用该操作对应的状态或输出操作。

图片生成完成后，处理程序返回的最终生成元数据。不包含图片字节、previewDataUrl 或下载 URL。

<ResponseExample>
  ```json HTTP 202 theme={null}
  {
    "data": {
      "requestId": "61becb70-00d6-4de0-9eb1-05829a0d851c",
      "status": "running",
      "pollAction": "action_request_get",
      "pollAfterSeconds": 5
    },
    "requestId": "61becb70-00d6-4de0-9eb1-05829a0d851c"
  }
  ```

  ```json HTTP 200 theme={null}
  {
    "data": {
      "generationId": "6224018c7635dcb933ce5c8f7804aea73738dd1c",
      "status": "completed",
      "aspectRatio": "square",
      "width": 1024,
      "height": 1024,
      "referenceImageCount": 1,
      "createdAt": 1792144800000,
      "updatedAt": 1792144820000,
      "expiresAt": 1792749600000
    },
    "requestId": "61becb70-00d6-4de0-9eb1-05829a0d851c"
  }
  ```

  ```json HTTP 202 replay_running theme={null}
  {
    "data": {
      "requestId": "61becb70-00d6-4de0-9eb1-05829a0d851c",
      "operation": "images_generate",
      "createdAt": 1792144800000,
      "updatedAt": 1792144820000,
      "status": "running",
      "pollAfterSeconds": 5
    },
    "requestId": "61becb70-00d6-4de0-9eb1-05829a0d851c"
  }
  ```

  ```json HTTP 200 replay_completed_without_result theme={null}
  {
    "data": {
      "requestId": "61becb70-00d6-4de0-9eb1-05829a0d851c",
      "operation": "images_generate",
      "createdAt": 1792144800000,
      "updatedAt": 1792144820000,
      "status": "completed",
      "resultStored": false,
      "message": "The action completed. Retrieve its output from the corresponding history or resource action."
    },
    "requestId": "61becb70-00d6-4de0-9eb1-05829a0d851c"
  }
  ```
</ResponseExample>

## 结果字段

| 字段 | 说明 |
| - | - |
| `data.generationId` | 原样传给 images\_get 或 images\_download 的标识符。 |
| `data.status` | pending、completed、failed 或 expired。过期状态优先于已存储的状态。 |
| `data.aspectRatio` | square、landscape 或 portrait。 |
| `data.width` | 输出宽度，单位为像素；尺寸尚不可用时为 null。 |
| `data.height` | 输出高度，单位为像素；尺寸尚不可用时为 null。 |
| `data.referenceImageCount` | 使用的参考图片数量：0 或 1。 |
| `data.createdAt` | 创建时间，以 Unix 毫秒表示。 |
| `data.updatedAt` | 上次更新时间，采用 Unix 毫秒时间戳。 |
| `data.expiresAt` | 生成图像无法再下载的时间，以 Unix 毫秒表示。 |

## 恢复并轮询此请求

即使连接丢失，也应使用同一标识符。如果 resultStored 为 false，请通过相应的历史记录、结果或下载操作获取输出。切勿仅为查看进度而启动第二次生成。

```bash theme={null}
curl --request POST 'https://api.voicecheap.ai/v1/actions/action_request_get' \
  --header "x-api-key: $VOICECHEAP_API_KEY" \
  --header 'Content-Type: application/json' \
  --data '{"requestId":"61becb70-00d6-4de0-9eb1-05829a0d851c","operation":"images_generate"}'
```

```json HTTP 200 theme={null}
{
  "data": {
    "requestId": "61becb70-00d6-4de0-9eb1-05829a0d851c",
    "operation": "images_generate",
    "status": "completed",
    "createdAt": 1792144800000,
    "updatedAt": 1792144820000,
    "resultStored": true,
    "result": {
      "generationId": "6224018c7635dcb933ce5c8f7804aea73738dd1c",
      "status": "completed",
      "aspectRatio": "square",
      "width": 1024,
      "height": 1024,
      "referenceImageCount": 1,
      "createdAt": 1792144800000,
      "updatedAt": 1792144820000,
      "expiresAt": 1792749600000
    }
  }
}
```

## 操作说明

* 需要 images:write。prompt 会去除首尾空白，且须包含 10–1200 个字符；生成前还会进行规范化。aspectRatio 默认为 square，也接受 landscape 或 portrait。最多接受一个 referenceImageKey。
* 这是一个长时间运行的操作。首次 API/MCP 响应通常显示 running；请使用 operation images\_generate 和相同的 requestId 轮询 action\_request\_get，获取此处所示的最终结果。
* 使用账户的常规每日图片生成限额。账户生成的图片在创建七天后过期；请在 expiresAt 前下载结果。
* 此操作使用已连接用户自己的生成内容和上传文件，包括凭据在团队上下文中连接时。
* 引用键必须指向 uploads/\{yourUserId}/ 下已完成的上传，且以 .jpg、.jpeg、.png 或 .webp 结尾。不允许远程 URL、其他用户的键、路径穿越片段、反斜杠、百分号转义、查询字符串、片段标识符和控制字符。
* 每张参考图片必须非空，最大为 8 MiB（8,388,608 字节），解码后的图片类型必须与扩展名一致，且每边尺寸为 128–4096 像素。解码后的像素总数也受限。已上传的参考图片可以重复使用；这些生成操作不会消耗或删除其上传文件。
* 请使用 UUID v4 requestId 或 HTTP Idempotency-Key 标头，并在重试时以完全相同的输入重复使用。集成层会记录每次变更，并返回其已记录的状态或结果，不会重新执行。失败或中断的请求不会自动重试。
* 每位用户一次只能生成一张图片。使用 images\_list 和 images\_get 检查创建状态，使用 images\_download 获取 PNG 字节数据。生成标识符是不透明字符串；不要将其替换为 requestId。

## 用量

每次成功调用消耗一次月度 API/MCP 调用额度。媒体额度和操作本身的生成限额也适用。

## 错误

| HTTP 状态 | 代码 | 含义或恢复方式 |
| - | - | - |
| 400 | `BAD_REQUEST` | 服务器无法确定可信的请求网络上下文。 |
| 400 | `CREDENTIAL_IN_QUERY` | 凭证通过 access\_token 或 api\_key 查询参数提供。请在标头中发送凭证。 |
| 400 | `IDEMPOTENCY_CONFLICT` | Idempotency-Key 请求头必须与请求体中的 requestId 字段一致。 |
| 400 | `IMAGE_PROMPT_INVALID` | 规范化后的提示词不在 10–1200 个字符的范围内。 |
| 400 | `IMAGE_REFERENCE_INVALID` | 参考图片字节不符合受支持的图片类型、字节限额、尺寸或解码要求。 |
| 400 | `INVALID_REQUEST_ID` | requestId 或 Idempotency-Key 不是 UUID v4。 |
| 400 | `REFERENCE_IMAGE_INVALID` | 参考键不安全、不属于当前账户、不受支持、不可用，或其对象大于 8 MiB。 |
| 400 | `VALIDATION_ERROR` | 某个参数缺失、值不受支持、超出限额，或不属于严格输入架构。 |
| 401 | `API_KEY_EXPIRED` | API 密钥已过期。 |
| 401 | `INVALID_API_KEY` | 请提供有效且未过期的 API 密钥。 |
| 401 | `INVALID_CREDENTIAL` | Authorization 标头格式错误或与 x-api-key 冲突。 |
| 401 | `MISSING_CREDENTIAL` | 未提供可用的请求头凭据，或其长度超过 4096 个字符。 |
| 401 | `USER_NOT_FOUND` | 该凭据对应的账户已不存在。 |
| 403 | `ACCOUNT_UNAVAILABLE` | 调用者账户或计费账户不可用，或正在删除。 |
| 403 | `AMBIGUOUS_TEAM_API_KEY` | 此凭据对应的用户属于多个团队，因此计费上下文不明确。 |
| 403 | `INSUFFICIENT_SCOPE` | 请授予全部所需权限，或选择其他凭据。 |
| 403 | `INTEGRATION_PLAN_UNSUPPORTED` | 无法验证该账户的集成限制。 |
| 403 | `SUBSCRIPTION_REQUIRED` | 请使用有效的付费订阅。 |
| 403 | `TEAM_OWNER_NOT_FOUND` | 找不到实际生效的团队所有者账户。 |
| 409 | `CONCURRENT_ACTION_LIMIT_REACHED` | 此账户已有 10 个集成操作正在运行。请先轮询现有请求，再启动另一个操作。 |
| 409 | `IDEMPOTENCY_CONFLICT` | 该请求标识符已用于不同参数；请求头与请求体中的标识符不一致时，也会返回相同代码及 HTTP 400。 |
| 409 | `IMAGE_GENERATION_IN_PROGRESS` | 此图像请求或已连接用户的另一个图像请求仍在运行。 |
| 409 | `IMAGE_REQUEST_ID_REUSED` | 该图片请求标识符已与不同输入关联。 |
| 422 | `IMAGE_REQUEST_REJECTED` | 无法根据此提示词或参考图生成图像。请修改内容后发起新请求。 |
| 429 | `IMAGE_DAILY_LIMIT_REACHED` | 账户的每日图像生成限额已用尽。 |
| 429 | `INTEGRATION_MONTHLY_LIMIT_REACHED` | 请等待月度额度重置，或更改套餐。 |
| 429 | `INTEGRATION_RATE_LIMIT_REACHED` | 如果提供了 Retry-After，请等待其指定的时间，并采用退避重试。 |
| 500 | `INTERNAL_ERROR` | 在决定是否重试修改操作前，请检查已保存的请求状态和资源历史记录。 |
| 502 | `INTERNAL_ERROR` | 图像生成或准备失败。在决定发起另一次生成前，请检查请求状态。 |
| 503 | `ACTION_RESULT_UNAVAILABLE` | 操作已执行，但未能保存结果。在启动新的生成前，请检查已保存的请求状态和资源历史记录。 |
| 503 | `INTERNAL_ERROR` | 图像生成或其已存储的结果暂时不可用。重试前请检查请求状态。 |
| 503 | `SERVER_RESTARTING` | 服务器正在重启。请稍后重试；对于修改操作，请保持请求标识符不变。 |

```json theme={null}
{
  "code": "INSUFFICIENT_SCOPE",
  "message": "This credential does not permit the requested action.",
  "status": 403,
  "details": {
    "requiredScopes": ["images:write"]
  }
}
```


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