question

Kim Strasser avatar image
Kim Strasser asked

PlayFab Rules: What is an external API?

I found the following code in this article:

https://api.playfab.com/docs/tutorials/landing-automation/using-cloud-script-actions-with-playstream

// Post data about the event to an external API
var content = JSON.stringify({user: profile.PlayerId, event: psEvent.EventName});
var response = http.request('https://httpbin.org/status/200', 'post', content, 'application/json', null, true);
	
return { externalAPIResponse: response };

What is an external API? For what purposes is an external API normally used when a Cloud Script function is triggered by Rules?

Can I use an external API to get the values of profile.PlayerId and psEvent.EventName in the client code in result.Result.FunctionResult of ExecuteCloudScript API?

For example:

private async Task UpdatePlayerHighscore(string leaderboardname, string playerscore)
{
    var result = await PlayFabClientAPI.ExecuteCloudScriptAsync(new ExecuteCloudScriptRequest()
    {
        FunctionName = "UpdateLeaderboard",
        FunctionParameter = new { Leaderboardname = leaderboardname, Playerscore = playerscore, Level = 1 },
        GeneratePlayStreamEvent = true
    });

    if (result.Error != null)
        Console.WriteLine(result.Error.Error.ToString());
    else
    {
        if (result.Result.FunctionResult != null)
            Console.WriteLine(result.Result.FunctionResult.ToString());
    }
}
CloudScript
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

·
Turner avatar image
Turner answered

In this case, "External API" means that it isn't hosted by or affiliated with PlayFab. So in this particular instance, we are making a request to httpbin, which isn't affiliated with PlayFab.

Regarding your second question, you can get the EventName and the PlayerID using Cloud Script. If you're new to CloudScript, you can view the Quickstart here.

2 comments
10 |1200

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

Kim Strasser avatar image Kim Strasser commented ·

I have another question about the second point. Is it possible to return the value of a Cloud Script function that is triggered by PlayFab Rules in the client code?

For example, I have the following Rule and Cloud Script function. But I don't know how to return the value of playerresult in the client code because the client has not directly called the function handlePlayStreamEventFirstTimeLevelFinished with ExecuteCloudScript API. handlePlayStreamEventFirstTimeLevelFinished is triggered by Rules and not by ExecuteCloudScript API.

Is it still possible to return the value of playerresult in the client code?

handlers.handlePlayStreamEventFirstTimeLevelFinished = function (args, context)
{
    var playerresult = "";
    var level = context.playStreamEvent.Level;
    var playerscore = context.playStreamEvent.Playerscore;
    var rating = "";
    
    if (Number(playerscore) >= 150)
        rating = "This was amazing!";
    else
        rating = "You should practice more!";
    
    playerresult = "Level: " + level + " Your highscore: " + playerscore + " Rating: " + rating;

    return playerresult;
}
0 Likes 0 ·
Turner avatar image Turner Kim Strasser commented ·

Unfortunately, if the client needs the CloudScript function's response, then you need to call that function in your client.

A possible workaround would be to store the data using something like UpdateUserData, then retrieve it client-side using GetUserData, but I wouldn't recommend this approach.

What I would recommend in this particular scenario is to try to convert the situation so that the client is making the call to your Cloud Script function.

0 Likes 0 ·

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.