RingCentral Client
This is a library implemented in TypeScript which provides convenient APIs for TypeScript and JavaScript developers to access RingCentral webservice(https://developer.ringcentral.com/api-docs/latest/index.html).
Table of contents
Getting started
Install
npm install ringcentral-client --save
npm install ringcentral
yarn add ringcentral-client ringcentral
Import the module
In Typescript or ES6 (Recommended)
import RingCentralClient , { SERVER_SANDBOX } from " ringcentral-client " ;
import SDK from " ringcentral " ;
In commonjs(node.js, webpack and browserify)
var ringcentral = require ( " ringcentral-client " ) ;
var RingCentralClient = ringcentral . Client ;
var SERVER_SANDBOX = ringcentral . SERVER_SANDBOX ;
var SDK = require ( " ringcentral " ) ;
Used in browser through the prebuilt javascript bundle
All APIs are exposed on the global variable RingCentral
.
< script type = " text/javascript " src = " node_modules/ringcentral-client/build/ringcentral-client.min.js " > < / script >
< script type = " text/javascript " >
console . log ( " All api " , RingCentral ) ;
var RingCentralClient = RingCentral . Client ;
var SERVER_SANDBOX = RingCentral . SERVER_SANDBOX ;
var SDK = RingCentral . SDK ;
</ script >
Quick Start
Login, logout, get account info.
const sdk = new SDK ( {
server : SERVER_PRODUCTION ,
appKey : process . env . APP_KEY ,
appSecret : process . env . APP_SECRET
} ) ;
const client = new RingCentralClient ( sdk ) ;
sdk . platform ( ) . login ( {
" username " : process . env . USERNAME ,
" extension " : process . env . EXTENSION ,
" password " : process . env . PASSWORD
} ) . then ( ( ) => {
console . log ( " Login success " ) ;
return client . account ( ) . get ( ) ;
} ) . then ( ( accountInfo ) => {
console . log ( " Current account info " , accountInfo ) ;
return sdk . platform ( ) . logout ( ) ;
} ) . then ( ( ) => {
console . log ( " logout success " ) ;
} ) . catch ( e => {
console . error ( " Error occured " , e ) ;
} ) ;
API Call Examples
Telephony Calls
Make phone calls by ringout(https://developer.ringcentral.com/api-docs/latest/index.html#!#MakeRingOut.html):
client . account ( ) . extension ( ) . ringout ( ) . post ( {
from : { phoneNumber : " xxx " } ,
to : { phoneNumber : " xxx " } ,
callerId : { phoneNumber : " xxx " }
} ) . then ( ringout => {
console . log ( " Ringout sucess " , ringout ) ;
} , e => {
console . error ( " Fail to ringout " , e ) ;
} ) ;
Track the telephony status
To get notications when calls come in, go out or ends, subscribe to the Presence Event :
let subscription = client . createSubscription ( ) ;
subscription . on ( subscription . events . notification , function ( msg ) {
let presenceEvt = msg . body ;
console . log ( " @@@@presence event " , presenceEvt ) ;
console . log ( " telephonyStatus " , presenceEvt . telephonyStatus ) ;
console . log ( " activeCalls " , presenceEvt . activeCalls ) ;
} ) ;
subscription
. setEventFilters ( [ ' /account/~/extension/~/presence?detailedTelephonyState=true ' ] )
. register ( )
. then ( ( subscription ) => {
console . log ( " Subscription created " , subscription . json ( ) ) ;
} , e => {
console . error ( " Fail to create subscription " , e ) ;
} ) ;
View the list of active calls
client . account ( ) . extension ( ) . activeCalls ( ) . list ( {
page : 1 ,
direction : " Inbound "
} ) . then ( results => {
console . log ( " Active calls " , results . records ) ;
} , e => {
console . error ( " Fail to get active calls " , e ) ;
} ) ;
View the recent calls
let dateFrom = new Date ( Date . now ( ) - 24 * 60 * 60 * 1000 ) ;
client . account ( ) . extension ( ) . callLog ( ) . list ( { dateFrom : dateFrom . toISOString ( ) } ) . then ( results => {
console . log ( " Recent call logs " , results . records ) ;
} , e => {
console . error ( " Fail to get call logs " , e ) ;
} ) ;
Send SMS
client . account ( ) . extension ( ) . sms ( ) . post ( {
to : [ {
phoneNumber : " {receiverPhoneNumber} "
} ] ,
from : {
phoneNumber : " {yourSmsNumber} "
} ,
text : " Sms content "
} ) . then ( function ( messageInfo ) {
console . log ( " Sms sent successfully " , messageInfo ) ;
} ) . catch ( function ( e ) {
console . error ( " Fail to send sms " , e ) ;
} ) ;
Send Fax
For all supported options and mediatype, please refer to https://developer.ringcentral.com/api-docs/latest/index.html#!#RefFaxMessages.html .
import * as fs from " fs " ;
client . account ( ) . extension ( ) . fax ( ) . post ( {
to : [ { phoneNumber : " {receiverPhoneNumber} " } ] ,
faxResolution : ' High '
} , [
" {Message text} " ,
fs . createReadStream ( " {filePath} " )
] ) ;
}) ;
Extension management
Get detail information of an extension:
client . account ( ) . extension ( ' theExtensionId ' ) . get ( ) . then ( function ( extInfo ) {
console . log ( " The extension info " , extInfo ) ;
} ) . catch ( function ( e ) {
console . error ( " Get extension error " , e ) ;
} ) ;
List extensions of an account:
client . account ( " theAccountId " ) . extension ( ) . list ( ) . then ( function ( extensions ) {
console . log ( " The list of extension info " , extensions . records ) ;
} ) . catch ( function ( e ) {
console . error ( " Get extension list error " , e ) ;
} ) ;
Update infomation of an extension:
client . account ( ) . extension ( ) . put ( { status : " Enabled " } ) . then ( function ( ) {
console . log ( " Success to update extension. " ) ;
} ) . catch ( function ( ) {
console . error ( " Fail to update extension. " ) ;
} ) ;