question

Wisarut Kiatboonsri by Playfab avatar image
Wisarut Kiatboonsri by Playfab asked

JavaScript Azure Function call Server API Example

For some reason our backend team write azure function in JavaScript. Can anyone give us an example of Azure Function that write in JS and call some simple server api.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Rick Chen avatar image
Rick Chen answered

You could do the following steps to implement the JavaScript Azure Function and calling a server API.

var PlayFab = require("playfab-sdk/Scripts/PlayFab/PlayFab");
var PlayFabServer = require("playfab-sdk/Scripts/PlayFab/PlayFabServer");


const yourDeveloperSecretKey = "your developer secret key here";


module.exports = function (context, req){
    PlayFab.settings.titleId = req.body.TitleAuthenticationContext.Id;
    PlayFab.settings.developerSecretKey = yourDeveloperSecretKey;


    var getdataRequest = {
        PlayFabId: req.body.CallerEntityProfile.Lineage.MasterPlayerAccountId,
        Keys: req.body.FunctionArgument.keys,
    }


    context.log("calling the get user api");


    PlayFabServer.GetUserData(
        getdataRequest,
        (error, result)=>{
            if (result !== null) {
                context.log("API call was successful.");
                context.res = {
                    status: 200,
                    body: result
                };
                context.done();
            } else if (error !== null) {
                context.log("Something went wrong with the API call.");
                context.log("Here's some debug information:");
                context.log(CompileErrorReport(error));
                context.res = {
                    status: 500,
                };
                context.done();
            }
        }
    )
}


function CompileErrorReport(error) {
    if (error == null)
        return "";
    var fullErrors = error.errorMessage;
    for (var paramName in error.errorDetails)
        for (var msgIdx in error.errorDetails[paramName])
            fullErrors += "\n" + paramName + ": " + error.errorDetails[paramName][msgIdx];
    return fullErrors;
}


  • Replace your index.js with the code above and replace the yourDeveloperSecretKey variable with the developer secret key of your title.
  • Re-deploy the Azure Function.
  • Follow this document to register the azure function in your title: Quickstart: Writing a PlayFab CloudScript using Azure Functions.
  • Login as a user to your title and add some player data to this user. You can do it in [Your Game Manager]->[Players]->search this user->[Player Data(Title)].
  • After login, call the ExecuteFunction API, with the following request body:
{
  "FunctionName": "YourAzureFunctionName",
  "FunctionParameter": {
    "Keys": [
    "preferences",
    "progress"
  ]
  }
}


  • Replace the function name with your azure function name, and the keys with the player data you created previously.
  • Then the API should return the result similar to below:
{
    "code": 200,
    "status": "OK",
    "data": {
        "ExecutionTimeMilliseconds": 2409,
        "FunctionName": "YourAzureFunctionName",
        "FunctionResult": {
            "code": 200,
            "status": "OK",
            "data": {
                "PlayFabId": "1FCAC4066224C1FB",
                "DataVersion": 1,
                "Data": {
                    "preferences": {
                        "Value": "fish&chips",
                        "LastUpdated": "2021-07-20T08:28:54.223Z",
                        "Permission": "Private"
                    },
                    "progress": {
                        "Value": "50%",
                        "LastUpdated": "2021-07-20T08:28:54.223Z",
                        "Permission": "Private"
                    }
                }
            }
        }
    }
}


10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.