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.
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
Obtain an API key in the console and set it as the DASHSCOPE_API_KEY environment variable.
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-intl.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()
}
API reference
Constructor options
Constructor options for SpeechSynthesizer:
Parameter | Type | Required | Description |
|---|---|---|---|
|
| No | API key. Defaults to |
|
| No | Workspace ID. Defaults to |
|
| No | Set the service hostname to |
|
| 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 | 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 |
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. |