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.
| Parameter | Value |
|---|---|
| Package | @2chat/voice-sdk |
| CDN | https://cdn.jsdelivr.net/npm/@2chat/voice-sdk/dist/index.global.js |
| Module formats | ESM, CJS, UMD/global (TwoChatVoice) |
| Framework deps | None — works with React, Vue, Svelte, or vanilla JS |
| Transport | SIP 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-Keystays on your backend. - Auto reconnect on dropped WebSockets, with
reconnecting/reconnectedevents. - 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 │
└────────────────┘ └────────────┘
- Your backend mints a short-lived SDK JWT for a 2Chat user using your
X-User-API-Key. - The browser hands that JWT to the SDK and calls
device.register(). - 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-Keyand 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
- Quickstart — install the SDK and place your first call.
- Authentication — mint short-lived JWTs from your backend.
- Device API — full reference for the
Deviceclass. - Call API — full reference for the
Callclass. - Errors — error codes you can switch on.