/deepgram-node-sdk

Official JavaScript SDK for Deepgram's automated speech recognition APIs.

Primary LanguageTypeScriptMIT LicenseMIT

Deepgram Node.js SDK

CI npm (scoped) Contributor Covenant

Official Node.js SDK for Deepgram's automated speech recognition APIs.

This SDK only supports hosted usage of api.deepgram.com.

To access the API you will need a Deepgram account. Sign up for free at signup.

Documentation

Full documentation of the Node.js SDK can be found on the Deepgram Developer Portal.

You can learn more about the full Deepgram API at https://developers.deepgram.com.

Installation

With NPM

npm install @deepgram/sdk

With Yarn

yarn add @deepgram/sdk

Constructor

const { Deepgram } = require("@deepgram/sdk");

const deepgram = new Deepgram(DEEPGRAM_API_KEY);

Examples

Transcribe an Existing File

Remote Files

const fileSource = { url: URL_OF_FILE };

const response = await deepgram.transcription.preRecorded(fileSource, {
  punctuate: true,
});

Local Files

const streamSource = {
  stream: fs.createReadStream("/path/to/file"),
  mimetype: MIMETYPE_OF_FILE,
};

const response = await deepgram.transcription.preRecorded(streamSource, {
  punctuate: true,
});

Transcribe Audio in Real-Time

navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => {
  const mediaRecorder = new MediaRecorder(stream, {
    mimeType: 'audio/webm',
  });
  const deepgramSocket = deepgram.transcription.live({ punctuate: true });

  deepgramSocket.addEventListener('open', () => {
    mediaRecorder.addEventListener('dataavailable', async (event) => {
      if (event.data.size > 0 && deepgramSocket.readyState == 1) {
        deepgramSocket.send(event.data)
      }
    })
    mediaRecorder.start(1000)
  });

  deepgramSocket.addEventListener("message", (message) => {
    const received = JSON.parse(message.data);
    const transcript = received.channel.alternatives[0].transcript;
    if (transcript && received.is_final) {
      console.log(transcript);
    }
  });
});

Samples

To run the sample code, first run the following in your terminal:

npm install
npm build

Then update the config object located at the top of the index.js file in the sample folder.

const config = {
  deepgramApiKey: "YOUR_DEEPGRAM_API_KEY",
  urlToFile:
    "https://static.deepgram.com/examples/Bueller-Life-moves-pretty-fast.wav",
};

Finally, run the sample code using the following command in your terminal:

node sample/index.js

The sample demonstrates the following uses:

  • Transcribing a prerecorded file
  • Retrieving usage for a project
  • Getting a project
  • Creating an API key
  • Deleting an API key

Development and Contributing

Interested in contributing? We ❤️ pull requests!

To make sure our community is safe for all, be sure to review and agree to our Code of Conduct. Then see the Contribution guidelines for more information.

Getting Help

We love to hear from you so if you have questions, comments or find a bug in the project, let us know! You can either:

Further Reading

Check out the Developer Documentation at https://developers.deepgram.com/