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 { 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()
}