Use the speech Node.js SDK to call Qwen-Audio-ASR-Message, handle recognition results, and update context during a task.
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.
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 { readFile } from 'node:fs/promises'
import { setTimeout as sleep } from 'node:timers/promises'
import { RecognitionClient } from 'dashscope-sdk-nodejs/asr'
const pcm = await readFile('input.pcm')
const asr = new RecognitionClient({
apiKey: process.env.DASHSCOPE_API_KEY,
workspaceId: process.env.DASHSCOPE_WORKSPACE_ID,
baseUrl: 'dashscope-intl.aliyuncs.com',
model: 'qwen-audio-3.1-asr-flash-message',
format: 'pcm',
sampleRate: 16000,
})
asr.on('sentenceEnd', sentence => console.log('Final sentence:', sentence.text))
asr.on('error', err => console.error(err.code, err.message))
try {
await asr.start()
for (let offset = 0; offset < pcm.length; offset += 3200) {
asr.sendAudio(pcm.subarray(offset, offset + 3200))
await sleep(100)
}
await asr.finish()
} finally {
asr.close()
}
API reference
Constructor options
Constructor options for RecognitionClient:
Parameter | Type | Required | Description |
|---|---|---|---|
|
| No | API key. Defaults to |
|
| No | Workspace ID. Defaults to |
|
| No | Set the service hostname to |
|
| Yes | Set the model ID to |
|
| No | Audio format. Default: |
|
| No | Audio sample rate. Use |
|
| No | Precompiled vocabulary ID. |
|
| No | Recognition language hints. Up to the first 4 values take effect. Language is detected automatically if omitted. |
|
| No | Remove disfluencies and polish results. Default: |
|
| No | VAD sentence-ending silence threshold in ms: |
|
| No | Silence keepalive. Default: |
|
| No | Recognition context. Message format: |
|
| No | Additional recognition parameters, such as |
|
| No | Connection timeout in ms. Default: |
|
| No | Start/finish acknowledgement timeout in ms. Default: |
|
| No | Near-field or far-field VAD. Default: |
|
| No | Preserve dialect expressions. Default: |
|
| No | Instant vocabulary: words as keys and weights in |
|
| No | VAD sensitivity threshold: |
|
| No | Return intermediate recognition results. Default: |
Main methods
Method | Return value | Description |
|---|---|---|
|
| Start recognition; send audio after it resolves. |
|
| Send audio frames in the configured format and sample rate. |
|
| Update recognition context after starting and before finishing input. |
|
| Finish audio input and wait for completion. Text arrives through callbacks. |
|
| 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. |
|
| Intermediate or final sentence, excluding heartbeat results. |
|
| Final sentence with |
|
| Task has finished and local task state is cleared. |
|
| Call error with |
|
| Underlying connection-close notification. |
|
| Raw service JSON message. |