Deepgram JavaScript SDK

repository·main·Indexed 42 days ago

https://github.com/deepgram/deepgram-js-sdk

Official client library (@deepgram/sdk v5.11.0) for interacting with Deepgram's AI services. Provides programmatic access to automated speech recognition (ASR), text-to-speech (TTS), conversational voice agents, and text intelligence. Supports both Node.js and browser environments with features for prerecorded audio transcription, real-time WebSocket streaming, and a Management API for project and billing administration.

Tokens
59.3K
Snippets
227
Records
325
Agent score
55%

What's inside Deepgram

  1. Overview of Browser Example Categories

    main

    The browser examples directory provides demonstrations for the following functional areas:

    • Authentication: API Key, Access Token, and Proxy authentication.
    • Transcription: Prerecorded audio (URL, local file, callbacks), Live WebSocket (V1 and V2), Captions (WebVTT/SRT), and Advanced options.
    • Voice Agent: Configuration and usage of the Voice Agent API.
    • Text-to-Speech: Single request TTS and Streaming TTS via WebSocket.
    • Text Intelligence: AI-powered text analysis.
    • Management API: Managing Projects, API Keys, Members, Invites, Usage, Billing, and Models.
    • On-Premises: Credentials management for on-prem deployments.
    • Advanced: Scoped configuration, file upload types, error handling, and binary response handling.
  2. Management API features

    main

    The SDK allows for programmatic management of your Deepgram resources:

    • Projects: List, get, update, and delete projects.
    • Keys: Manage API keys (list, get, create, delete).
    • Members & Permissions: Manage members, invitations, and specific member permissions.
    • Usage & Billing: Retrieve usage statistics, usage breakdowns, billing information, and balance details.
    • Models: Access information about available models and project-specific models.
  3. Configure Authentication in v5.0.0

    main

    The v5.0.0 SDK supports both API Keys and Access Tokens. When initializing DeepgramClient, the SDK follows a specific priority order for authentication credentials:

    1. Explicit accessToken parameter: Highest priority.
    2. Explicit apiKey parameter: Second priority.
    3. DEEPGRAM_API_KEY environment variable: Lowest priority.
    // Access Token Authentication
    const deepgram = new DeepgramClient({ accessToken: "YOUR_ACCESS_TOKEN" });
    
    // API Key Authentication
    const deepgram = new DeepgramClient({ apiKey: "YOUR_API_KEY" });
  4. Summary of breaking changes in v5.0.0

    main

    The migration from v4 to v5 involves several major architectural changes:

    Major Changes

    • Client Initialization: Use new DeepgramClient() instead of createClient().
    • API Structure: All methods now use versioned namespaces (e.g., v1, v2).
    • Error Handling: Switched from { result, error } to try/catch.
    • WebSocket API: Redesigned to use async connect() and waitForOpen().
    • Configuration: The options object is simplified, using apiKey instead of key.

    Removed Features

    • createClient()
    • key property in options
    • { result, error } return pattern
    • LiveTranscriptionEvents enum (use string events instead)
    • Separate analyzeText() / analyzeUrl() methods (unified into analyze())
    • Top-level models namespace (moved to manage.v1.models)
    • onprem namespace (renamed to selfHosted)
  5. Authentication methods in the Deepgram SDK

    main

    The SDK supports several authentication patterns depending on your environment and security requirements:

    • API Key Authentication: Standard method using a permanent API key.
    • Access Token Authentication: Using short-lived access tokens.
    • Proxy Authentication: Specifically for browser environments where requests are routed through a proxy.
  6. How streaming WebSocket connections work

    main

    Every streaming client (Voice Agent, Speech-to-Text, Text-to-Speech) exposes connect() and createConnection() methods, which are aliases that return a start-closed socket.

    To use a streaming connection:

    1. Register event handlers using .on("open" | "message" | "close" | "error", callback).
    2. Call .connect() on the socket.
    3. Await .waitForOpen() before attempting to send data.

    Streaming connections are also async iterables, allowing you to consume messages using a for await...of loop. Iteration ends when the connection closes, throws on error, or when you break the loop (which closes the connection). Note that messages arriving while the loop body is executing are buffered (up to 1,000 messages or 16 MiB) rather than dropped.

    const connection = await client.listen.v1.connect({ model: "nova-3", interim_results: "true" });
    
    connection.connect();
    await connection.waitForOpen();
    
    for await (const message of connection) {
      if (message.type === "Results" && message.is_final) {
        console.log(message.channel.alternatives[0].transcript);
      }
    }
  7. Transcription use cases and features

    main

    The SDK provides a wide range of transcription capabilities:

    • Prerecorded Audio: Transcribe from a URL, a local file, or via async callbacks.
    • Live Transcription: Real-time transcription via WebSocket (including WebSocket V2 support).
    • Captions: Convert transcriptions into WebVTT or SRT formats.
    • Advanced Tuning: Includes Flux end-of-turn (EOT) tuning, manual Flux turn finalization, and AbortSignal support for safe WebSocket cancellation.
    • Resiliency: Production-grade reconnection logic with backoff, jitter, and audio buffering.
  8. Cancel WebSocket connections using AbortSignal

    main

    All real-time connect() methods—including listen.v1.connect(), listen.v2.connect(), agent.v1.connect(), speak.v1.connect(), and speak.v2.connect()—accept an abortSignal.

    When the signal aborts:

    • The SDK stops the pending or active transport.
    • Automatic reconnection is disabled.
    • A registered close callback may run.

    Important Notes:

    • AbortSignal does not clear callbacks registered with connection.on().
    • waitForOpen() does not observe the signal directly. If you need to cancel a connection while it is still opening, you must implement an abort-aware wrapper using signal.addEventListener('abort', ...) to reject the promise manually.
    import { DeepgramClient } from "@deepgram/sdk";
    
    const client = new DeepgramClient();
    const controller = new AbortController();
    
    function waitForOpenOrAbort(
      connection: { waitForOpen(): Promise<unknown> },
      signal: AbortSignal,
    ): Promise<void> {
      return new Promise((resolve, reject) => {
        const cleanup = () => signal.removeEventListener("abort", onAbort);
        const onAbort = () => {
          cleanup();
          reject(signal.reason ?? new Error("Connection aborted"));
        };
    
        if (signal.aborted) {
          onAbort();
          return;
        }
    
        signal.addEventListener("abort", onAbort, { once: true });
        connection.waitForOpen().then(
          () => {
            cleanup();
            resolve();
          },
          (error) => {
            cleanup();
            reject(error);
          },
        );
      });
    }
    
    const connection = await client.listen.v1.connect({
      model: "nova-3",
      language: "en",
      abortSignal: controller.signal,
    });
    
    connection.on("open", () => console.log("Connection opened"));
    connection.on("message", (data) => console.log(data));
    
    connection.connect();
    await waitForOpenOrAbort(connection, controller.signal);
    
    // Cancel the session later.
    controller.abort();
  9. Voice Agent, TTS, and Text Intelligence capabilities

    main

    Beyond standard transcription, the SDK supports:

    • Voice Agent: Configuration and usage, including custom third-party providers, provider combinations, and injecting messages into active sessions.
    • Text-to-Speech (TTS): Single request TTS, streaming TTS via WebSocket, and streaming TTS with Flux.
    • Text Intelligence: AI-powered text analysis with both standard and advanced options.
  10. Use the Deepgram SDK in a Browser

    main

    The SDK works in modern browsers with the following rules:

    WebSocket Features (Full Support)

    Direct connections are supported for:

    • Live Transcription (wss://api.deepgram.com)
    • Voice Agent (wss://agent.deepgram.com)
    • Live Text-to-Speech (wss://api.deepgram.com)

    REST API Features (Proxy Required)

    Due to CORS restrictions, you cannot call REST APIs directly from a browser. You must use a proxy server. Configure the client by passing "proxy" as the apiKey and providing your proxy's URL as baseUrl.

    Setup via CDN

    UMD:

    <script src="https://cdn.jsdelivr.net/npm/@deepgram/sdk"></script>
    <script>
      const { DeepgramClient } = deepgram;
    </script>

    ESM:

    <script type="module">
      import { DeepgramClient } from "https://cdn.jsdelivr.net/npm/@deepgram/sdk/+esm";
    </script>
    // Browser REST API configuration via proxy
    import { DeepgramClient } from "@deepgram/sdk";
    
    const client = new DeepgramClient({
      apiKey: "proxy",
      baseUrl: "http://localhost:8080",
    });
  11. Migrate WebSocket Streaming (Listen V1) from v4 to v5

    main

    The WebSocket connection for Listen V1 has changed significantly in v5:

    1. Namespace: deepgram.listen.live is now deepgram.listen.v1.connect.
    2. Initialization: The connection is now an async operation (await deepgram.listen.v1.connect(...)).
    3. Boolean Options: In v5, WebSocket options that accept boolean values must be passed as strings (e.g., "true" instead of true).
    4. Lifecycle: You must explicitly call .connect() and it is recommended to use .waitForOpen().
    5. Events: Event names have changed (e.g., LiveTranscriptionEvents.Transcript is now a 'message' event where you check data.type === 'Results').
    // v5.0.0 Listen V1 WebSocket
    const connection = await deepgram.listen.v1.connect({
      model: "nova-3",
      language: "en",
      punctuate: "true",
      interim_results: "true",
    });
    
    connection.on("open", () => {
      console.log("Connected");
    });
    
    connection.on("message", (data) => {
      if (data.type === "Results") {
        console.log(data);
      }
    });
    
    connection.connect();
    await connection.waitForOpen();
  12. Run Deepgram Browser Examples

    main

    To run the browser-based examples, you must first build the SDK and then start the local development server. The server requires your Deepgram API Key and Project ID to be provided as environment variables. The server also automatically starts a proxy to handle CORS restrictions for REST API calls.

    1. Build the SDK:

      make build
    2. Start the server:

      DEEPGRAM_API_KEY=your_key DEEPGRAM_PROJECT_ID=your_id make browser-serve
    3. Access examples: Open the provided URL in a modern browser (Chrome, Edge, Firefox, or Safari). The root view will list all examples as clickable links. Once an example is open, click the "Run" button to execute the logic and view results in the UI.

    DEEPGRAM_API_KEY=your_key DEEPGRAM_PROJECT_ID=your_id make browser-serve