> ## 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 tạo và chỉnh sửa hình ảnh

> Chọn các route hình ảnh của CometAPI cho tác vụ tạo và chỉnh sửa tương thích OpenAI, Gemini và Nano Banana, Midjourney, Flux, Bria, Replicate, và quy trình Seedream hoặc SeedEdit.

Sử dụng tài liệu mô hình hình ảnh của CometAPI bằng cách chọn định dạng request phù hợp với quy trình làm việc của bạn. Các endpoint hình ảnh tương thích OpenAI hỗ trợ các luồng tạo và chỉnh sửa phổ biến; các trang dành riêng cho từng nhà cung cấp bao quát các hệ thống hình ảnh theo tác vụ.

## Chọn một image API

<CardGroup cols={2}>
  <Card title="Tạo một hình ảnh" icon="image" href="/api/image/openai/images">
    Tạo hình ảnh bằng một request tương thích OpenAI.
  </Card>

  <Card title="Tạo chỉnh sửa hình ảnh" icon="sparkles" href="/api/image/openai/image-editing">
    Chỉnh sửa hình ảnh bằng một request tương thích OpenAI.
  </Card>

  <Card title="Tạo hình ảnh với Gemini" icon="image" href="/api/image/gemini/gemini-generates-image">
    Tạo hoặc chỉnh sửa hình ảnh với định dạng Gemini.
  </Card>

  <Card title="Bắt đầu với Midjourney API" icon="sparkles" href="/api/image/midjourney/midjourney-api-quick-start">
    Gửi và poll các tác vụ hình ảnh Midjourney.
  </Card>

  <Card title="Tạo một hình ảnh Flux" icon="image" href="/api/image/flux/flux-generate-image">
    Tạo hình ảnh Flux thông qua CometAPI.
  </Card>

  <Card title="Tạo một dự đoán Replicate" icon="sparkles" href="/api/image/replicate/create-predictions-general">
    Chạy các request dự đoán hình ảnh theo định dạng Replicate.
  </Card>

  <Card title="Tạo một hình ảnh Bria" icon="image" href="/api/image/bria/generate-image">
    Sử dụng các công cụ tạo hình ảnh Bria.
  </Card>

  <Card title="Tạo hình ảnh Seedream" icon="image" href="/api/image/seededit-seedream/bytedance-image-generation">
    Tạo các request sinh hình ảnh Seedream.
  </Card>
</CardGroup>

## Tạo một hình ảnh

Sử dụng một model ID có khả năng xử lý hình ảnh từ [trang Models](/vi/overview/models) hoặc [danh mục model](https://www.cometapi.com/models/). Các ví dụ bên dưới gọi endpoint Create an image tương thích OpenAI.

<Note>
  Các ví dụ này sử dụng placeholder `your-image-model-id`. Hãy thay thế nó bằng một model ID hình ảnh khả dụng từ [trang Models](/vi/overview/models) hoặc [danh mục model](https://www.cometapi.com/models/) trước khi bạn chạy request.
</Note>

<Tip>
  Mở [Create an image](/api/image/openai/images) để sử dụng playground và schema của endpoint.
</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>

## Ví dụ phản hồi

Một phản hồi thành công có thể trông như sau. Tùy thuộc vào model được chọn, mỗi mục có thể chứa một hình ảnh base64 hoặc một URL kết quả:

```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
  }
}
```

## Ví dụ về bản ghi model

<Info>
  Phản hồi danh mục model ví dụ này hiển thị lớp bao `/api/models` và hình dạng của một bản ghi model hình ảnh. Đây không phải là danh sách model đầy đủ.
</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
      }
    }
  ]
}
```

## Các lỗi thường gặp

<AccordionGroup>
  <Accordion title="Invalid model ID">
    Chọn một model có hỗ trợ hình ảnh từ [trang Models](/vi/overview/models).
  </Accordion>

  <Accordion title="Unsupported size">
    Sử dụng kích thước mà endpoint hình ảnh đã chọn chấp nhận.
  </Accordion>

  <Accordion title="Expired result URL">
    Tải xuống các tài nguyên được tạo trước khi các liên kết từ nhà cung cấp hết hạn.
  </Accordion>

  <Accordion title="Upload too large">
    Sử dụng URL hình ảnh hoặc các tệp nhỏ hơn thay vì payload base64 lớn.
  </Accordion>

  <Accordion title="Missing image data">
    Kiểm tra cả trường `b64_json` và `url` vì hình dạng đầu ra có thể khác nhau tùy theo model.
  </Accordion>
</AccordionGroup>

## Mã lỗi và chiến lược retry

<AccordionGroup>
  <Accordion title="400">
    Không retry cho đến khi prompt, kích thước hoặc dữ liệu đầu vào của hình ảnh được sửa.
  </Accordion>

  <Accordion title="401">
    Không retry cho đến khi API key hiện diện và hợp lệ.
  </Accordion>

  <Accordion title="404">
    Kiểm tra base URL, path và model ID trước khi retry.
  </Accordion>

  <Accordion title="413">
    Giảm kích thước tải lên trước khi retry.
  </Accordion>

  <Accordion title="429">
    Retry với exponential backoff và giảm mức độ đồng thời.
  </Accordion>

  <Accordion title="500 or 503">
    Retry với backoff cho các lỗi tạm thời từ nhà cung cấp hoặc dịch vụ.
  </Accordion>
</AccordionGroup>

<Tip>
  Để biết các mẫu triển khai, xem [Mã lỗi và chiến lược retry](/vi/guides/error-codes-and-retry-strategy) và [Giới hạn tốc độ và mức độ đồng thời](/vi/guides/rate-limits-and-concurrency).
</Tip>

## Giá và thư mục model

<CardGroup cols={3}>
  <Card title="Models page" icon="list" href="/overview/models">
    Đọc cách CometAPI hiển thị model ID trong tài liệu.
  </Card>

  <Card title="Model directory" icon="puzzle-piece" href="https://www.cometapi.com/models/">
    Duyệt tình trạng khả dụng và các khả năng của model.
  </Card>

  <Card title="Pricing" icon="tag" href="https://www.cometapi.com/pricing/">
    Kiểm tra giá trước khi bạn gọi một model.
  </Card>
</CardGroup>
