Purecloud Node.js library not working

I installed purecloud_api_sdk_javascript and purecloud and used the below sample code:

// Node.js - Client credentials strategy
var pureCloudSession = require('purecloud_api_sdk_javascript');
var purecloud = require("purecloud");

var session = purecloud.PureCloudSession({
strategy: 'client-credentials',
clientId: 'my keys',
clientSecret: 'my keys'
});

// Browser - Implicit strategy

pureCloudSession.login()
.then(function() {
// Do authenticated things
console.log("authenticated");
});

but getting the above error. I tried to search in forum. I am trying to integrate pure node.js application with pure cloud. Is purecloud api provided for node.js stable? or I am doing something wrong?

remove line 2, this code is working for me

var purecloud = require('purecloud_api_sdk_javascript');

var session = purecloud.PureCloudSession({
    strategy: 'client-credentials',
    clientId: process.env.purecloud_client_id,
    clientSecret: process.env.purecloud_secret,
    timeout: 10000,
    environment: 'mypurecloud.com'
});

session.login()
1 Like

Hey Kevin, Thank you very much. I am noobs to purecloud .I followed your suggestion and now, I am not getting any error. Can you tell me how do we write 'after login funcion'. I am trying to write equivalent for this example https://developer.mypurecloud.com/api/tutorials/call-list-management/#javascript .

the JS library uses async promises so you need a .then or .error chained after the login. from that tutorial you have

pureCloudSession.login().then(function(){
        addContactToList("John Doe", "3172222222");
    });

so everything that happens within the

.then(function(){
            addContactToList("John Doe", "3172222222");
        });

is called after the login succeeds

1 Like

Thank you Again for quick response. See

Still facing issue with that

var purecloud = require('purecloud_api_sdk_javascript');

var session = purecloud.PureCloudSession({
strategy: 'client-credentials',
clientId: 'xx',
clientSecret: 'xx',
timeout: 10000,
environment: 'mypurecloud.com'
});

purecloud.login()
.then(function() {
// Do authenticated things
console.log("authenticated");
});

should be session.login()

1 Like

Thank youl. This is another related issue which I have been facing when I tried this around a week back:

var purecloud = require('purecloud_api_sdk_javascript');

var session = purecloud.PureCloudSession({
strategy: 'client-credentials',
clientId: 'xx',
clientSecret: 'xx',
timeout: 10000,
environment: 'mypurecloud.com'
});

var outboundApi = new OutboundApi(purecloud);
var notificationsapi = new NotificationsApi(purecloud);

function addContactToList(name, number){
    var contactData = [
                        {
                            "contactListId": contactListId,
                            "data": {
                                "Name": name,
                                "Number": number
                            },
                            "callable": true
                        }
                        ];

    outboundApi.postContactlists.ContactlistId.Contacts(contactListId, contactData, true).then(function(){
        console.log("contact added to list")
    });
}

session.login().then(function() {
// Do authenticated things
addContactToList("John Doe", "3172222222");
});

I maybe wrong, but even after using your code, it doesn't gives any error or response. The samples provided in node.js by purecloud, most of them don't work.

The .then function isn't being called?

1 Like

.then function is being called directly.

that means you have an authenticated session, what is it not doing that you are expecting it to do?

I am writing a program to initiate simple login, and will print something after login is succesfull.