Prerequisites
- A modern web browser with WebRTC support. For more information, refer to Supported Browsers.
- Node.js version 18 or later.
- A web application where you can install npm packages.
- The web application must be served over HTTPS or from
localhost. WebSocket and UDP traffic must be allowed for SIP signaling and media transport.
- The web application must be served over HTTPS or from
- A Cognigy.AI Flow with a configured Voice Gateway Endpoint.
Install the SDK
-
Install the SDK in your web application project using npm:
-
Verify the installation:
Configure the Client
Import the SDK, verify browser support, and create a client instance:endpointUrl parameter must point to a valid Cognigy Voice Gateway Endpoint:
- In the Configuration section of the Voice Gateway Endpoint, copy the URL from the Endpoint URL field.
-
Replace
wss://withhttps://and remove/voiceGatewayfrom the path. For example, if your Endpoint URL is:Then yourendpointUrlwill be:
(Optional) Configure Custom STUN/TURN Servers
For advanced use cases, such as specifying custom STUN or TURN servers, provide apcConfig object when creating the client.
Use pcConfig only if your application requires custom NAT traversal or TURN servers. Otherwise, the SDK uses default servers automatically.
Register Event Listeners
Set up event listeners before making any calls:Audio playback is automatic. You don’t need to add an
<audio> element to your HTML. For advanced use cases, such as custom audio processing or visualization, refer to Custom Audio.Start and End a Call
With the client configured and events registered, start a voice call:Run and Verify
- Combine the code from the previous sections into a single function and bind it to a button click in your web application. A call must be initiated by a user gesture, for example, a button click, because browsers block audio autoplay without user interaction. Refer to the Complete Example.
- Start your application and open it in a supported browser.
- Open the browser developer console.
- Click the button that triggers the call. You should see the following messages in the console:
Connecting to SIP server...— the SDK is establishing a connection.SIP client registered— registration succeeded.Call answered. Session ID: ...— the call is active and audio should be playing.