Quickstart: Writing a PlayFab CloudScript using Azure Functions
In this quickstart, you write a CloudScript using Azure Functions with Visual Studio Code, Azure Functions C# and Unity C#. After finishing this guide, you’ll be able to link your new CloudScript to rules, scheduled tasks or even call it from your client code.Prerequisites
There are a couple of steps needed to get started with PlayFab C# CloudScript.- Visit the Visual Studio Code QuickStart: Create an Azure Functions project using Visual Studio Code and return here once you’re set up. The following prerequisites are covered in their quickstart guide:
- An Azure account. Signing up for an Azure Account is free
- An Azure Subscription
- A Functions App resource configured in the Azure portal
- To minimize latency of your CloudScript using Azure Functions place them in the US-West, US-West 2, or US-West 3 Azure regions.
- Security Note: From a security perspective, you should make sure to only use a given function secret with PlayFab and not use it for calling the same function from any other source.
- Security Note: For queued functions, you should set up a distinct storage account for the queues used for the queue trigger.
- A PlayFab account.
PlayFab Azure Functions can use the Azure Functions V2 runtime or greater, and .NET Core 2 or greater. We recommend that you use the latest version (currently Azure Functions V4 and .NET 6).
Create an Azure Function
- Create a basic “HelloWorld” example function. You can see how to do this by following the Create your first function using Visual Studio Code guide. For a code example using PlayFab variables, see the section PlayFab Function Context, Variables, and using the Server SDKs.
The “Create your first function using Visual Studio Code” guide instructs you to set the Authorization Level of your Azure Function to
Anonymous. This is done to simplify testing. In a production environment, in most cases, you shouldn’t use Anonymous authorization since it enables anyone to call your function endpoint. To properly secure your function in the PlayFab environment, we recommend that you use Function level authorization.-
After you create and deploy your function, go to Automation > Cloud Script and select Register Function button in the top right hand corner of the page.

-
For Name, enter a human-friendly name for your function. For Function URL, enter the HTTP Trigger URL of the function. The URL can be found in the context menu of the Azure function resource as shown in “Run the function in Azure” section of Quickstart: Create a function in Azure using Visual Studio Code. If your Azure Function uses
Functionlevel authorization, the URL contains the Authorization key.
Using and Calling CloudScript using Azure Functions from your PlayFab Title
The example code in this guide is written in Unity C# & Azure Function C# code. Now that your function is registered, you can call that function from within using PlayFab APIs.Calling your function using HTTP requests from Visual Studio Code
Using the REST Client extension, you can invoke the function from within Visual Studio.Calling your function from Unity
You can use this code in Unity to invoke your function.PlayFab CloudScript Context, Variables, and Server SDKs
One advantage of using CloudScript using Azure Functions is that the PlayStream Event and Player Profile context is automatically passed to the Azure Function. On invocation of the CloudScript, you receive the context according to the function’s invocation scenario. For example, the context varies depending on whether it’s triggered by a PlayStream Action or called directly from the client. This includes information such as the entity profile on whose behalf the CloudScripts was invoked, and potentially the PlayStream events used to invoke the CloudScript.- You’ll need to install the PlayFab SDK via Package Manager. To do this open Terminal or CMD Console in Visual Studio Code and type:
dotnet add package PlayFabAllSDK - You need to include the CS2AFHelperClasses.cs file that contains the implementation of
PlayFab.Samples - Execution of a script can occur through several methods (APIs, Scheduled Tasks, PlayStream Event, Segment Entering and Exit method). The context of the execution is important to implement your CloudScript. See the Using CloudScript context models tutorial for details on how to use the context of the script.
Environment.GetEnvironmentVariable() method.
CurrentPlayerId of the caller is available as it is in our traditional CloudScript implementation. Parameters that you pass in the FunctionParameters field are available in the args. But, unlike the Hello World example in Create your first function using Visual Studio Code guide, parameters are passed in the POST body instead of the query string.
To call the HelloWorld Azure Function from a PlayFab SDK use ExecuteFunction.
Azure Functions in Automation Rules
Azure Functions can also be called by creating rules and scheduled tasks. This works in the same way as our standard CloudScript. To create a rule or scheduled task, go to Automation > Rules or Automation > Scheduled Tasks.- Select New Rule
- Enter a name for your rule
- Select the event type that this rule triggers on
- Add an action
- From the action dropdown, select Execute Azure Function

