On this page

Native API

Reach the provider's own client or socket, and provider-specific helpers, when the session API doesn't cover what you need.

The session API is the same for every provider. Sometimes you need something only one provider has. session.native and session.ext let you reach it without giving up the session.

What they are

MemberHolds
session.nativeThe provider's own client or socket: what the adapter uses to talk to the provider.
session.ext.<provider>Helpers specific to one provider, such as session.ext.deepgram. Present only when that provider is part of the session.

What session.native holds depends on the adapter. google(), azure() and aws() wrap the vendor's SDK, so it is that SDK's client. The other adapters call the provider's HTTP or WebSocket API directly, so it is the socket or HTTP client the adapter uses.

When to use them

  • A provider feature the session API doesn't cover, such as a provider-specific setting or control message.
  • A workaround while you wait for the SDK. If you reach for native for something common, it may belong in the session API. Open an issue so it can be added.

Don't use them for anything the session API already does. A transcript you read from the native socket skips the SDK's result handling, so you lose ok: false results and VoiceError codes.

session.native

For Deepgram, session.native is the WebSocket the adapter streams audio over. This example sends Deepgram's keepalive message while the caller is on hold, so the connection stays open while no audio flows.

TypeScript
import { createSession } from "voiceai-sdk";
import { deepgram } from "voiceai-sdk/deepgram";

await using session = await createSession(deepgram());

const socket = session.native;
const keepalive = setInterval(() => socket.send(JSON.stringify({ type: "KeepAlive" })), 5000);

try {
  await new Promise((resolve) => setTimeout(resolve, 30_000)); // caller on hold, no audio sent
} finally {
  clearInterval(keepalive);
}

The message format is Deepgram's, not the SDK's. Check the provider's own API reference for what it accepts.

Don't close or replace session.native. The session owns it and closes it in session.close().

session.ext

session.ext holds one entry per provider that offers helpers. In a pipeline of Deepgram and ElevenLabs, the speech-to-text side's helpers are under session.ext.deepgram and the text-to-speech side's under session.ext.elevenlabs.

Helpers differ per provider. Check that the entry exists before you use it, because it is missing on other providers:

TypeScript
const helpers = session.ext.deepgram;

if (helpers) {
  // Deepgram-only code goes here.
}

Keep native code in one place

Put provider-specific code in one module and check session.provider before you use it. When you switch providers, that module is the only thing to change. The rest of your app keeps running on the session API.

elevenlabs-extras.ts
import type { Session } from "voiceai-sdk";

// Everything that depends on ElevenLabs lives in this file.
export async function tuneElevenLabs(session: Session) {
  if (session.provider !== "elevenlabs") return;

  const client = session.native;
  // Call ElevenLabs' own API through client here,
  // following ElevenLabs' API reference.
}
app.ts
import { createSession } from "voiceai-sdk";
import { elevenlabs } from "voiceai-sdk/elevenlabs";
import { tuneElevenLabs } from "./elevenlabs-extras.js";

await using session = await createSession(elevenlabs());
await tuneElevenLabs(session);

await session.speak("Your appointment is confirmed.");

If you later move to another provider, tuneElevenLabs returns early and the rest of app.ts still runs.

Portability

Before you rely on a native call, check whether a session option or a capability flag already covers it. See Capabilities.

Was this page helpful?

Esc

Loading…

↑↓ navigate openCtrl J preview