Use the speech Node.js SDK to stream audio to Qwen-Audio-ASR-Streaming and handle recognition results, context, and usage.
Install the SDK
Requires Node.js 18 or later. Download the SDK 0.1.0 package, then run this command in your project:
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:
example.mjs and run node example.mjs.
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 | Service hostname. Default: dashscope.aliyuncs.com (Beijing). |
model | string | No | Model ID. Default: qwen-audio-3.1-asr-flash-streaming. |
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 | Actual audio sample rate in Hz. Default: 16000. |
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. |
multiThresholdModeEnabled | boolean | No | Multithreshold mode to avoid overly long VAD segments. 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]. |
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. |
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. |