Skip to main content

2Chat Voice SDK

@2chat/voice-sdk is an embeddable, framework-agnostic browser SDK that lets your web app place and receive phone calls through 2Chat. It speaks SIP over WebSockets (JsSIP under the hood) and exposes a small Device / Call API on top.

ParameterValue
Package@2chat/voice-sdk
CDNhttps://cdn.jsdelivr.net/npm/@2chat/voice-sdk/dist/index.global.js
Module formatsESM, CJS, UMD/global (TwoChatVoice)
Framework depsNone — works with React, Vue, Svelte, or vanilla JS
TransportSIP over Secure WebSocket + WebRTC media

What you can build

  • Outbound and inbound phone calls from a browser tab
  • Click-to-call inside a CRM, helpdesk, or internal admin panel
  • Multi-agent softphones (mint one token per agent)
  • Custom call UIs on top of your 2Chat virtual numbers and caller IDs

Feature highlights

  • Place outbound calls to any E.164 number using your 2Chat caller IDs and virtual numbers.
  • Receive inbound calls routed to a connected user — accept, reject (486), or let them ring.
  • Mute, hold, send DTMF.
  • Short-lived JWT auth — your long-lived X-User-API-Key stays on your backend.
  • Auto reconnect on dropped WebSockets, with reconnecting / reconnected events.
  • Audio device selection — pick microphone and output devices, switch them mid-call.
  • Pluggable ICE servers when you need to override defaults.

How it fits together

 ┌────────────────┐         POST /sdk/voip/access-token          ┌──────────────────┐
│ Your backend │ ──────────────────────────────────────────▶ │ 2Chat API │
│ (holds X-User- │ │ (mints SDK JWT) │
│ API-Key) │ ◀──────────── short-lived JWT ───────────── │ │
└───────┬────────┘ └──────────────────┘
│ token (≤ 60 min)

┌────────────────┐ GET /sdk/voip/sip-credentials ┌──────────────────┐
│ Browser │ ───────────────────────────────────────────▶ │ 2Chat API │
│ @2chat/voice- │ ◀───────────────── SIP creds ────────────── │ │
│ sdk │ └────────┬─────────┘
│ │ SIP/WSS + WebRTC media │
│ │ ────────────────────────────────────────────────────▶ │ FreeSWITCH │
└────────────────┘ └────────────┘
  1. Your backend mints a short-lived SDK JWT for a 2Chat user using your X-User-API-Key.
  2. The browser hands that JWT to the SDK and calls device.register().
  3. The SDK fetches SIP credentials, registers with the SIP server, and is ready to place or receive calls.

Prerequisites

  • A 2Chat account with at least one virtual phone number or registered caller ID.
  • A backend that can hold your X-User-API-Key and expose a small endpoint your frontend can call to fetch a fresh JWT.
  • One 2Chat User per concurrent agent (each token is scoped to a single user UUID — see List Account Users).

What's next

  1. Quickstart — install the SDK and place your first call.
  2. Authentication — mint short-lived JWTs from your backend.
  3. Device API — full reference for the Device class.
  4. Call API — full reference for the Call class.
  5. Errors — error codes you can switch on.