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

# Create speech

> Sử dụng CometAPI POST /v1/audio/speech để chuyển đổi văn bản thành âm thanh với model chuyển văn bản thành giọng nói và định dạng đầu ra được chọn.

Sử dụng endpoint này để chuyển văn bản thành tệp âm thanh thông qua audio API tương thích OpenAI. Endpoint này phù hợp cho thuyết minh, lời nhắc thoại ngắn, tính năng đọc thành tiếng và các quy trình khác nơi ứng dụng của bạn đã có văn bản và cần đầu ra giọng nói.

## Yêu cầu đầu tiên

Bắt đầu với ba trường: `model`, `input`, và `voice`. Hãy giữ yêu cầu đầu tiên ngắn gọn để bạn có thể xác minh xác thực, định dạng âm thanh và cách xử lý tệp trước khi tinh chỉnh tốc độ hoặc định dạng đầu ra.

## Đọc phản hồi

Phản hồi là âm thanh nhị phân, không phải JSON. Trong các ví dụ SDK, hãy ghi phản hồi vào một tệp như `output.mp3`. Trong các HTTP client trực tiếp, hãy lưu phần thân phản hồi và đặt phần mở rộng tệp khớp với `response_format` đã yêu cầu.

## Các bước tiếp theo

* Sử dụng [Create Transcription](/api/audio/create-transcription) khi bạn cần chuyển giọng nói trở lại thành văn bản.
* Sử dụng [Create Translation](/api/audio/create-translation) khi bạn cần văn bản tiếng Anh từ âm thanh không phải tiếng Anh.


## OpenAPI

````yaml api/openapi/audio/post-create-speech.openapi.json POST /v1/audio/speech
openapi: 3.1.0
info:
  title: Create speech API
  version: 1.0.0
servers:
  - url: https://api.cometapi.com
security:
  - bearerAuth: []
paths:
  /v1/audio/speech:
    post:
      summary: Create speech
      operationId: create_speech
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - model
                - input
                - voice
              properties:
                model:
                  type: string
                  description: >-
                    The TTS model to use. Choose a current speech model from the
                    [Models page](/overview/models).
                  default: tts-1
                input:
                  type: string
                  description: >-
                    The text to generate audio for. Maximum length is 4096
                    characters.
                  maxLength: 4096
                voice:
                  type: string
                  description: The voice to use for speech synthesis.
                  enum:
                    - alloy
                    - ash
                    - ballad
                    - coral
                    - echo
                    - fable
                    - onyx
                    - nova
                    - sage
                    - shimmer
                  default: alloy
                response_format:
                  type: string
                  description: The audio output format.
                  enum:
                    - mp3
                    - opus
                    - aac
                    - flac
                    - wav
                    - pcm
                  default: mp3
                speed:
                  type: number
                  description: >-
                    The speed of the generated audio. Select a value between
                    0.25 and 4.0.
                  minimum: 0.25
                  maximum: 4
                  default: 1
            examples:
              Default:
                summary: Standard TTS (tts-1)
                value:
                  model: tts-1
                  input: The quick brown fox jumped over the lazy dog.
                  voice: alloy
              gpt_4o_mini_tts:
                summary: GPT-4o mini TTS (gpt-4o-mini-tts)
                value:
                  model: gpt-4o-mini-tts
                  input: The quick brown fox jumped over the lazy dog.
                  voice: alloy
      responses:
        '200':
          description: The audio file content.
          content:
            audio/mpeg:
              schema:
                type: string
                format: binary
      x-codeSamples:
        - lang: python
          label: Create speech
          source: |-
            import os
            from openai import OpenAI

            client = OpenAI(
                api_key=os.environ["COMETAPI_KEY"],
                base_url="https://api.cometapi.com/v1"
            )

            response = client.audio.speech.create(
                model="tts-1",
                voice="alloy",
                input="The quick brown fox jumped over the lazy dog."
            )

            response.stream_to_file("output.mp3")
        - lang: javascript
          label: Create speech
          source: |-
            import OpenAI from "openai";
            import fs from "fs";

            const client = new OpenAI({
              apiKey: process.env.COMETAPI_KEY,
              baseURL: "https://api.cometapi.com/v1"
            });

            const response = await client.audio.speech.create({
              model: "tts-1",
              voice: "alloy",
              input: "The quick brown fox jumped over the lazy dog."
            });

            const buffer = Buffer.from(await response.arrayBuffer());
            fs.writeFileSync("output.mp3", buffer);
        - lang: shell
          label: Create speech
          source: |-
            curl -X POST https://api.cometapi.com/v1/audio/speech \
              -H "Authorization: Bearer $COMETAPI_KEY" \
              -H "Content-Type: application/json" \
              -d '{
                "model": "tts-1",
                "input": "The quick brown fox jumped over the lazy dog.",
                "voice": "alloy"
              }' \
              --output output.mp3
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: Bearer token authentication. Use your CometAPI key.

````