Deepgram Node.js SDK
Official Node.js SDK for Deepgram's automated
speech recognition APIs.
To access the API you will need a Deepgram account. Sign up for free at
signup.
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({
apiKey: DEEPGRAM_API_KEY,
apiUrl: CUSTOM_API_URL,
});
Usage
Transcription
The transcription
property can handle both pre-recorded and live transcriptions.
Prerecorded Transcription
The transcription.preRecorded
method handles sending an existing file or
buffer to the Deepgram API to generate a transcription. Additional options
can be provided to customize the result.
const fileSource = { url: URL_OF_FILE };
const bufferSource = { buffer: BUFFER_OF_FILE, mimetype: MIMETYPE_OF_FILE };
const response = await deepgram.transcription.preRecorded(
fileSource | bufferSource,
{
punctuate: true,
}
);
Prerecorded Transcription Options
Additional transcription options can be provided for prerecorded transcriptions.
{
model?: Models | string;
version: string;
language?: string;
punctuate?: boolean;
profanity_filter?: boolean;
redact?: Array<string>;
diarize?: boolean;
multichannel?: boolean;
alternatives?: number;
numerals?: boolean;
search?: Array<string>;
callback?: string;
keywords?: Array<string>;
utterances?: boolean;
utt_split?: number;
}
Prerecorded Transcription Response
{
request_id?: string;
metadata?: {
request_id: string;
transaction_key: string;
sha256: string;
created: string;
duration: number;
channels: number;
};
results?: {
channels: Array<{
search?: Array<{
query: string;
hits: Array<{
confidence: number;
start: number;
end: number;
snippet: string;
}>;
}>;
alternatives: Array<{
transcript: string;
confidence: number;
words: Array<{
word: string;
start: number;
end: number;
confidence: number;
punctuated_word?: string;
}>;
}>;
}>;
utterances?: Array<{
start: number;
end: number;
confidence: number;
channel: number;
transcript: string;
words: Array<{
word: string;
start: number;
end: number;
confidence: number;
punctuated_word?: string;
}>;
speaker?: number;
id: string;
}>;
};
};
Live Transcription
The transcription.live
method provides access to a websocket connection
to the Deepgram API for generating streaming transcriptions. Additional options
can be provided to customize the result.
const deepgramLive = deepgram.transcription.live({ punctuate: true });
socket.on("microphone-stream", (stream) => {
deepgramSocket.send(stream);
});
deepgramLive.addListener("transcriptReceived", (transcription) => {
console.log(transcription.data);
});
Events
The following events are fired by the live transcription object:
Event | Description | Data |
---|
open | The websocket connection to Deepgram has been opened. | The DG live transcription object |
close | The websocket connection to Deepgram has been closed. | WebSocket.CloseEvent |
error | An error occurred with the websocket connection | Error object |
transcriptReceived | Deepgram has responded with a transcription | Transcription Response |
Live Transcription Options
Additional transcription options can be provided for live transcriptions.
{
model?: Models | string;
version: string;
language?: string;
punctuate?: boolean;
profanity_filter?: boolean;
redact?: Array<string>;
diarize?: boolean;
multichannel?: boolean;
alternatives?: number;
numerals?: boolean;
search?: Array<string>;
callback?: string;
keywords?: Array<string>;
interim_results?: boolean;
endpointing?: boolean;
vad_turnoff?: number;
encoding?: string;
channels?: number;
sample_rate?: number;
}
Live Transcription Response
{
channel_index: Array<number>;
duration: number;
start: number;
is_final: boolean;
speech_final: boolean;
channel: {
search?: Array<{
query: string;
hits: Array<{
confidence: number;
start: number;
end: number;
snippet: string;
}>
}>,
alternatives: Array<{
transcript: string;
confidence: number;
words: Array<{
word: string;
start: number;
end: number;
confidence: number;
punctuated_word?: string;
}>
}>
}
};
Project Management
List Projects
Retrieve all projects
const projects = await deepgram.projects.list();
List Projects Response
{
projects: [
{
id: string,
name: string,
},
],
}
Get a Project
Retrieves all project based on the provided project id.
const project = await deepgram.projects.get(PROJECT_ID);
Get a Project Response
{
id: string,
name: string,
}
Update a Project
Updates a project based on a provided project object. This object must contain
project_id
and name
properties.
const updateResponse = await deepgram.projects.update(project);
Update a Project Response
{
message: string;
}
Key Management
List Keys
Retrieves all keys for a given project.
const response = await deepgram.keys.list(PROJECT_ID);
List Keys Response
{
api_keys: [
{
api_key_id: string,
comment: string,
created: string,
scopes: Array<string>
},
];
}
Create Key
Create a new API key for a project using the keys.create
method
with a name for the key.
const response = await deepgram.keys.create(PROJECT_ID, COMMENT_FOR_KEY);
Create Key Response
{
api_key_id: string,
key: string,
comment: string,
created: string,
scopes: Array<string>
}
Delete key
Delete an existing API key using the keys.delete
method with the key to
delete.
await deepgram.keys.delete(PROJECT_ID, KEY_ID);
Usage
Requests by Project
Retrieves transcription requests for a project based on the provided options.
const response = await deepgram.usage.listRequests(PROJECT_ID, {
limit: 10,
});
Requests by Project Options
{
start?: string,
end?: string,
page?: number,
limit?: number,
status?: 'succeeded' | 'failed'
}
Requests by Project Response
{
page: number,
limit: number,
requests?: [
{
request_id: string;
created: string;
path: string;
accessor: string;
response?: {
details: {
usd: number;
duration: number;
total_audio: number;
channels: number;
streams: number;
model: string;
method: string;
tags: Array<string>;
features: Array<string>;
config: {
multichannel?: boolean;
interim_results?: boolean;
punctuate?: boolean;
ner?: boolean;
utterances?: boolean;
replace?: boolean;
profanity_filter?: boolean;
keywords?: boolean;
sentiment?: boolean;
diarize?: boolean;
detect_language?: boolean;
search?: boolean;
redact?: boolean;
alternatives?: boolean;
numerals?: boolean;
};
}
}, ||
{
message?: string;
},
callback?: {
code: number;
completed: string;
},
},
];
}
Get Specific Request
Retrieves a specific transcription request for a project based on the provided
projectId
and requestId
.
const response = await deepgram.usage.getRequest(PROJECT_ID, REQUEST_ID);
Specific Request Response
{
request_id: string;
created: string;
path: string;
accessor: string;
response?: {
details: {
usd: number;
duration: number;
total_audio: number;
channels: number;
streams: number;
model: string;
method: string;
tags: Array<string>;
features: Array<string>;
config: {
multichannel?: boolean;
interim_results?: boolean;
punctuate?: boolean;
ner?: boolean;
utterances?: boolean;
replace?: boolean;
profanity_filter?: boolean;
keywords?: boolean;
sentiment?: boolean;
diarize?: boolean;
detect_language?: boolean;
search?: boolean;
redact?: boolean;
alternatives?: boolean;
numerals?: boolean;
};
}
}, ||
{
message?: string;
},
callback?: {
code: number;
completed: string;
}
}
Get Usage by Project
Retrieves aggregated usage data for a project based on the provided options.
const response = await deepgram.usage.getUsage(PROJECT_ID, {
start: "2020-01-01T00:00:00+00:00",
});
Usage by Project Options
{
start?: string,
end?: string,
accessor?: string,
tag?: Array<string>,
method?: "sync" | "async" | "streaming",
model?: string,
multichannel?: boolean,
interim_results?: boolean,
punctuate?: boolean,
ner?: boolean,
utterances?: boolean,
replace?: boolean,
profanity_filter?: boolean,
keywords?: boolean,
sentiment?: boolean,
diarize?: boolean,
detect_language?: boolean,
search?: boolean,
redact?: boolean,
alternatives?: boolean,
numerals?: boolean
}
Get Usage Response
{
start: string,
end: string,
resolution: {
units: string,
amount: number
};
results: [
{
start: string,
end: string,
hours: number,
requests: number
}
];
}
Get Fields
Retrieves features used by the provided projectId based on the provided options.
const response = await deepgram.usage.getUsage(PROJECT_ID, {
start: "2020-01-01T00:00:00+00:00",
});
Get Fields Options
{
start?: string,
end?: string
}
Get Fields Response
{
tags: Array<string>,
models: Array<string>,
processing_methods: Array<string>,
languages: Array<string>,
features: Array<string>
}
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/