Javascript SDK
  • 12 Oct 2021
  • 1 Minute to read
  • Contributors
  • Dark
    Light
  • PDF

Javascript SDK

  • Dark
    Light
  • PDF

Article Summary

Authentication is out of scope for this document, please check out https://docs.callable.io/v1/docs/rest-api for how to authenticate server to server.

Include module in react project

Using your favourite package manager, add the callable SDK

yarn add callable-data-objects

This will add the latest library to the package.json

{
   "dependencies": {
        "callable-data-objects": "^0.2.50",
   }
}

Create a new API instance

import { CallableAPI } from 'callable-data-objects';

// Grab auth token from where you saved it
const apiKey = sessionStorage.getItem("authentication");

//Create new instance of callable api
const api = new CallableAPI("https://api.control.callable.io", apiKey);

Create new phone instance

import { CallablePhone } from 'callable-data-objects';

//Grab the associated phoneUser data of authenticated user
const phoneUser = await api.userPhone.phone();

//A phone user can have 0 to many WebRTC clients, lets assume they have 1 client
const client = phoneUser.clients[0];

//Pass  the client, url & apikey to the constructor of the CallablePhone
const phone = new CallablePhone({
  url: "https://api.control.callable.io",
  apiKey: apiKey,
  client: client
});

//add event listeners
phone.on('message', (messages) => console.log({message}, "websocket message") );
phone.on('update', (event) => console.log({event}, "session/client update") );

//start phone
phone.start();

...

//cleanup
phone.stop();

What's Next