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

# GPT Image API 快速入门：通过 CometAPI 生成图像

> 通过 CometAPI 生成 GPT 图像，解码 b64_json，并使用 curl、Python 或 Node.js 保存结果。

## 你将构建什么

你将调用 `POST /v1/images/generations`，使用 GPT 图像模型生成一张图像，解码返回的 `b64_json`，并将其保存为图像文件。

## 前提条件

* 存储在 `COMETAPI_KEY` 中的 CometAPI API key
* 用于文件保存示例的 Python 3.10+ 或 Node.js 18+
* 服务端环境。不要在公开的前端代码中使用密钥调用图像生成。

## API key、base URL、身份验证

使用兼容 OpenAI 的图像端点：

```text theme={null}
https://api.cometapi.com/v1/images/generations
```

使用 Bearer token 进行身份验证：

```text theme={null}
Authorization: Bearer $COMETAPI_KEY
```

## 代码示例

使用下面的标签页获取可直接复制的 cURL、Python 和 Node.js 示例。

<CodeGroup>
  ```bash cURL theme={null}
  curl https://api.cometapi.com/v1/images/generations \
    -H "Authorization: Bearer $COMETAPI_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "gpt-image-2",
      "prompt": "A paper boat floating on calm water at sunrise.",
      "quality": "low",
      "size": "1024x1024",
      "output_format": "jpeg"
    }'
  ```

  ```python Python theme={null}
  import base64
  import os
  from openai import OpenAI

  client = OpenAI(
      api_key=os.environ["COMETAPI_KEY"],
      base_url="https://api.cometapi.com/v1",
  )

  image = client.images.generate(
      model="gpt-image-2",
      prompt="A paper boat floating on calm water at sunrise.",
      quality="low",
      size="1024x1024",
      output_format="jpeg",
  )

  image_base64 = image.data[0].b64_json
  with open("cometapi-gpt-image.jpeg", "wb") as file:
      file.write(base64.b64decode(image_base64))
  ```

  ```javascript Node.js theme={null}
  import fs from "node:fs/promises";
  import OpenAI from "openai";

  const client = new OpenAI({
    apiKey: process.env.COMETAPI_KEY,
    baseURL: "https://api.cometapi.com/v1",
  });

  const image = await client.images.generate({
    model: "gpt-image-2",
    prompt: "A paper boat floating on calm water at sunrise.",
    quality: "low",
    size: "1024x1024",
    output_format: "jpeg",
  });

  const imageBase64 = image.data[0].b64_json;
  await fs.writeFile("cometapi-gpt-image.jpeg", Buffer.from(imageBase64, "base64"));
  ```
</CodeGroup>

## 流程说明

GPT 图像生成会在一次请求中返回完整响应。GPT 图像模型会在 `data[0].b64_json` 中返回 base64 图像数据。解码该值，并将字节写入与 `output_format` 匹配的文件。

如果你需要 URL 输出，请检查所选模型是否支持 URL 响应。GPT 图像模型在维护的参考示例中使用 base64 输出。

## 常用参数

| Parameter       | 用途                                        |
| --------------- | ----------------------------------------- |
| `model`         | GPT 图像 model ID。参考示例使用 `gpt-image-2`。     |
| `prompt`        | 要生成图像的文本描述。                               |
| `size`          | 请求的输出尺寸。支持的值取决于所选模型。                      |
| `quality`       | 适用于支持该参数的模型的质量设置。                         |
| `output_format` | GPT 图像结果的编码图像类型，例如 `jpeg`、`png` 或 `webp`。 |

## 故障排查 / FAQ

<AccordionGroup>
  <Accordion title="保存的文件无法打开">
    确认你已将 `b64_json` 解码为 base64 字节，并使用与 `output_format` 匹配的文件扩展名保存文件。
  </Accordion>

  <Accordion title="模型拒绝某个参数">
    不同模型的图像参数各不相同。先从最小参考示例开始，然后每次添加一个可选字段。
  </Accordion>

  <Accordion title="响应中没有 URL">
    对于 GPT 图像模型，请使用 `b64_json`。URL 输出取决于具体模型。
  </Accordion>
</AccordionGroup>

## 后续步骤

* 阅读[创建图像 API 参考](/api/image/openai/images)。
* 在[模型](/zh-Hans/overview/models)中查找图像 model ID。
* 使用[在调用模型前估算请求成本](/zh-Hans/guides/how-to-estimate-cost-before-calling-a-model)来估算请求成本。
* 使用[错误代码和重试策略](/zh-Hans/guides/error-codes-and-retry-strategy)处理失败情况。
