Skip to main content
WebRTC is the recommended transport for real-time call audio. The audio goes over UDP with Opus and forward error correction (FEC), so it copes far better with bad networks, flaky Wi-Fi and 4G than audio over the WebSocket. You use it through d-api-sdk, with one option:
It works for WhatsApp calls (outbound and incoming) and for live IVR calls. You need an already connected unofficial WhatsApp connection and an API Key from the Dashboard.

How it works

1

Your backend starts the call

With the API Key, dapi.calls.stream() returns callId and wsUrl. Only those two go to the browser, never the API Key.
2

The browser opens the call

LiveCall.connect() asks for the microphone, opens the control WebSocket and starts playing audio over it.
3

The audio moves to WebRTC

In under a second the SDK negotiates WebRTC and moves the audio to it. Events, mute and hangup stay on the WebSocket.
4

If the network blocks WebRTC

The audio goes back to the WebSocket on its own, without dropping the call, and the SDK retries WebRTC in the background.
Events, call.* webhooks and recordings are the same as over the WebSocket: only the audio path changes.

Installation

On Node (bots, AI agents), WebRTC uses one optional package:
Without @roamhq/wrtc, transport: "webrtc" keeps working over the WebSocket. The browser needs nothing else: WebRTC is built in.

In the browser

The complete example has two parts: a route in your backend, which holds the API Key, and the page.

Backend: start the call

server.js

Page: talk and listen

call.js
The SDK asks for the microphone, plays the other side and only sends your audio once the other person answers. Call LiveCall.connect() from a click: browsers only allow audio after a user action. The page must be served over HTTPS (or localhost).
Use a bundler (Vite, webpack, Next.js) to import d-api-sdk in the browser.

On Node (bots and AI agents)

On Node the SDK hands the raw audio to your code: PCM s16le, 16 kHz, mono, in 20 ms frames (320 samples).
bot.js
Audio sent before accepted is dropped: the other side is not listening yet, and that audio would delay the whole call. Send at real-time pace (20 ms of audio every 20 ms); large bursts get queued and arrive late.

Receiving calls

With receiving enabled on the connection:
In the browser, follow the same pattern as the example above: your backend calls dapi.calls.accept({ sessionId, callId, from }) and hands { callId, wsUrl } to the page, which calls LiveCall.connect().

Live IVR calls

The same interface: events (including dtmf), audio, hangup(). The full example, with the browser, is in Live IVR calls with WebRTC.

Events

Options

When WebRTC drops

The transport event tells you where the audio is going and why: During the switch the audio pauses for a few seconds and the call goes on. Corporate networks that block UDP stay on the WebSocket.

Migrating from the manual WebSocket

If you followed Stream Calls, Step 1 (the backend calling POST /calls/stream) does not change. Steps 2 to 5 become one line:
The events your code already handles arrive unchanged in call.on("event").

Other languages

The SDK speaks an open protocol on the same WebSocket as wsUrl. To implement it in another language, with any WebRTC library: If something fails, you get {"type":"webrtc.error","code":"..."} with unavailable, invalid_offer, at_capacity, gateway_error or not_negotiated, and the audio stays on the WebSocket. Clients that never send these messages keep working exactly as before.

Next steps

Stream Calls

The WebSocket protocol, the events and recordings.

Receive Calls

Enable receiving and answer with the SDK.