JavaScript SDK Setup

The GameSparks JavaScript SDK allows you to interact with the GameSparks platform from any JavaScript environment.

Getting the SDK

Download the SDK, and example HTML here:

JavaScript SDK

Adding the SDK to your Web Page

There are two core JavaScript files included with the SDK:

To include the files in your HTML page, use the following script tags to reference the files.

    <script type="text/javascript" src="gamesparks.js"></script>
    <script type="text/javascript" src="gamesparks-functions.js"></script>

The example application also uses a script to perform HMAC SHA-256 operations on the SDK for security.

    <script type="text/javascript" src="hmac-sha256.js"></script>

This is used for the example project, but it is assumed you will secure the secret on your own server.

Initializing the SDK

There are two initialization methods in the SDK, one for using the preview servers (for your internal use and testing) and another for the live players. It's important that you don't try to use the preview servers for your live players because the preview servers have a limit imposed on the number of concurrent connections for each game by Pricing Tier - please see our Usage Limits and Fair Usage Policy webpage for details.

Firstly, you need to create a GameSparks object:

    //Create a gamesparks object to be used
    var gamesparks = new GameSparks();

Once you have an object reference, you can initialize the SDK. To do this, you need to call one of the initialization methods with an options object. To initialize against the preview servers use the following function:

     key:<YOUR API KEY>,
     secret: <YOUR API SECRET>,
     onNonce: <HMAC FUNCTION>,
     logger: console.log,

To initialize against the live servers use the following function:

     key:<YOUR API KEY>,
     secret: <YOUR API SECRET>,
     onNonce: <HMAC FUNCTION>,
     logger: console.log,

Initialization Options

key : The API Key of your game from the GameSparks developer portal.

secret : The API Secret of your game from the GameSparks developer portal.

onNonce : A JavaScript function that takes a string and returns an SHA-256 HMAC of the string using your secret. A simple example below:

    //Callback function to hmac sha256 a nonce with the secret. It's assumed you will have your own method of securing the secret;
    function onNonce(nonce){
        return CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(nonce, <YOU API SECRET>));

Note: The onNonce is optional and is not required for initialization.

onInit : A JavaScript function that is called when the SDK is finished initialising. You can use this function to perform a common task, such as authenticating the user when the SDK is ready.

    //Callback to handle when the SDK is initialized and ready to go
    function onInit(){

onMessage : A JavaScript function that takes a JS Object that was sent to the client asynchronously. This function is called whenever a Message object is sent from the server to the client.

    //Callback to handle async messages from the gamesparks platform
    function onMessage(message){

logger : Points to a custom logger function defined by the user and can be used to print out log entries

Sending Requests to GameSparks

A number of helper methods exist in the gamesparks object to allow you to make requests. See gamesparks-functions.js for details of each.

A method exists for each GameSparks request object, with each of the parameters required in the request as function arguments.

Each method take a callback function as the final parameter to be called when the response is received from the GameSparks platform. This function should take a single object parameter. A basic authentication example is shown below.

    gamesparks.authenticationRequest("testuser", "testuser", loginResponse);

    function loginResponse(response){

Custom requests, which allow you to post events to the GameSparks platform can be achieved by using the sendWithData method. See the example below for sending a LogEventRequest for an Event with shortCode "FIRST_EVENT" and 3 attributes "NUMBER_ATTR", "STRING_ATTR" and "JSON_ATTR"

            eventKey : "FIRST_EVENT",
            NUMBER_ATTR : 123,
            STRING_ATTR : "this is a string",
            JSON_ATTR : {key1 : 12, key2 : "abc"}