> ## 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 Text to Audio API，將文字 Prompt 轉換為音訊，並支援語音風格控制、時長選項與可用於串流的輸出。

使用此端點可透過 Kling 根據文字 Prompt 產生一段短音訊片段。

## 呼叫前準備

* 保持 `prompt` 簡潔明確
* 先將 `duration` 設定在 3 到 10 秒之間
* 將 `callback_url` 和 `external_task_id` 視為可選的整合輔助欄位，而非核心需求

## 任務流程

<Steps>
  <Step title="提交音訊任務">
    送出文字 Prompt 與時長，然後儲存回傳的任務 id。
  </Step>

  <Step title="輪詢任務">
    使用 Kling 音訊查詢路徑，持續透過[取得 Kling 任務](./individual-queries)查詢，直到任務進入終止狀態。
  </Step>

  <Step title="儲存音訊輸出">
    如果你需要在供應商交付視窗之外保留最終音訊資產，請將其持久化儲存。
  </Step>
</Steps>

<Tip>
  如需完整的參數參考，請參閱[官方 Kling 文件](https://app.klingai.com/global/dev/document-api/apiReference/model/textToAudio)。
</Tip>


## OpenAPI

````yaml api/openapi/video/kling/post-text-to-audio.openapi.json POST /kling/v1/audio/text-to-audio
openapi: 3.1.0
info:
  title: Text to Audio API
  version: 1.0.0
  description: Create a Kling audio-generation task from a text prompt.
servers:
  - url: https://api.cometapi.com
security:
  - bearerAuth: []
paths:
  /kling/v1/audio/text-to-audio:
    post:
      summary: Create a Kling text-to-audio task
      description: >-
        Submit a text prompt and duration, then poll the returned task id
        through the generic Kling query route.
      operationId: text_to_audio
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - prompt
                - duration
              properties:
                prompt:
                  type: string
                  description: >-
                    Text prompt describing the audio to generate. Max 200
                    characters.
                duration:
                  type: integer
                  description: >-
                    Duration of the generated audio in seconds. Range: 3.0–10.0,
                    supports one decimal place.
                external_task_id:
                  type: string
                  description: >-
                    Optional user-defined task ID for your own tracking. Does
                    not replace the system-generated task ID. Must be unique per
                    account.
                callback_url:
                  type: string
                  description: >-
                    Webhook URL for task status notifications. The server sends
                    a callback when the task status changes.
              default:
                prompt: Soft ambient ocean waves at sunrise.
                duration: 5
            examples:
              Validated request:
                summary: Validated request
                value:
                  prompt: Soft ambient ocean waves at sunrise.
                  duration: 5
      responses:
        '200':
          description: Task accepted.
          content:
            application/json:
              schema:
                type: object
                required:
                  - code
                  - message
                  - data
                properties:
                  code:
                    type: integer
                    description: |
                      Error code; specific error code definition
                  message:
                    type: string
                    description: |
                      Error message
                  data:
                    type: object
                    required:
                      - task_id
                      - task_status
                      - created_at
                      - updated_at
                    properties:
                      task_id:
                        type: string
                        description: |
                          Task ID, system-generated
                      task_status:
                        type: string
                        description: >
                          Task status, enum values: submitted, processing,
                          succeed, failed
                      created_at:
                        type: integer
                        description: |+
                          Task creation time, Unix timestamp, in ms

                      updated_at:
                        type: integer
                        description: |
                          Task update time, Unix timestamp, in ms
                example:
                  code: 0
                  message: SUCCEED
                  data:
                    task_id: '861254119619698760'
                    task_status: submitted
                    task_info: {}
                    created_at: 1773367502225
                    updated_at: 1773367502225
      x-codeSamples:
        - lang: Shell
          label: Default
          source: |
            curl https://api.cometapi.com/kling/v1/audio/text-to-audio \
              -H "Authorization: Bearer $COMETAPI_KEY" \
              -H "Content-Type: application/json" \
              -d '{
                  "prompt": "Soft ambient ocean waves at sunrise.",
                  "duration": 5
                }'
        - lang: Python
          label: Default
          source: >
            import os

            import requests


            response = requests.post(
                "https://api.cometapi.com/kling/v1/audio/text-to-audio",
                headers={"Authorization": "Bearer " + os.environ["COMETAPI_KEY"]},
                json={
                        "prompt": "Soft ambient ocean waves at sunrise.",
                        "duration": 5
                },
            )


            result = response.json()

            print(result["code"], result.get("data", {}).get("task_id"))  # code
            0 means submitted; poll the matching query endpoint until
            task_status is succeed
        - lang: JavaScript
          label: Default
          source: >
            const response = await
            fetch("https://api.cometapi.com/kling/v1/audio/text-to-audio", {
                method: "POST",
                headers: {
                    Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
                    "Content-Type": "application/json",
                },
                body: JSON.stringify({
                        "prompt": "Soft ambient ocean waves at sunrise.",
                        "duration": 5
                    }),
            });


            const result = await response.json();

            console.log(result.code, result.data?.task_id); // code 0 means
            submitted; poll the matching query endpoint until task_status is
            succeed
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: Bearer token authentication. Use your CometAPI key.

````