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

# Expand an image with Kling

> Use Kling Image Expansion API via CometAPI to expand images beyond their original borders with controllable outpainting.

Use this endpoint to outpaint an image beyond its original borders when you need more canvas around the source content.

## How the expansion ratios work

* Each ratio is measured against the original width or height
* The combined expanded canvas must still stay within Kling's documented area limits
* Start with small values such as `0.1` on each side before attempting larger expansions

## Request flow

* Send all four ratio fields, using `0` for any side you do not want to expand
* Save the returned `task_id`
* Poll the matching Kling image query path until the task reaches a terminal state

<Tip>
  For the complete parameter reference, see the [official Kling documentation](https://kling.ai/document-api/api/image/common/outpainting).
</Tip>


## OpenAPI

````yaml api/openapi/image/kling/post-image-expansion.openapi.json POST /kling/v1/images/editing/expand
openapi: 3.1.0
info:
  title: Kling Image Expansion API
  version: 1.0.0
servers:
  - url: https://api.cometapi.com
security:
  - bearerAuth: []
paths:
  /kling/v1/images/editing/expand:
    post:
      summary: Expand an image with Kling
      operationId: kling_image_expansion
      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:
                - image
                - up_expansion_ratio
                - down_expansion_ratio
                - left_expansion_ratio
                - right_expansion_ratio
              properties:
                image:
                  type: string
                  description: >-
                    Source image to expand. 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.
                up_expansion_ratio:
                  type: number
                  description: >-
                    Upward expansion ratio as a multiple of the original image
                    dimension. Range: 0-2. Send `0` for sides you do not want to
                    expand.
                  minimum: 0
                  maximum: 2
                down_expansion_ratio:
                  type: number
                  description: >-
                    Downward expansion ratio as a multiple of the original image
                    dimension. Range: 0-2. Send `0` for sides you do not want to
                    expand.
                  minimum: 0
                  maximum: 2
                left_expansion_ratio:
                  type: number
                  description: >-
                    Leftward expansion ratio as a multiple of the original image
                    dimension. Range: 0-2. Send `0` for sides you do not want to
                    expand.
                  minimum: 0
                  maximum: 2
                right_expansion_ratio:
                  type: number
                  description: >-
                    Rightward expansion ratio as a multiple of the original
                    image dimension. Range: 0-2. Send `0` for sides you do not
                    want to expand.
                  minimum: 0
                  maximum: 2
                prompt:
                  type: string
                  description: >-
                    Optional text prompt to guide the expanded area content.
                    Maximum 2500 characters.
                'n':
                  type: integer
                  description: 'Number of expanded images to generate. Range: 1-9.'
                  minimum: 1
                  maximum: 9
                  default: 1
                watermark_info:
                  type: object
                  description: Watermark options.
                  properties:
                    enabled:
                      type: boolean
                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:
                image: https://your-image-host/source.jpg
                up_expansion_ratio: 0.1
                down_expansion_ratio: 0.1
                left_expansion_ratio: 0.1
                right_expansion_ratio: 0.1
                prompt: extend the soft studio background
                'n': 1
            examples:
              Default:
                summary: Expand an image on all four sides
                value:
                  image: https://your-image-host/source.jpg
                  up_expansion_ratio: 0.1
                  down_expansion_ratio: 0.1
                  left_expansion_ratio: 0.1
                  right_expansion_ratio: 0.1
                  prompt: extend the soft studio background
                  'n': 1
      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/editing/expand \
              -H "Authorization: Bearer $COMETAPI_KEY" \
              -H "Content-Type: application/json" \
              -d '{
              "image": "https://your-image-host/source.jpg",
              "up_expansion_ratio": 0.1,
              "down_expansion_ratio": 0.1,
              "left_expansion_ratio": 0.1,
              "right_expansion_ratio": 0.1,
              "prompt": "extend the soft studio background",
              "n": 1
            }'
        - lang: Python
          label: Default
          source: |
            import os
            import requests

            response = requests.post(
                "https://api.cometapi.com/kling/v1/images/editing/expand",
                headers={"Authorization": "Bearer " + os.environ["COMETAPI_KEY"]},
                json={
                "image": "https://your-image-host/source.jpg",
                "up_expansion_ratio": 0.1,
                "down_expansion_ratio": 0.1,
                "left_expansion_ratio": 0.1,
                "right_expansion_ratio": 0.1,
                "prompt": "extend the soft studio background",
                "n": 1
            },
            )

            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/editing/expand", {
              method: "POST",
              headers: {
                Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
                "Content-Type": "application/json",
              },
              body: JSON.stringify({
                "image": "https://your-image-host/source.jpg",
                "up_expansion_ratio": 0.1,
                "down_expansion_ratio": 0.1,
                "left_expansion_ratio": 0.1,
                "right_expansion_ratio": 0.1,
                "prompt": "extend the soft studio background",
                "n": 1
            }),

            });


            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.

````