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

# 创建一个 Kling 虚拟试衣

> 使用 CometAPI Kling Virtual Try-On API，通过人物图片和服装图片创建一张试衣图。

使用此端点，可以通过一张人物图片和一张服装图片生成虚拟试衣图。

## 调用前准备

* 提供一张 `human_image` 和一张 `cloth_image`
* 从 `kolors-virtual-try-on-v1` 或 `kolors-virtual-try-on-v1-5` 开始
* 首次测试时，使用干净的服装产品图或白底服装图片

## 任务流程

<Steps>
  <Step title="提交试衣任务">
    发送人物图片和服装图片，然后保存返回的任务 id。
  </Step>

  <Step title="轮询任务">
    轮询对应的 Kling 图片查询路径，直到任务进入终态并返回渲染后的图片。
  </Step>

  <Step title="保存结果">
    如果试衣结果需要保留更长时间，请将完成的图片保存到你自己的存储中。
  </Step>
</Steps>

<Tip>
  有关完整的参数参考，请参阅[官方 Kling 文档](https://kling.ai/document-api/api/image/virtual-try-on)。
</Tip>


## OpenAPI

````yaml api/openapi/image/kling/post-virtual-try-on.openapi.json POST /kling/v1/images/kolors-virtual-try-on
openapi: 3.1.0
info:
  title: Kling Virtual Try-On API
  version: 1.0.0
servers:
  - url: https://api.cometapi.com
security:
  - bearerAuth: []
paths:
  /kling/v1/images/kolors-virtual-try-on:
    post:
      summary: Create a Kling virtual try-on
      operationId: kling_virtual_try_on
      parameters:
        - name: Content-Type
          in: header
          required: false
          description: Must be `application/json`.
          schema:
            type: string
            default: application/json
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - human_image
                - cloth_image
              properties:
                model_name:
                  type: string
                  description: >-
                    Virtual try-on model version. Defaults to
                    `kolors-virtual-try-on-v1`.
                  enum:
                    - kolors-virtual-try-on-v1
                    - kolors-virtual-try-on-v1-5
                  default: kolors-virtual-try-on-v1
                human_image:
                  type: string
                  description: >-
                    Person image to dress. Image URL or raw Base64 string
                    without a `data:` prefix. Supported formats: JPG, JPEG, PNG.
                    Maximum 10 MB, minimum 300x300 px, aspect ratio between
                    1:2.5 and 2.5:1.
                cloth_image:
                  type: string
                  description: >-
                    Clothing image for the try-on. Image URL or raw Base64
                    string without a `data:` prefix. Supported formats: JPG,
                    JPEG, PNG. Maximum 10 MB, minimum 300x300 px, aspect ratio
                    between 1:2.5 and 2.5:1. Supports tops, bottoms, and
                    one-piece dresses.
                callback_url:
                  type: string
                  description: Webhook URL for task status notifications.
                external_task_id:
                  type: string
                  description: >-
                    Optional user-defined task ID for your own tracking. Must be
                    unique per account.
              default:
                model_name: kolors-virtual-try-on-v1
                human_image: https://your-image-host/person.jpg
                cloth_image: https://your-image-host/garment.jpg
            examples:
              Default:
                summary: Virtual try-on request
                value:
                  model_name: kolors-virtual-try-on-v1
                  human_image: https://your-image-host/person.jpg
                  cloth_image: https://your-image-host/garment.jpg
      responses:
        '200':
          description: Task request accepted or an error response returned by the API.
          content:
            application/json:
              schema:
                type: object
                required:
                  - code
                  - message
                  - data
                properties:
                  code:
                    oneOf:
                      - type: integer
                      - type: string
                    description: Response code. `0` means the task request was accepted.
                  message:
                    type: string
                    description: Response message.
                  request_id:
                    type: string
                    description: Request identifier returned when present.
                  data:
                    type: object
                    required:
                      - task_id
                      - task_status
                      - created_at
                      - updated_at
                    properties:
                      task_id:
                        type: string
                        description: System-generated task ID.
                      task_status:
                        type: string
                        description: Task status.
                        enum:
                          - submitted
                          - processing
                          - succeed
                          - failed
                      task_status_msg:
                        oneOf:
                          - type: string
                          - type: 'null'
                        description: Task status detail when present.
                      task_info:
                        type: object
                        properties:
                          external_task_id:
                            oneOf:
                              - type: string
                              - type: 'null'
                            description: Caller-provided external task ID when supplied.
                        additionalProperties: true
                      task_result:
                        oneOf:
                          - type: object
                            additionalProperties: true
                          - type: 'null'
                        description: Task result payload after the task completes.
                      created_at:
                        type: integer
                        description: >-
                          Task creation time as a Unix timestamp in
                          milliseconds.
                      updated_at:
                        type: integer
                        description: Task update time as a Unix timestamp in milliseconds.
                    additionalProperties: true
                additionalProperties: true
      x-codeSamples:
        - lang: Shell
          label: Default
          source: >
            curl https://api.cometapi.com/kling/v1/images/kolors-virtual-try-on
            \
              -H "Authorization: Bearer $COMETAPI_KEY" \
              -H "Content-Type: application/json" \
              -d '{
              "model_name": "kolors-virtual-try-on-v1",
              "human_image": "https://your-image-host/person.jpg",
              "cloth_image": "https://your-image-host/garment.jpg"
            }'
        - lang: Python
          label: Default
          source: |
            import os
            import requests

            response = requests.post(
                "https://api.cometapi.com/kling/v1/images/kolors-virtual-try-on",
                headers={"Authorization": "Bearer " + os.environ["COMETAPI_KEY"]},
                json={
                "model_name": "kolors-virtual-try-on-v1",
                "human_image": "https://your-image-host/person.jpg",
                "cloth_image": "https://your-image-host/garment.jpg"
            },
            )

            result = response.json()
            print(result["code"], result.get("data", {}).get("task_id"))
        - lang: JavaScript
          label: Default
          source: >
            const response = await
            fetch("https://api.cometapi.com/kling/v1/images/kolors-virtual-try-on",
            {
              method: "POST",
              headers: {
                Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
                "Content-Type": "application/json",
              },
              body: JSON.stringify({
                "model_name": "kolors-virtual-try-on-v1",
                "human_image": "https://your-image-host/person.jpg",
                "cloth_image": "https://your-image-host/garment.jpg"
            }),

            });


            const result = await response.json();

            console.log(result.code, result.data?.task_id);
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: Bearer token authentication. Use your CometAPI key.

````