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 enhanced domains aren’t enabled in your org, your URL format is 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            'Telephony-Provider-Name': 'Amazon Connect'
79        }
80    })
81    .then(response => { 
82        return response;
83    })
84    .catch(error => {
85        let context = {};
86        if (error.response) {
87            // The request was made and the server responded with 
88            // a status code that falls out of the range of 2xx
89            context = error.response.data;
90        } else if (error.request) {
91            // The request was made but no response was received
92            context = error.request;
93        } else {
94            // Something happened in setting up the request that triggered an error
95            context = error.message;
96        }
97         throw new Error('Error creating VoiceCall record');
98    });
99
100    return response.data;
101}