Newer Version Available

This content describes an older version of this product. View Latest

Telephony Integration REST API Sample Code

The following JavaScript code sample performs the authorization process and then invokes the Voice API.

If you don’t have a My Domain deployed in your org, your URL formats are different. For details, see My Domain URL Formats in Salesforce Help.

Note

1const jwt = require('jsonwebtoken');
2const SSM = require('aws-sdk/clients/ssm');
3const uuid = require('uuid/v1');
4
5// SCRT EndPoint Base example: https://MyDomainName.my.salesforce-scrt.com/telephony/v1 
6// Contact your admin to get the exact URL. This should have been configured as part 
7// of the Service Cloud Voice Setup.
8
9const axios = require('axios').create({
10    baseURL: "ScrtEndpointBase"
11});
12
13// Example function to retrieve privateKey parameter using Amazon Systems Manager 
14async function getSSMParameterValue(paramName, withDecryption) {
15    return await new Promise(resolve => {
16        const ssm = new SSM();
17        const query = {
18            Names:  [paramName],
19            WithDecryption: withDecryption
20        };
21        
22        ssm.getParameters(query, (err, data) => {
23            let paramValue = null;
24            
25            if (!err && data && data.Parameters && data.Parameters.length) {
26                paramValue = data.Parameters[0].Value;
27            }
28            
29            resolve(paramValue);
30        });
31    });
32}
33
34// Generate a JWT based on the specified parameters.
35async function generateJWT(params) {
36    const { privateKeyParamName, orgId, callCenterApiName, expiresIn } = params;
37    // Retrieve privateKey from the param store
38    const privateKey = await getSSMParameterValue(privateKeyParamName, true);
39    const signOptions = {
40        issuer: orgId,
41        subject:  callCenterApiName,
42        expiresIn:  expiresIn,
43        algorithm:  'RS256',
44        jwtid: uuid()
45    };
46
47    return jwt.sign({}, privateKey, signOptions);
48}
49
50// Function to Create VoiceCall 
51async function createVoiceCall(fieldValues) {
52    const generateJWTParams = {
53        privateKeyParamName   : "YourPrivateKeyParamName",
54        orgId                 : "YourSalesforceOrganizationId",
55        callCenterApiName     : "YourSalesforceCallCenterAPIName",
56        expiresIn             : "TokenExpiryTime"
57    };
58    
59    const jwt = await generateJWT(generateJWTParams);
60    
61    var fieldValues = {
62        "vendorCallKey":"5324881f-1e84-4367-8930-f69a74b30ca6",
63        "callCenterApiName":"HVCC",
64        "to":"8002345678",
65        "from":"4081456688",
66        "initiationMethod":"Inbound",
67        "startTime":"2020-07-13T11:43:01Z",
68        "participants":
69            [{"participantKey":"+18033568299",
70            "type": "END_USER"
71            }]
72    };
73    
74    const response = await axios.post('/voiceCalls', fieldValues, {
75        headers: {
76            'Authorization': `Bearer ${jwt}`,
77            'Content-Type': 'application/json'
78        }
79    })
80    .then(response => { 
81        return response;
82    })
83    .catch(error => {
84        let context = {};
85        if (error.response) {
86            // The request was made and the server responded with 
87            // a status code that falls out of the range of 2xx
88            context = error.response.data;
89        } else if (error.request) {
90            // The request was made but no response was received
91            context = error.request;
92        } else {
93            // Something happened in setting up the request that triggered an error
94            context = error.message;
95        }
96         throw new Error('Error creating VoiceCall record');
97    });
98
99    return response.data;
100}