Quickstart: Recognize speech with the Speech SDK for Node.js

This article shows you how to create a Node.js project by using the JavaScript binding of the Speech SDK for Azure Cognitive Services to transcribe speech to text. The application is based on the Speech SDK for JavaScript.


Create a new project

Create a new folder and initialize the project:

npm init -f

This command initializes the package.json files with default values. You'll probably want to edit this file later.

Install the Speech SDK

Add the Speech SDK to your Node.js project:

npm install microsoft-cognitiveservices-speech-sdk

This command downloads and installs the latest version of the Speech SDK and any required prerequisites from npmjs. The SDK installs in the node_modules directory inside your project folder.

Use the Speech SDK

Create a new file in the folder, named index.js, and open this file with a text editor.


In Node.js, the Speech SDK doesn't support the microphone or the File data type. Both are only supported on browsers. Instead, use the Stream interface to the Speech SDK, either through AudioInputStream.createPushStream() or AudioInputStream.createPullStream().

In this example, we use the PushAudioInputStream interface.

Add this JavaScript code:

"use strict";

// pull in the required packages.
var sdk = require("microsoft-cognitiveservices-speech-sdk");
var fs = require("fs");

// replace with your own subscription key,
// service region (e.g., "westus"), and
// the name of the file you want to run
// through the speech recognizer.
var subscriptionKey = "YourSubscriptionKey";
var serviceRegion = "YourServiceRegion"; // e.g., "westus"
var filename = "YourAudioFile.wav"; // 16000 Hz, Mono

// create the push stream we need for the speech sdk.
var pushStream = sdk.AudioInputStream.createPushStream();

// open the file and push it to the push stream.
fs.createReadStream(filename).on('data', function(arrayBuffer) {
}).on('end', function() {

// we are done with the setup
console.log("Now recognizing from: " + filename);

// now create the audio-config pointing to our stream and
// the speech config specifying the language.
var audioConfig = sdk.AudioConfig.fromStreamInput(pushStream);
var speechConfig = sdk.SpeechConfig.fromSubscription(subscriptionKey, serviceRegion);

// setting the recognition language to English.
speechConfig.speechRecognitionLanguage = "en-US";

// create the speech recognizer.
var recognizer = new sdk.SpeechRecognizer(speechConfig, audioConfig);

// start the recognizer and wait for a result.
  function (result) {

    recognizer = undefined;
  function (err) {
    console.trace("err - " + err);

    recognizer = undefined;

Run the sample

To open the app, adapt YourSubscriptionKey, YourServiceRegion, and YourAudioFile.wav to your configuration. Then run it by calling this command:

node index.js

It triggers a recognition by using the provided filename. And it presents the output on the console.

This sample is the output when you run index.js after you update the subscription key and use the file whatstheweatherlike.wav:

SpeechRecognitionResult {
  "privResultId": "9E30EEBD41AC4571BB77CF9164441F46",
  "privReason": 3,
  "privText": "What's the weather like?",
  "privDuration": 15900000,
  "privOffset": 300000,
  "privErrorDetails": null,
  "privJson": {
    "RecognitionStatus": "Success",
    "DisplayText": "What's the weather like?",
    "Offset": 300000,
    "Duration": 15900000
  "privProperties": null

Install and use the Speech SDK with Visual Studio Code

You can also run the sample from Visual Studio Code. Follow these steps to install, open, and run the quickstart:

  1. Start Visual Studio Code. Select Open Folder. Then browse to the quickstart folder.

    Open folder

  2. Open a terminal in Visual Studio Code.

    The terminal window

  3. Run npm to install the dependencies.

    npm install

  4. Now you're ready to open index.jsand set a breakpoint.

    index.js with a breakpoint on line 16

  5. To start debugging, either select F5 or select Debug/Start Debugging from the menu.

    The debug menu

  6. When a breakpoint is hit, you can inspect the call stack and variables.


  7. Any output shows in the debug console window.

    Debug console

Next steps