PlayKit.ai

Authentication

How PlayKit SDK handles developer and player authentication in Unity

Authentication

PlayKit SDK uses two authentication modes: a developer token for development (charged to your account) and a player token for production (charged to the player's account). The SDK handles both automatically — you write no authentication code for the common case.

Developer Setup (Editor)

Before running your game in the editor, configure the SDK once via the Settings window:

  1. In Unity, open Tools > PlayKit SDK > Settings
  2. Click Login — a browser window opens
  3. Log in with your PlayKit account
  4. Your developer token is stored automatically in EditorPrefs (never committed to source control)
  5. Select your game from the Game Selection dropdown
  6. Choose default AI models in the AI Model Defaults section

From this point on, the SDK uses your developer credentials automatically whenever you run in the editor. No code changes are required.

Alternatively, if you have a developer token from the dashboard, paste it directly in the Direct Key Input section of the Settings window instead of logging in.

Developer tokens charge all API usage to your developer account. The SDK displays a red warning indicator when running with a developer token. Do not ship developer tokens to players.

SDK Initialization (Runtime)

The SDK auto-initializes before any scene loads — you do not need to call any initialization code for basic usage:

// The SDK is already initializing before this runs.
// Just start using it:
var chatClient = PlayKitSDK.Factory.CreateChatClient();

If you need to explicitly wait for initialization to complete (for example, before showing your main menu):

using Cysharp.Threading.Tasks;
using PlayKit_SDK;
using UnityEngine;

public class GameStartup : MonoBehaviour
{
    async void Start()
    {
        await PlayKitSDK.InitializeAsync();
        // SDK is now initialized and authenticated
        ShowMainMenu();
    }
}

Player Authentication Flow

At runtime, the SDK authenticates in this order:

  1. Developer token — if configured in Settings, use it (development mode)
  2. Cross-game shared token — if the player previously logged in to any PlayKit game on this device, reuse that token
  3. Local token — if the player previously logged in to this game, use the saved token
  4. Login UI — if no valid token is found, display the built-in login interface (LoginWeb.prefab) for the player to authenticate

Players only ever need to log in once per device across all PlayKit games.

Player Client

Get Player Information

using PlayKit_SDK;
using UnityEngine;

public class PlayerInfoDisplay : MonoBehaviour
{
    void Start()
    {
        var playerClient = PlayKitSDK.GetPlayerClient();
        var playerInfo = playerClient.GetCachedPlayerInfo();

        if (playerInfo != null)
        {
            Debug.Log($"Player ID: {playerInfo.UserId}");
            Debug.Log($"Remaining credits: {playerInfo.Credits}");
        }
    }
}

Refresh Player Information

using Cysharp.Threading.Tasks;
using PlayKit_SDK;

public async UniTask RefreshPlayerInfo()
{
    var playerClient = PlayKitSDK.GetPlayerClient();

    var result = await playerClient.GetPlayerInfoAsync(
        this.GetCancellationTokenOnDestroy()
    );

    if (result.Success && result.Response != null)
    {
        Debug.Log($"Player ID: {result.Response.UserId}");
        Debug.Log($"Current credits: {result.Response.Credits}");
    }
    else
    {
        Debug.LogError($"Failed to get player info: {result.ErrorMessage}");
    }
}

Listen for Player Info Updates

using PlayKit_SDK;
using UnityEngine;

public class PlayerInfoMonitor : MonoBehaviour
{
    private PlayKit_PlayerClient playerClient;

    void Start()
    {
        playerClient = PlayKitSDK.GetPlayerClient();
        playerClient.OnPlayerInfoUpdated += HandlePlayerInfoUpdated;
        playerClient.OnPlayerTokenReceived += HandleTokenReceived;
        playerClient.OnError += HandleError;
    }

    void OnDestroy()
    {
        if (playerClient != null)
        {
            playerClient.OnPlayerInfoUpdated -= HandlePlayerInfoUpdated;
            playerClient.OnPlayerTokenReceived -= HandleTokenReceived;
            playerClient.OnError -= HandleError;
        }
    }

    void HandlePlayerInfoUpdated(PlayerInfo info)
    {
        Debug.Log($"Credits updated: {info.Credits}");
    }

    void HandleTokenReceived(string token)
    {
        Debug.Log("Player token refreshed");
    }

    void HandleError(string error)
    {
        Debug.LogError($"Player client error: {error}");
    }
}

FAQ

Next Steps