> ## 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 API key 儲存在 `COMETAPI_KEY`
* 用於檔案儲存範例的 Python 3.10+ 或 Node.js 18+
* 伺服器端環境。請勿在公開的前端程式碼中使用密鑰來呼叫圖片生成。

## API key、基底 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)。
* 在 [Models](/zh-Hant/overview/models) 中尋找圖片 model ID。
* 使用 [呼叫模型前預估請求成本](/zh-Hant/guides/how-to-estimate-cost-before-calling-a-model) 來估算請求成本。
* 使用 [錯誤代碼與重試策略](/zh-Hant/guides/error-codes-and-retry-strategy) 處理失敗情況。
