question

Andrea Galet avatar image
Andrea Galet asked

How can i call a PlayFab REST API inside a cloud Script Function?

I would like to call a PlayFab REST API, specifically the List Multiplayer Servers from the CloudScript Function, which will be called in Unity, in this way i can call it from a client without enabling the admin API on it

unity3dCloudScript
1 comment
10 |1200

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

Andrea Galet avatar image Andrea Galet commented ·

For Example in the old Legacy version i would do something like this:

handlers.ListVMSummaries = function (args, context) {    var VMSummaries = multiplayer.ListVirtualMachineSummaries({        "BuildId": "[YourBuildId]",        "Region": "EastUs", //Your region.    });    return VMSummaries;};
0 Likes 0 ·

1 Answer

·
Gosen Gao avatar image
Gosen Gao answered

If the “CloudScript Function” you mention means PlayFab CloudScript using Azure Functions - PlayFab | Microsoft Learn, then you can follow PlayFab CloudScript using Azure Functions Quickstart Guide - PlayFab | Microsoft Learn to set up an Azure Function to call Multiplayer Server - List Virtual Machine Summaries - REST API (PlayFab Multiplayer) | Microsoft Learn. You can refer to the code below.

FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync()); 
var args = context.FunctionArgument;


var authContext = new PlayFabAuthenticationContext();
authContext.EntityToken = context.TitleAuthenticationContext.EntityToken;


var apiSettings = new PlayFab.PlayFabApiSettings()
{
    TitleId = Environment.GetEnvironmentVariable("PLAYFAB_TITLE_ID", EnvironmentVariableTarget.Process),
    DeveloperSecretKey = Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", EnvironmentVariableTarget.Process)
};


var mpApi = new PlayFabMultiplayerInstanceAPI(apiSettings, authContext);


var ret = await mpApi.ListVirtualMachineSummariesAsync(new PlayFab.MultiplayerModels.ListVirtualMachineSummariesRequest{
    BuildId = args.buildId,
    Region = args.region
});
return new OkObjectResult(ret);
4 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.

Andrea Galet avatar image Andrea Galet commented ·

That's it, thanks, i didn't install the PlayFab SDK via Package Manager as shown in the quickstart

0 Likes 0 ·
Andrea Galet avatar image Andrea Galet commented ·

Last question, i hard coded this part with my title and secret key, is it right?:

 
                  
  1. TitleId=Environment.GetEnvironmentVariable("PLAYFAB_TITLE_ID",EnvironmentVariableTarget.Process),
  1. DeveloperSecretKey=Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY",EnvironmentVariableTarget.Process)
0 Likes 0 ·
Gosen Gao avatar image Gosen Gao Andrea Galet commented ·

Yes, this part should be your Title and Developer Secret Key.

1 Like 1 ·

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.