Nodejs SDK

Info Contents
Version 2.0.0
Support devsupport@superawesome.tv
License GNU Lesser General Public License Version 3

Instantiating the SDK

var KwsSdk = require('sa-kws-node-sdk');

// KWS will provide all the necessary info for your account
var kwsSdk = new KwsSdk({
    clientId: 'your_client_id',
    apiKey: 'your_api_key',
    kwsApiHost: 'https://examplekwsapihost.com',

    // cross domain options
    /*(optional)*/ allowedDomains: ['yourfrontenddomain.com'], // array of allowed frontend domains
    /*(optional)*/ loginRedirectUrl: 'https://yourloginurl.com',  // url to be redirected after the login
    /*(optional)*/ logoutRedirectUrl: 'https://yourlogouturl.com',  // url to be redirected after the logout
    /*(optional)*/ debug: true,  // by default equal false. will log the errors in the console using the logger options
    /*(optional)*/ logger: console.warn  // by default equal to console.log
});

Adding controller handlers for oauth callbacks

// assuming that app is your express instance object
app.use(kwsSdk.router);

Get Users Map

This call allows you to get the number of users aggregated by area (postal code related).

  • Function: app.user.getMap

  • Parameters:

    • countryCode: (string, required) ISO 3166-1 2 chars code
  • Response: It depends on the country. This could be an example for UK

{
    "n1": 200,
    "n12": 200,
    "n233": 200,
    "n244": 200,
    "n355": 200,
    "n366": 55,
    ...
}
  • Example:
kwsSdk.app.user.getMap({countryCode:'GB'})
    .then(function (data) {
        // Your resp handler here
    })
    .catch(function (err) {
        // Your error handler here
    });

Get App Statistics

This function allows you to get the total points that have been awarded by the app.

  • Function: app.getStatistics

  • Parameters: none

  • Response:

    • totalPointsAwarded (Number)

    Example:

{
    "totalPointsAwarded": 1054782
}
  • Example:
kwsSdk.app.getStatistics()
    .then(function (data) {
        // Your resp handler here
    })
    .catch(function (err) {
        // Your error handler here
    });

Notify

This function allows you to send a notification to one or more users of the app.

  • Function: app.notify

  • Parameters:

    • targetUserIds (Array of integers, required) ids of the target users
    • description (string, required) message to be sent (html accepted)
  • Response: Empty

  • Example:

kwsSdk.app.notify({
    targetUserIds: [125],
    description: 'Thank you!<br/><br/>Your welcome package has been sent!'
}).then(function () {
    // Your resp handler here
}).catch(function (err) {
    // Your error handler here
});

User functions

Requests coming from the frontend will have the possibility to make calls related to the user session. These kind of calls are preferably made from the frontend directly, but there might be some ocassions when doing it from the backend can be useful.

The following example is a middleware that injects the user profile data in the req object:

app.use(function (req, res, next) {

    // saUserSdk is now injected in the req obj and allows to make calls with the user session
    req.saUserSdk.user.get()
        .then(function (userData) {
            req.user = userData;
            next();
        })
        .catch(function () {
            // No user data could be fetched
            req.user = null;
            next();
        });
});

The functions that are available from the userSdk object are the same as in the frontend SDK. Please see Frontend SDK reference for details.