Deepgram JavaScript SDK

šÆ Development Setup: This project uses Corepack for package manager consistency. Run corepack enable
once, then use pnpm
commands normally. See DEVELOPMENT.md for details.
Documentation
You can learn more about the Deepgram API at developers.deepgram.com.
Migrating from earlier versions
V2 to V3
We have published a migration guide on our docs, showing how to move from v2 to v3.
V3.* to V3.4
We recommend using only documented interfaces, as we strictly follow semantic versioning (semver) and breaking changes may occur for undocumented interfaces. To ensure compatibility, consider pinning your versions if you need to use undocumented interfaces.
V3.* to V4
The Voice Agent interfaces have been updated to use the new Voice Agent V1 API. Please refer to our Documentation on Migration to new V1 Agent API.
Installation
You can install this SDK directly from [npm](https://www.npmjs.com/package/@deepgram/sdk).
npm install @deepgram/sdk
or
pnpm install @deepgram/sdk
or
yarn add @deepgram/sdk
UMD
You can now use plain <script>
s to import deepgram from CDNs, like:
<script src="https://cdn.jsdelivr.net/npm/@deepgram/sdk"></script>
or even:
<script src="https://unpkg.com/@deepgram/sdk"></script>
Then you can use it from a global deepgram variable:
<script>
const { createClient } = deepgram;
const deepgramClient = createClient("deepgram-api-key");
console.log("Deepgram client instance: ", deepgramClient);
</script>
ESM
You can now use type="module" <script>
s to import deepgram from CDNs, like:
<script type="module">
import { createClient } from "https://cdn.jsdelivr.net/npm/@deepgram/sdk/+esm";
const deepgramClient = createClient("deepgram-api-key");
console.log("Deepgram client instance: ", deepgramClient);
</script>
Initialization
All of the examples below will require createClient.
import { createClient } from "@deepgram/sdk";
const deepgramClient = createClient(DEEPGRAM_API_KEY);
Getting an API Key
š To access the Deepgram API you will need a free Deepgram API Key.
Scoped Configuration
The SDK supports scoped configuration. You'll be able to configure various aspects of each namespace of the SDK from the initialization. Below outlines a flexible and customizable configuration system for the Deepgram SDK. Here's how the namespace configuration works:
Global Defaults
- The
global
namespace serves as the foundational configuration applicable across all other namespaces unless overridden.
- Includes general settings like URL and headers applicable for all API calls.
- If no specific configurations are provided for other namespaces, the
global
defaults are used.
Namespace-specific Configurations
- Each namespace (
listen
, manage
, onprem
, read
, speak
) can have its specific configurations which override the global
settings within their respective scopes.
- Allows for detailed control over different parts of the application interacting with various Deepgram API endpoints.
Transport Options
- Configurations for both
fetch
and websocket
can be specified under each namespace, allowing different transport mechanisms for different operations.
- For example, the
fetch
configuration can have its own URL and proxy settings distinct from the websocket
.
- The generic interfaces define a structure for transport options which include a client (like a
fetch
or WebSocket
instance) and associated options (like headers, URL, proxy settings).
This configuration system enables robust customization where defaults provide a foundation, but every aspect of the client's interaction with the API can be finely controlled and tailored to specific needs through namespace-specific settings. This enhances the maintainability and scalability of the application by localizing configurations to their relevant contexts.
Examples
Change the API url used for all SDK methods
Useful for using different API environments (for e.g. beta).
import { createClient } from "@deepgram/sdk";
const deepgramClient = createClient(DEEPGRAM_API_KEY, {
global: { fetch: { options: { url: "https://api.beta.deepgram.com" } } },
});
Change the API url used for the Voice Agent websocket
Useful for using a voice agent proxy (for e.g. 3rd party provider auth).
import { createClient } from "@deepgram/sdk";
const deepgramClient = createClient(DEEPGRAM_API_KEY, {
global: { websocket: { options: { url: "ws://localhost:8080" } } },
});
Change the API url used for transcription only
Useful for on-prem installations. Only affects requests to /listen
endpoints.
import { createClient } from "@deepgram/sdk";
const deepgramClient = createClient(DEEPGRAM_API_KEY, {
listen: { fetch: { options: { url: "http://localhost:8080" } } },
});
Override fetch transmitter
Useful for providing a custom http client.
import { createClient } from "@deepgram/sdk";
const yourFetch = async () => {
return Response("...etc");
};
const deepgramClient = createClient(DEEPGRAM_API_KEY, {
global: { fetch: { client: yourFetch } },
});
Proxy requests in the browser
This SDK now works in the browser. If you'd like to make REST-based requests (pre-recorded transcription, on-premise, and management requests), then you'll need to use a proxy as we do not support custom CORS origins on our API. To set up your proxy, you configure the SDK like so:
import { createClient } from "@deepgram/sdk";
const deepgramClient = createClient("proxy", {
global: { fetch: { options: { proxy: { url: "http://localhost:8080" } } } },
});
Important: You must pass "proxy"
as your API key, and use the proxy to set the Authorization
header to your Deepgram API key.
Your proxy service should replace the Authorization header with Authorization: token <DEEPGRAM_API_KEY>
and return results verbatim to the SDK.
Check out our example Node-based proxy here: Deepgram Node Proxy.
Useful for many things.
import { createClient } from "@deepgram/sdk";
const deepgramClient = createClient("proxy", {
global: { fetch: { options: { headers: { "x-custom-header": "foo" } } } },
});
Browser Usage
To use this SDK in the browser, check out UMD and/or ESM initialisation.
Also see proxy requests in the browser if you're planning to make RESTful requests to our API.
Transcription
Remote Files
Transcribe audio from a URL.
const { result, error } = await deepgramClient.listen.prerecorded.transcribeUrl(
{ url: "https://dpgr.am/spacewalk.wav" },
{
model: "nova-3",
}
);
See our API reference for more info.
Local Files
Transcribe audio from a file.
const { result, error } = await deepgramClient.listen.prerecorded.transcribeFile(
fs.createReadStream("./examples/spacewalk.wav"),
{
model: "nova-3",
}
);
See our API reference for more info.
Callbacks / Async
We have a Callback
version of both transcribeFile
and transcribeUrl
, which simply takes a CallbackUrl
class.
import { CallbackUrl } from "@deepgram/sdk";
const { result, error } = await deepgramClient.listen.prerecorded.transcribeUrlCallback(
{ url: "https://dpgr.am/spacewalk.wav" },
new CallbackUrl("http://callback/endpoint"),
{
model: "nova-3",
}
);
See our API reference for more info.
Live Transcription (WebSocket)
Connect to our websocket and transcribe live streaming audio.
const deepgramConnection = deepgramClient.listen.live({
model: "nova-3",
});
deepgramConnection.on(LiveTranscriptionEvents.Open, () => {
deepgramConnection.on(LiveTranscriptionEvents.Transcript, (data) => {
console.log(data);
});
source.addListener("got-some-audio", async (event) => {
deepgramConnection.send(event.raw_audio_data);
});
});
See our API reference for more info.
Captions
Convert deepgram transcriptions to captions.
import { webvtt, srt } from "@deepgram/sdk";
const { result, error } = await deepgramClient.listen.prerecorded.transcribeUrl({
model: "nova-3",
});
const vttResult = webvtt(result);
const srtResult = srt(result);
See our standalone captions library for more information.
Voice Agent
Configure a Voice Agent.
import { AgentEvents } from "@deepgram/sdk";
const deepgramConnection = deepgramClient.agent();
deepgramConnection.on(AgentEvents.Open, () => {
console.log("Connection opened");
deepgramConnection.on(AgentEvents.ConversationText, (data) => {
console.log(data);
});
deepgramConnection.configure({
});
});
See our API reference for more info.
Text to Speech
Single-Request
Convert text into speech using the REST API.
const { result } = await deepgramClient.speak.request(
{ text },
{
model: "aura-2-thalia-en",
}
);
See our API reference for more info.
Continuous Text Stream (WebSocket)
Connect to our websocket and send a continuous text stream to generate speech.
const deepgramConnection = deepgramClient.speak.live({
model: "aura-2-thalia-en",
});
deepgramConnection.on(LiveTTSEvents.Open, () => {
console.log("Connection opened");
deepgramConnection.sendText(text);
deepgramConnection.flush();
deepgramConnection.on(LiveTTSEvents.Close, () => {
console.log("Connection closed");
});
});
See our API reference for more info.
Text Intelligence
Analyze text using our intelligence AI features.
const text = `The history of the phrase 'The quick brown fox jumps over the
lazy dog'. The earliest known appearance of the phrase was in The Boston
Journal...`;
const { result, error } = await deepgramClient.read.analyzeText(
{ text },
{
language: "en",
}
);
See our API reference for more info.
Authentication
Get Token Details
Retrieves the details of the current authentication token.
const { result, error } = await deepgramClient.manage.getTokenDetails();
See our API reference for more info
Grant Token
Creates a temporary token with a 30-second TTL.
const { result, error } = await deepgramClient.auth.grantToken();
This example shows how to use the temporary token to authenticate a client instance. Note that you must pass an accessToken
property to use a temporary token. Passing the token as a raw string will error, as the SDK will treat it as an API key and use the incorrect header prefix.
See our API reference for more info.
Projects
Get Projects
Returns all projects accessible by the API key.
const { result, error } = await deepgramClient.manage.getProjects();
See our API reference for more info.
Get Project
Retrieves a specific project based on the provided project_id.
const { result, error } = await deepgramClient.manage.getProject(projectId);
See our API reference for more info.
Update Project
Update a project.
const { result, error } = await deepgramClient.manage.updateProject(projectId, options);
See our API reference for more info.
Delete Project
Delete a project.
const { error } = await deepgramClient.manage.deleteProject(projectId);
See our API reference for more info.
Keys
List Keys
Retrieves all keys associated with the provided project_id.
const { result, error } = await deepgramClient.manage.getProjectKeys(projectId);
See our API reference for more info.
Get Key
Retrieves a specific key associated with the provided project_id.
const { result, error } = await deepgramClient.manage.getProjectKey(projectId, projectKeyId);
See our API reference for more info.
Create Key
Creates an API key with the provided scopes.
const { result, error } = await deepgramClient.manage.createProjectKey(projectId, options);
See our API reference for more info.
Delete Key
Deletes a specific key associated with the provided project_id.
const { error } = await deepgramClient.manage.deleteProjectKey(projectId, projectKeyId);
See our API reference for more info.
Members
Get Members
Retrieves account objects for all of the accounts in the specified project_id.
const { result, error } = await deepgramClient.manage.getProjectMembers(projectId);
See our API reference for more info.
Remove Member
Removes member account for specified member_id.
const { error } = await deepgramClient.manage.removeProjectMember(projectId, projectMemberId);
See our API reference for more info.
Scopes
Get Member Scopes
Retrieves scopes of the specified member in the specified project.
const { result, error } = await deepgramClient.manage.getProjectMemberScopes(
projectId,
projectMemberId
);
See our API reference for more info.
Update Scope
Updates the scope for the specified member in the specified project.
const { result, error } = await deepgramClient.manage.updateProjectMemberScope(
projectId,
projectMemberId,
options
);
See our API reference for more info.
Invitations
List Invites
Retrieves all invitations associated with the provided project_id.
const { result, error } = await deepgramClient.manage.getProjectInvites(projectId);
See our API reference for more info.
Send Invite
Sends an invitation to the provided email address.
const { result, error } = await deepgramClient.manage.sendProjectInvite(projectId, options);
See our API reference for more info.
Delete Invite
Removes the specified invitation from the project.
const { error } = await deepgramClient.manage.deleteProjectInvite(projectId, email);
See our API reference for more info.
Leave Project
Removes the authenticated user from the project.
const { result, error } = await deepgramClient.manage.leaveProject(projectId);
See our API reference for more info.
Usage
Get All Requests
Retrieves all requests associated with the provided project_id based on the provided options.
const { result, error } = await deepgramClient.manage.getProjectUsageRequests(projectId, options);
Get Request
Retrieves a specific request associated with the provided project_id.
const { result, error } = await deepgramClient.manage.getProjectUsageRequest(projectId, requestId);
See our API reference for more info.
Summarize Usage
Retrieves usage associated with the provided project_id based on the provided options.
const { result, error } = await deepgramClient.manage.getProjectUsageSummary(projectId, options);
See our API reference for more info.
Get Fields
Lists the features, models, tags, languages, and processing method used for requests in the specified project.
const { result, error } = await deepgramClient.manage.getProjectUsageFields(projectId, options);
See our API reference for more info.
Summarize Usage
Deprecated
Retrieves the usage for a specific project. Use Get Project Usage Breakdown for a more comprehensive usage summary.
const { result, error } = await deepgramClient.manage.getProjectUsage(projectId, options);
See our API reference for more info.
Billing
Get All Balances
Retrieves the list of balance info for the specified project.
const { result, error } = await deepgramClient.manage.getProjectBalances(projectId);
See our API reference for more info.
Get Balance
Retrieves the balance info for the specified project and balance_id.
const { result, error } = await deepgramClient.manage.getProjectBalance(projectId, balanceId);
See our API reference for more info.
Models
Get All Project Models
Retrieves all models available for a given project.
const { result, error } = await deepgramClient.manage.getAllModels(projectId, {});
See our API reference for more info.
Get Model
Retrieves details of a specific model.
const { result, error } = await deepgramClient.manage.getModel(projectId, modelId);
See our API reference for more info
On-Prem APIs
List On-Prem credentials
Lists sets of distribution credentials for the specified project.
const { result, error } = await deepgramClient.onprem.listCredentials(projectId);
See our API reference for more info
Get On-Prem credentials
Returns a set of distribution credentials for the specified project.
const { result, error } = await deepgramClient.onprem.getCredentials(projectId, credentialId);
See our API reference for more info
Create On-Prem credentials
Creates a set of distribution credentials for the specified project.
const { result, error } = await deepgramClient.onprem.createCredentials(projectId, options);
See our API reference for more info
Delete On-Prem credentials
Deletes a set of distribution credentials for the specified project.
const { result, error } = await deepgramClient.onprem.deleteCredentials(projectId, credentialId);
See our API reference for more info
Backwards Compatibility
Older SDK versions will receive Priority 1 (P1) bug support only. Security issues, both in our code and dependencies, are promptly addressed. Significant bugs without clear workarounds are also given priority attention.
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.
Debugging and making changes locally
If you want to make local changes to the SDK and run the examples/
, you'll need to pnpm build
first, to ensure that your changes are included in the examples that are running.
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: