Use the speech Node.js SDK to configure Qwen-Audio-Realtime sessions, exchange audio, and handle transcription and function calling.
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
Download the sample audio as input.mp3 and convert it to 16 kHz, 16-bit, mono PCM:
ffmpeg -i input.mp3 -f s16le -ac 1 -ar 16000 input.pcm
Save the following example as example.mjs and run node example.mjs.
import { readFileSync, writeFileSync } from 'node:fs'
import { setTimeout as sleep } from 'node:timers/promises'
import { RealtimeClient } from 'dashscope-sdk-nodejs/realtime'
const pcm = readFileSync('input.pcm')
const client = new RealtimeClient({
apiKey: process.env.DASHSCOPE_API_KEY,
workspaceId: process.env.DASHSCOPE_WORKSPACE_ID,
baseUrl: 'dashscope-intl.aliyuncs.com',
model: 'qwen-audio-3.1-realtime-plus',
voice: 'longanqian_v3.1',
turnDetection: null,
inputTranscription: { language: 'zh' },
})
const audio = []
function fail(err) {
console.error(err.code, err.message)
process.exitCode = 1
client.close()
}
const timer = setTimeout(() => fail(new Error('Response timed out')), 60000)
client.on('close', () => clearTimeout(timer))
client.on('error', fail)
client.on('audio', chunk => audio.push(chunk))
client.on('inputTranscript', ({ transcript }) => console.log('User:', transcript))
client.on('outputTranscript', ({ transcript }) => console.log('Assistant:', transcript))
client.once('sessionUpdated', () => {
;(async () => {
for (let offset = 0; offset < pcm.length; offset += 3200) {
client.appendAudio(pcm.subarray(offset, offset + 3200))
await sleep(100)
}
client.commit()
client.createResponse()
})().catch(fail)
})
client.once('responseDone', event => {
if (event.response?.status !== 'completed') {
fail(new Error('Response status: ' + event.response?.status))
return
}
writeFileSync('output.pcm', Buffer.concat(audio))
client.close()
})
try {
await client.connect()
} catch (err) {
fail(err)
}
Request parameters
Constructor options for RealtimeClient:
Parameter | Type | Required | Description |
|---|---|---|---|
|
| No | API key. Defaults to |
|
| No | Workspace ID. Defaults to |
|
| No | Set the service hostname to |
|
| Yes | Model ID. |
|
| No | Voice ID. Uses the service default if omitted. |
|
| No | System instructions for role, style, and other behavior. |
|
| No | Default: |
|
| No | Output modalities. Default: |
|
| No | History turns: |
|
| No | Tool definitions. The application executes tools and validates their arguments. |
|
| No | Input transcription settings, such as |
|
| No | Additional session settings, such as |
|
| No | Connection timeout in ms. Default: |
API reference
Main methods
Method | Return value | Description |
|---|---|---|
|
| Connect and send session settings. |
|
| Update session settings; confirm with |
|
| Send 16 kHz, 16-bit, mono PCM audio. |
|
| Commit the audio buffer in manual mode. |
|
| Clear the uncommitted audio buffer. |
|
| Request a model response. |
|
| Cancel the current response and keep the session. |
|
| Add conversation history or tool results. |
|
| Submit a tool-result string. |
|
| Delete a conversation item. |
|
| Retrieve an item; receive it through |
|
| Send a raw client event. |
|
| Close the session and WebSocket connection. |
Callback events
Register callbacks with on(event, listener). See server events for the full event fields.
Event | Payload | Description |
|---|---|---|
|
| Connection established and initial configuration sent. |
|
| Session creation or configuration confirmation. |
|
| Binary audio chunk; may fire multiple times. |
|
| Incremental output-audio transcript. |
|
| Complete output-audio transcript. |
|
| Incremental or complete text in text-only modality. |
|
|
|
|
| Final input-audio transcript. |
|
| User speech started or stopped. |
|
| Response started or ended. Check whether |
|
| Complete function-call arguments. The application parses and validates the |
|
| Current or final ambient-audio transcript, excluded from conversation context. |
|
| Raw service JSON message. |
|
| Connection or service error. |
|
| Underlying connection-close notification. |