Description
Referral codes let existing players (referrers) share a unique code with friends (referees). When a new player redeems the code, both parties receive a reward. The mechanic enforces two important constraints:- Each player account can only be referred once (signified by a
ReferralBadgeinventory item). - A referrer can earn rewards for referring up to 10 players (configurable via the
MAXIMUM_REFERRALSconstant in CloudScript).
ReferralBadge. The referrer receives 10 Gems.
How it works
The flow involves two clients: Client A (referrer) and Client B (referee).Client A authenticates
Client A logs in via one of the PlayFab authentication methods to obtain a valid session ticket.
Client A shares their referral code
The referral code is simply Client A’s PlayFab ID (
currentPlayerId). Client A reads this from the authentication response and shares it with Client B (for example, by copying it or sending it via a social channel).Client B authenticates
Client B downloads the game and logs in to obtain their own valid session ticket.
Client B enters and redeems the code
Client B enters Client A’s code and calls
ExecuteCloudScript with FunctionName: "RedeemReferral" and FunctionParameter: { referralCode: "<Client A's PlayFab ID>" }.RedeemReferral validates the request
The CloudScript handler performs the following checks in order:
- Confirms
args.referralCodeis present and non-empty. - Confirms the referral code is not the caller’s own ID (self-referral is blocked).
- Reads Client B’s inventory to verify that no
ReferralBadgeitem exists (each player can only be referred once). - Reads Client A’s
ReferralsPlayerData key to confirm the code is valid and that the referrer has not yet hit the 10-referral limit.
Reward the referrer (Client A)
If all checks pass, the script calls
ProcessReferrer:- Appends Client B’s PlayFab ID to Client A’s
Referralsarray and writes it back viaserver.UpdateUserReadOnlyData. - Grants 10 Gems to Client A via
server.AddUserVirtualCurrency.
Reward the referee (Client B)
The script calls
GrantReferralBonus, which uses server.GrantItemsToUser to grant both the premiumStarterPack bundle and the referralBadge item to Client B. The item annotation records who referred them.PlayFab building blocks
- Accounts — player authentication; the PlayFab ID doubles as the referral code
- Player Inventory —
ReferralBadgeacts as a one-time redemption lock - Player ReadOnly Data —
Referralsarray on the referrer tracks who has been referred - Virtual Currency — Gems (GM) awarded to the referrer
- Catalog —
premiumStarterPackbundle andreferralBadgeitem - CloudScript —
RedeemReferralhandler containing all validation and grant logic
Setup
Create the Gems virtual currency
In Game Manager, go to Economy > Currencies and select New Currency. Enter:
Select Save Currency.
| Property | Value | Detail |
|---|---|---|
| Code | GM | Abbreviation for the Gems currency |
| Name | Gems | Display name |
| Initial Deposit | 5 | Starting balance for every new player |
Upload the catalog
Go to Economy > Catalogs and select Upload JSON. Select
Catalog.json from the PlayFab-JSON folder of this recipe. The catalog must include premiumStarterPack and referralBadge items.The CloudScript
TheRedeemReferral handler performs all validation and grants server-side.
Helper functions
Helper functions
Running the example
- Unity 3D
- JavaScript
Install the PlayFab Unity SDK
Download the PlayFab Unity 3D SDK and import it into a new or existing Unity project.
Import the recipe package
Import
ReferralCodesRecipe.unitypackage from the Example-Unity3d folder (or download it from the PlayFab-Samples GitHub repo).Open the scene
In the Project window, open Assets > PlayFab Recipes > ReferralCodes > Scenes and add the ReferralCodes scene to your Hierarchy.
Set your title ID
Select the Main Camera under the ReferralCodes scene. In the Inspector, set Play Fab Title Id to your PlayFab title ID.
