CosyVoice HTTP API

Updated at:

Synthesize text into speech and receive a complete audio file URL or audio chunks over SSE.

NoteThe examples use China (Beijing). Replace {WorkspaceId} with your workspace ID and set DASHSCOPE_API_KEY to an API key for that region.

POSThttps://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/audio/tts/SpeechSynthesizer

Request headers

AuthorizationstringRequired

Use the API key for the endpoint region, in the format Bearer <API Key>.

Content-TypestringRequired

The media type of the request body. Fixed to application/json.

X-DashScope-SSEstringOptional

Select how to receive the synthesized audio.

  • Omit the header to receive one JSON response when synthesis finishes.
  • Set the header to enable to stream synthesis results over SSE.

Request body

modelstringRequired

Speech synthesis model ID.

inputobjectRequired

The input parameters.

textstringRequired

Text to synthesize. Pass plain text directly. For SSML input, set enable_ssml to true.

To read a mathematical expression in Chinese, enclose the LaTeX formula in $ delimiters within the text. No additional parameter is required. For example, $\frac{2}{7}$ is read as two sevenths in Chinese. In a JSON string, escape each backslash as \\. For fraction and radical notation, see LaTeX tags and symbols.

voicestringRequired

The ID of the voice that model uses for synthesis.

  • System voice: Select a voice listed for your model in the voice list.
  • Custom voice: Use the ID returned by voice cloning or voice design. Set model to the target model you specified when creating the voice.

Note

  • cosyvoice-v3.5-plus and cosyvoice-v3.5-flash do not support system voices.
  • cosyvoice-v2 does not support voice design. Use voice cloning to create a custom voice.
formatstringOptional

Audio encoding. Valid values: mp3, pcm, wav, and opus.

Default: mp3

sample_rateintegerOptional

Audio sample rate in Hz. Valid values: 8000, 12000, 16000, 22050, 24000, 44100, and 48000.

Default: 22050

NoteWhen format is opus, the supported sample rates are 8000, 12000, 16000, 24000, and 48000 Hz.

bit_rateintegerOptional

NoteApplies only when format is set to opus.

Audio bitrate in kbps. Range: [6, 510].

Default: 32

volumeintegerOptional

Volume. Range: [0, 100].

Default: 50

ratefloatOptional

Speaking rate. Range: [0.5, 2.0].

Default: 1.0

pitchfloatOptional

Pitch. Range: [0.5, 2.0].

Default: 1.0

enable_ssmlbooleanOptional

For SSML, set enable_ssml to true. For supported models, voices, and tags, see SSML.

word_timestamp_enabledbooleanOptional

NoteApplies only to streaming output.

Set to true to receive word-level start and end times in output.sentence.words. Cloned voices support timestamps. For a system voice, check timestamp support in the voice list.

Default: false

seedintegerOptional

Random seed. Range: [0, 65535]. With the same model version, text, voice, and other parameters, the same seed reproduces the same synthesis result.

Default: 0

language_hintsarray[string]Optional

Specifies the language in which to read the input text. Use this hint if numbers, abbreviations, or symbols are pronounced in the wrong language, or if speech in a less common language sounds unnatural.

For example, to have @ read as “at” in English text, pass ["en"]. Specify one language code per request. If you pass several, only the first takes effect.

Supported language codes

  • zh: Chinese
  • en: English
  • fr: French
  • de: German
  • ja: Japanese
  • ko: Korean
  • ru: Russian
  • pt: Portuguese
  • th: Thai
  • id: Indonesian
  • vi: Vietnamese
  • es: Spanish
  • it: Italian
  • ms: Malay
  • fil: Filipino
  • ar: Arabic

When using a cloned voice, choose the language of the text you want to synthesize. To specify the language of the recording used for cloning, set it when you create the voice.

instructionstringOptional

An instruction controlling dialect, emotion, or role. See Instruction control for model and voice support and usage.

enable_aigc_tagbooleanOptional

NoteSupported only by cosyvoice-v3-flash, cosyvoice-v3-plus, and cosyvoice-v2.

Whether to embed an invisible AIGC identifier in WAV, MP3, or Opus audio.

Default: false

aigc_propagatorstringOptional

NoteSupported only by cosyvoice-v3-flash, cosyvoice-v3-plus, and cosyvoice-v2 and effective only when enable_aigc_tag is true.

The ContentPropagator field of the identifier, identifying the distributor.

Default: Alibaba Cloud UID

aigc_propagate_idstringOptional

NoteSupported only by cosyvoice-v3-flash, cosyvoice-v3-plus, and cosyvoice-v2 and effective only when enable_aigc_tag is true.

The PropagateID field of the identifier, identifying this distribution operation.

Default: Request ID of this request

enable_markdown_filterbooleanOptional

Notecosyvoice-v2 does not support this feature.

