Qwen-Audio-TTS Node.js SDK
Use the speech Node.js SDK to call Qwen-Audio-TTS with complete or streaming text input and handle audio, word timestamps, and usage events.
Prerequisites
Obtain an API key in the console and set it as the DASHSCOPE_API_KEY environment variable.
Install the SDK
Requires Node.js 18 or later. Download the SDK 0.1.0 package, then run this command in your project:
npm install ./dashscope-sdk-nodejs-0.1.0.tgz
Quick start
Save the following example as example.mjs and run node example.mjs.
import { writeFile } from 'node:fs/promises'
import { SpeechSynthesizer } from 'dashscope-sdk-nodejs/tts'
const synth = new SpeechSynthesizer({
apiKey: process.env.DASHSCOPE_API_KEY,
workspaceId: process.env.DASHSCOPE_WORKSPACE_ID,
baseUrl: 'dashscope.aliyuncs.com',
model: 'qwen-audio-3.1-tts-flash',
voice: 'longanhuan_v3.1',
format: 'mp3',
sampleRate: 24000,
})
synth.on('error', err => console.error(err.code, err.message))
try {
const audio = await synth.synthesize('Hello, welcome to Qwen Audio.')
await writeFile('output.mp3', audio)
} finally {
synth.close()
}
Request parameters
Constructor options for SpeechSynthesizer:
Parameter | Type | Required | Description |
|---|---|---|---|
|
| No | API key. Defaults to |
|
| No | Workspace ID. Defaults to |
|
| No | Service hostname. Default: |
|
| No | Model ID. Default: |
|
| Yes | Voice ID. Choose a voice compatible with the model. |
|
| No | Audio encoding. Uses the service default if omitted. |
|
| No | Audio sample rate in Hz. Must match the encoding; uses the service default if omitted. |
|
| No | Volume: |
|
| No | Speech rate: |
|
| No | Pitch: |
|
| No | Return word timestamps. Default: |
|
| No | NoteFor the multilingual and dialect voices of qwen-audio-3.1-tts-flash, this parameter specifies the language or dialect. See multilingual and dialect voice settings for details. Target-language hints. Only the first element takes effect. |
|
| No | Synthesis instructions for dialect, emotion, or role. |
|
| No | Additional synthesis parameters, such as |
|
| No | Connection timeout in ms. Default: |
|
| No | Start/finish acknowledgement timeout in ms. Default: |
|
| No | Enable SSML. Send the complete nonempty SSML text once per task. See limits. |
|
| No | MP3/Opus bit rate in kbps. Parameter range: |
|
| No | Random seed: |
|
| No | Hot-fix configuration passed as |
|
| No | Whether to enable Markdown filtering. When enabled, the service converts Markdown into text suitable for reading aloud before synthesis. Default: |
API reference
Main methods
Method | Return value | Description |
|---|---|---|
|
| Synthesize complete text and return audio. Up to 20000 characters. |
|
| Start a streaming synthesis task, then send text. |
|
| Send a text chunk: up to 20000 characters per call and 200000 per task. |
|
| Finish text input, wait for synthesis, and return the complete audio. |
|
| Request synthesis cancellation. |
|
| Close the connection and release task resources. |
Callback events
Register callbacks with on(event, listener). See server events for the full event fields.
Event | Payload | Description |
|---|---|---|
|
| Task has started and accepts input. |
|
| Binary audio chunk; may fire multiple times. |
|
| Synthesis of a sentence has started. |
|
| Sentence synthesis is complete; includes optional word timestamps and raw usage. |
|
| Task has finished and local task state is cleared. |
|
| Call error with |
|
| Underlying connection-close notification. |
|
| Raw service JSON message. |