> ## 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.

# 影像生成與編輯 API

> 選擇 CometAPI 影像路由，以支援 OpenAI 相容的生成與編輯、Gemini 與 Nano Banana、Midjourney、Flux、Bria、Replicate，以及 Seedream 或 SeedEdit 工作流程。

依照符合你工作流程的請求格式，使用 CometAPI 影像模型文件。OpenAI 相容的影像端點涵蓋常見的生成與編輯流程；供應商專屬頁面則涵蓋以任務為導向的影像系統。

## 選擇影像 API

<CardGroup cols={2}>
  <Card title="建立影像" icon="image" href="/api/image/openai/images">
    使用 OpenAI 相容請求生成影像。
  </Card>

  <Card title="建立影像編輯" icon="sparkles" href="/api/image/openai/image-editing">
    使用 OpenAI 相容請求編輯影像。
  </Card>

  <Card title="使用 Gemini 生成影像" icon="image" href="/api/image/gemini/gemini-generates-image">
    使用 Gemini 格式生成或編輯影像。
  </Card>

  <Card title="開始使用 Midjourney API" icon="sparkles" href="/api/image/midjourney/midjourney-api-quick-start">
    提交並輪詢 Midjourney 影像任務。
  </Card>

  <Card title="生成 Flux 影像" icon="image" href="/api/image/flux/flux-generate-image">
    透過 CometAPI 生成 Flux 影像。
  </Card>

  <Card title="生成 Bria 影像" icon="image" href="/api/image/bria/generate-image">
    使用 Bria 影像生成工具。
  </Card>

  <Card title="建立 Replicate prediction" icon="sparkles" href="/api/image/replicate/create-predictions-general">
    執行 Replicate 格式的影像 prediction 請求。
  </Card>

  <Card title="建立 Seedream 影像" icon="image" href="/api/image/seededit-seedream/bytedance-image-generation">
    建立 Seedream 影像生成請求。
  </Card>
</CardGroup>

## 生成影像

