How to Implement External HTTP Callbacks

You can make HTTP calls into the GameSparks platform either from external servers or from your own processes. When these calls are made, you can execute some custom Cloud Code to do whatever processing you need:

Set SparkHttp timeout! The default timeout for SparkHttp calls is 30 seconds but we strongly recommend that you set this at 5 seconds or lower. If there's any delay in receiving a reply from the endpoint, then waiting for the default 30 second period could seriously impact your Cloud Code's performance. You can use Spark.getHttp() to set the timeout.

Creating Cloud Code to Handle Requests

To create a script to handle the request from the Cloud Code page, under Scripts select System, and use Callback URL.

The Callback URL


Trailing Slash Required! If you don't pass a playerId, you still need to include the / after your serverSecret.

Adding Parameters

For a GET request, you can add parameters to the query string that are passed to your script. For example:


With this URL, the following code can be used to access these parameters:

    //attribute1 is a string
    var attribute1 = Spark.getData().attr1;

    //attribute2 is an array
    var attribute2 = Spark.getData().attr2;

    //attribute2[0] is bcd
    //attribute2[1] is cde

Producing Response Body

Using Script Data to Send Back JSON Response

The scriptData object from the callback script is written to the output stream as a JSON document.

    Spark.setScriptData("key1", "value1");
    Spark.setScriptData("key2", "value2");

This will generate the JSON output:

        "key1": "value1",
        "key2": "value2"

Using RESPONSE_RAW for Text-Based Output

If you do not want to write JSON but want to use your own text-based output, setting a value with the key "RESPONSE_RAW" in script data will override the JSON output and return this value directly;

    Spark.setScriptData("RESPONSE_RAW", "OK");

Will generate the response as :


Note: If you use setScriptData and not RESPONSE_RAW, then RESPONSE_HEADERS is a reserved word so it won't appear in the JSON output.

Producing Headers

Setting Response Headers

You can set response headers in the output:

Spark.setScriptData("RESPONSE_HEADERS", {"myHeader":"myValue"})

Other ways to set ResponseHeaders:

You can specify the Content-Type Response Header by passing in the following parameters: contentType and characterEncoding like this:


If these parameters are passed in but the Content-Type is also passed-in in the RESPONSE_HEADERS, the RESPONSE_HEADERS will be used.

Reading POST Data

There are instances where a remote server may not set the content-type header correctly, and the post data are not automatically translated into Spark.getData(). In these cases, the POST requests have an additional "REQUEST_BODY" attribute set into Spark.getData(). This is the body of the HTTP post request as a string, so you can do your own parsing of this value:

    var rawPostData = Spark.getData().REQUEST_BODY