Whether to enable Markdown filtering. When enabled, the service converts Markdown into text suitable for reading aloud before synthesis.

Default: false

Response body

request_idstring

The unique request identifier.

outputobject

The synthesis result. For streaming responses, parse the data: line of each SSE event as JSON, then read the output object.

finish_reasonstring

The reason the synthesis task stopped.

  • "null" — Synthesis is in progress.
  • "stop" — The task finished normally. When streaming, stop reading events after receiving this value.
typestring

NoteThis field is returned only for sentence events in streaming responses.

The type of a sentence-level synthesis event. Use it to determine how to handle the text or audio for the current sentence.

  • sentence-begin — Synthesis of the sentence has started. Read the sentence text from output.original_text when present.
  • sentence-synthesis — The event carries one audio chunk for the current sentence in output.audio.data. A sentence can produce multiple consecutive chunks.
  • sentence-end — Synthesis of the sentence has finished. The event returns sentence text and cumulative usage up to this point. Continue reading events until output.finish_reason is "stop".
original_textstring

A sentence segmented from the input text. May be absent for the final sentence.

sentenceobject

Sentence information.

indexinteger

The sentence index, starting at 0.

wordsarray[object]

Words in the sentence and their timestamps.

textstring

A word or character.

begin_indexinteger

The start position in the sentence, starting at 0.

end_indexinteger

The end position in the sentence, starting at 1.

begin_timeinteger

The audio start timestamp of the word, in milliseconds.

end_timeinteger

The audio end timestamp of the word, in milliseconds.

audioobject

The synthesized audio.

datastring

In streaming output, each sentence-synthesis event carries one Base64-encoded audio chunk in this field. Decode each chunk separately and append the decoded bytes to the same audio file in the order received.

In non-streaming output, this field is empty. Download the complete audio from output.audio.url.

urlstring

The complete audio file URL, valid for 24 hours.

idstring

The synthesized audio identifier.

expires_atinteger

The expiry timestamp of url.

usageobject

Usage is cumulative. For streaming requests, use the usage from the final event. Do not add the values from successive events.

charactersinteger

Cumulative billable characters.

Non-streaming

curl "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/audio/tts/SpeechSynthesizer" \
  -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
  -H "Content-Type: application/json" \
  --data '{
  "model": "cosyvoice-v3-flash",
  "input": {
    "text": "Welcome to Model Studio.",
    "voice": "longanyang",
    "format": "wav",
    "sample_rate": 24000
  }
}'
{
  "output": {
    "audio": {
      "data": "",
      "id": "audio_704d43de-969c-9b55-adaa-ed64b64b9c42",
      "expires_at": 1795794025,
      "url": "https://example.com/output.wav"
    },
    "finish_reason": "stop"
  },
  "usage": {
    "characters": 24
  },
  "request_id": "704d43de-969c-9b55-adaa-ed64b64b9c42"
}

Streaming (SSE)

curl --no-buffer "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/audio/tts/SpeechSynthesizer" \
  -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
  -H "Content-Type: application/json" \
  -H "X-DashScope-SSE: enable" \
  --data '{
  "model": "cosyvoice-v3-flash",
  "input": {
    "text": "Welcome to Model Studio.",
    "voice": "longanyang",
    "format": "wav",
    "sample_rate": 24000
  }
}'
event:result
data:{"output":{"type":"sentence-begin","original_text":"Welcome to Model Studio.","normalized_text":"Welcome to Model Studio.","audio":{"data":"","id":"audio_716e8dda-4835-938d-8d67-25a8e128f8e8","expires_at":1795794026},"finish_reason":"null","sentence":{"index":0,"words":[]}},"request_id":"716e8dda-4835-938d-8d67-25a8e128f8e8"}

event:result
data:{"output":{"type":"sentence-synthesis","audio":{"data":"<base64 audio chunk>","id":"audio_716e8dda-4835-938d-8d67-25a8e128f8e8","expires_at":1795794026},"finish_reason":"null","sentence":{"index":0,"words":[]}},"request_id":"716e8dda-4835-938d-8d67-25a8e128f8e8"}

event:result
data:{"output":{"type":"sentence-end","original_text":"Welcome to Model Studio.","normalized_text":"Welcome to Model Studio.","audio":{"data":"","id":"audio_716e8dda-4835-938d-8d67-25a8e128f8e8","expires_at":1795794026},"finish_reason":"null","sentence":{"index":0,"words":[]}},"usage":{"characters":24},"request_id":"716e8dda-4835-938d-8d67-25a8e128f8e8"}

event:result
data:{"output":{"audio":{"data":"","id":"audio_716e8dda-4835-938d-8d67-25a8e128f8e8","expires_at":1795794026,"url":"https://example.com/output.wav"},"finish_reason":"stop"},"usage":{"characters":24},"request_id":"716e8dda-4835-938d-8d67-25a8e128f8e8"}