# Connect and Call

> Load the Edesy JavaScript SDK, connect with a token and place outbound phone calls from the browser with device.call().

Source: https://edesy.in/docs/webrtc/sdk/connect-and-call

The Edesy JavaScript SDK has one entry point, `connect()`. Pass it the JSON from your token endpoint, or the URL of that endpoint, and it resolves with a `Device` once the browser is registered. Call `device.call(number)` to dial a phone number. It returns a `Call` object with events and controls.

## SDK reference

| API | Returns | Description |
|---|---|---|
| `connect(config)` | `Promise<Device>` | `config` is the token JSON object, or a URL string the SDK `POST`s to for that JSON. Resolves once registered |
| `device.call(number)` | `Call` | Dials a phone number (country code, no plus) |
| `device.on(event, cb)` | `Device` | `ready`, `unregistered`, `error`, `incoming` |
| `device.disconnect()` | - | Unregisters and closes the connection |
| `call.on(event, cb)` | `Call` | `ringing`, `answered`, `ended`, `failed`, `mute`, `hold`, `dtmf` |
| `call.hangup()` | - | Ends the call |
| `call.toggleMute()` | - | Mutes or unmutes the microphone |
| `call.toggleHold()` | - | Holds or resumes the call |
| `call.sendDigit(d)` | - | Sends a DTMF digit |
| `call.isHeld` | boolean | Whether the call is on hold |
| `EdesyVoice.version` | string | SDK version (hosted bundle) |

## Install

Hosted bundle (no build step). It sets a global `EdesyVoice`:

```html
<script src="https://webrtc.edesy.in/sdk/edesy-voice.js"></script>
```

Or the package:

```bash
npm install @edesy/voice-sdk
```

```js
import { connect } from "@edesy/voice-sdk";
```

## Connect

With a token URL. The SDK sends a `POST` with no body and throws if the response is not OK:

```js
const device = await EdesyVoice.connect("https://your-app.example.com/edesy-token");
```

With the JSON you already fetched:

```js
const config = await fetch("/edesy-token", { method: "POST" }).then((r) => r.json());
const device = await connect(config);
```

`connect()` resolves after registration, so a `ready` handler attached afterwards still runs straight away.

```js
device.on("ready", () => console.log("registered"));
device.on("error", (e) => console.error("device error", e));
```

## Place a call

```js
const call = device.call("919876543210");

call.on("ringing", () => showStatus("Ringing"));
call.on("answered", () => showStatus("In call"));
call.on("ended", () => showStatus("Call ended"));
call.on("failed", (e) => showStatus("Call failed"));

hangupButton.onclick = () => call.hangup();
```

## How it works

1. **Register.** `connect()` opens a secure WebSocket to the `server` in your token and registers with the short-lived credentials.
2. **Dial.** `device.call()` starts a WebRTC call using the `iceServers` from your token.
3. **Bridge.** Edesy places the call to the phone network with your `callerId` and bridges the audio.
4. **Clean up.** Call `device.disconnect()` when the user signs out or leaves the page.

## FAQ

### Can I place more than one call per connection?

Yes. Connect once, then call `device.call()` for each call. Keep the `Device` for the whole session.

### What happens if the token URL returns an error?

`connect()` rejects with an error that includes the HTTP status, for example `token endpoint 401`.

### Does `call()` need a plus sign?

No. Use the country code followed by the number, for example `919876543210`.

### Which version am I running?

With the hosted bundle, read `EdesyVoice.version`.

### How do I know the device is ready?

`connect()` resolves only after registration. You can also listen for `ready`.

### How do I end everything when the user logs out?

Hang up any active call with `call.hangup()`, then call `device.disconnect()`.

### Can I use it inside a CRM?

Yes. Any web page that can load a script can use the SDK. For zero code, use the [drop-in dialer](https://edesy.in/docs/webrtc/drop-in-dialer).
