All Products
Search
Document Center

Alibaba Cloud Model Studio:Qwen-Audio-ASR-Message Node.js SDK

Last Updated:Nov 11, 2026

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

apiKey

string

No

API key. Defaults to DASHSCOPE_API_KEY.

workspaceId

string

No

Workspace ID. Defaults to DASHSCOPE_WORKSPACE_ID.

baseUrl

string

No

Set the service hostname to dashscope-intl.aliyuncs.com (Singapore).

model

string

Yes

Set the model ID to qwen-audio-3.1-asr-flash-message.

format

'pcm' | 'wav' | 'mp3' | 'opus' | 'speex' | 'aac' | 'amr'

No

Audio format. Default: pcm. WAV uses PCM encoding, Opus/Speex use Ogg containers, and AMR uses AMR-NB.

sampleRate

number

No

Audio sample rate. Use 16000 Hz.

vocabularyId

string

No

Precompiled vocabulary ID.

languageHints

string[]

No

Recognition language hints. Up to the first 4 values take effect. Language is detected automatically if omitted.

disfluencyRemovalEnabled

boolean

No

Remove disfluencies and polish results. Default: false.

maxSentenceSilence

number

No

VAD sentence-ending silence threshold in ms: [200,6000]; default: 800.

heartbeat

boolean

No

Silence keepalive. Default: false; keep sending silent audio when enabled.

context

AsrContextMessage[]

No

Recognition context. Message format: { role, content: [{ type: "input_text", text }] }, with role set to user or assistant. Up to 5 messages per role and fewer than 400 characters in total, ordered by conversation turn.

parameters

Partial<RecognitionParameters>

No

Additional recognition parameters, such as vad_model, keep_dialect, and vocabulary.

connectTimeoutMs

number

No

Connection timeout in ms. Default: 10000.

taskTimeoutMs

number

No

Start/finish acknowledgement timeout in ms. Default: 60000; 0 disables the timeout.

parameters.vad_model

'near_meeting_16k' | 'far_field_meeting_16k'

No

Near-field or far-field VAD. Default: far_field_meeting_16k.

parameters.keep_dialect

boolean

No

Preserve dialect expressions. Default: false.

parameters.vocabulary

Record<string, number>

No

Instant vocabulary: words as keys and weights in [1,5] or 50 as values.

parameters.speech_noise_threshold

number

No

VAD sensitivity threshold: [-1,1].

parameters.intermediate_result_enabled

boolean

No

Return intermediate recognition results. Default: false.

Main methods

Method

Return value

Description

start()

Promise<void>

Start recognition; send audio after it resolves.

sendAudio(audio: Buffer | Uint8Array)

void

Send audio frames in the configured format and sample rate.

updateContext(context: AsrContextMessage[])

void

Update recognition context after starting and before finishing input.

finish()

Promise<void>

Finish audio input and wait for completion. Text arrives through callbacks.

close(code?: number, reason?: string)

void

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

started

{ taskId: string }

Task has started and accepts input.

result

{ text: string; sentence_end: boolean; sentence_begin?: boolean; words?: RecognitionWord[] }

Intermediate or final sentence, excluding heartbeat results.

sentenceEnd

{ text: string; sentence_end: boolean; sentence_begin?: boolean; words?: RecognitionWord[] }

Final sentence with sentence_end === true.

finished

{ taskId: string; duration?: number; usage?: RecognitionUsage }

Task has finished and local task state is cleared.

failed / error

DashScopeError

Call error with code, message, and request identifiers.

close

{ code: number; reason: string }

Underlying connection-close notification.

event

ServerEvent

Raw service JSON message.