使用來自 [Models page](/zh-Hant/overview/models) 或 [model directory](https://www.cometapi.com/models/) 的支援影像的 model ID。以下範例呼叫 OpenAI 相容的「建立影像」端點。

<Note>
  這些範例使用預留位置 `your-image-model-id`。執行請求前，請將它替換為 [Models page](/zh-Hant/overview/models) 或 [model directory](https://www.cometapi.com/models/) 中可用的影像 model ID。
</Note>

<Tip>
  開啟 [建立影像](/api/image/openai/images) 以使用 playground 與端點結構描述。
</Tip>

<CodeGroup>
  ```python Python theme={null}
  import os
  import requests

  response = requests.post(
      "https://api.cometapi.com/v1/images/generations",
      headers={
          "Authorization": "Bearer " + os.environ["COMETAPI_KEY"],
          "Content-Type": "application/json",
      },
      json={
          "model": "your-image-model-id",
          "prompt": "A clean product photo of a glass teapot on a white table",
          "size": "1024x1024",
      },
      timeout=120,
  )

  response.raise_for_status()
  result = response.json()
  print(result["data"][0].keys())
  ```

  ```javascript Node.js theme={null}
  const response = await fetch("https://api.cometapi.com/v1/images/generations", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      model: "your-image-model-id",
      prompt: "A clean product photo of a glass teapot on a white table",
      size: "1024x1024",
    }),
  });

  if (!response.ok) {
    throw new Error(await response.text());
  }

  const result = await response.json();
  console.log(Object.keys(result.data[0]));
  ```

  ```bash cURL theme={null}
  curl https://api.cometapi.com/v1/images/generations \
    -H "Authorization: Bearer $COMETAPI_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "your-image-model-id",
      "prompt": "A clean product photo of a glass teapot on a white table",
      "size": "1024x1024"
    }'
  ```
</CodeGroup>

## 回應範例

成功的回應可能如下所示。視所選模型而定，每個項目都可能包含 base64 圖像或結果 URL：

```json theme={null}
{
  "created": 1779872000,
  "background": "opaque",
  "data": [
    {
      "b64_json": "iVBORw0KGgo..."
    }
  ],
  "output_format": "png",
  "quality": "low",
  "size": "1024x1024",
  "usage": {
    "input_tokens": 19,
    "input_tokens_details": {
      "image_tokens": 0,
      "text_tokens": 19
    },
    "output_tokens": 196,
    "output_tokens_details": {
      "image_tokens": 196,
      "text_tokens": 0
    },
    "total_tokens": 215
  }
}
```

## 範例模型記錄

<Info>
  這個範例模型目錄回應展示了 `/api/models` 的封裝格式，以及單一圖像模型記錄的結構。這不是完整的模型清單。
</Info>

```bash cURL theme={null}
curl https://api.cometapi.com/api/models
```

```json theme={null}
{
  "success": true,
  "page": 1,
  "page_size": 20,
  "total": 302,
  "data": [
    {
      "created": 1776391310,
      "id": "your-image-model-id",
      "code": "your-image-model-id",
      "provider": "ExampleProvider",
      "provider_code": "example",
      "name": "Example image model",
      "model_type": "image",
      "features": [
        "text-to-image"
      ],
      "endpoints": "{\n  \"openai-image\": {\n    \"path\": \"/v1/images/generations\",\n    \"method\": \"POST\"\n  }\n}",
      "pricing": {
        "currency": "USD / M Tokens",
        "input": 4,
        "output": 24,
        "per_request": null,
        "per_second": null
      }
    }
  ]
}
```

## 常見錯誤

<AccordionGroup>
  <Accordion title="Invalid model ID">
    請從[模型頁面](/zh-Hant/overview/models)選擇支援圖像的模型。
  </Accordion>

  <Accordion title="Unsupported size">
    請使用所選圖像端點可接受的尺寸。
  </Accordion>

  <Accordion title="Expired result URL">
    請在供應商連結過期前下載已產生的資產。
  </Accordion>

  <Accordion title="Upload too large">
    請改用圖像 URL 或較小的檔案，而非大型 base64 負載。
  </Accordion>

  <Accordion title="Missing image data">
    請同時檢查 `b64_json` 和 `url` 欄位，因為輸出結構可能因模型而異。
  </Accordion>
</AccordionGroup>

## 錯誤碼與重試策略

<AccordionGroup>
  <Accordion title="400">
    在修正 prompt、尺寸或圖像輸入之前，請勿重試。
  </Accordion>

  <Accordion title="401">
    在 API key 已提供且有效之前，請勿重試。
  </Accordion>

  <Accordion title="404">
    重試前請檢查 base URL、路徑和 model ID。
  </Accordion>

  <Accordion title="413">
    請先縮小上傳大小後再重試。
  </Accordion>

  <Accordion title="429">
    請使用指數退避重試，並降低並行數。
  </Accordion>

  <Accordion title="500 or 503">
    針對暫時性的供應商或服務錯誤，請使用退避策略重試。
  </Accordion>
</AccordionGroup>

<Tip>
  如需實作模式，請參閱[錯誤碼與重試策略](/zh-Hant/guides/error-codes-and-retry-strategy)與[速率限制與並行處理](/zh-Hant/guides/rate-limits-and-concurrency)。
</Tip>

## 定價與模型目錄

<CardGroup cols={3}>
  <Card title="Models page" icon="list" href="/overview/models">
    了解 CometAPI 如何在文件中提供 model ID。
  </Card>

  <Card title="Model directory" icon="puzzle-piece" href="https://www.cometapi.com/models/">
    瀏覽模型可用性與功能。
  </Card>

  <Card title="Pricing" icon="tag" href="https://www.cometapi.com/pricing/">
    在呼叫模型前先檢查定價。
  </Card>
</CardGroup>
