Skip to main content

Overview

Togacha supports multiple currencies per app. Each player has a balance for each currency, which you can retrieve and modify through the SDK.
All currency operations require authentication. Make sure to call SetAuthToken() after login or registration.

Get Currencies

Retrieve all currencies in your app along with the authenticated player’s balances:
try
{
    var currencies = await client.GetCurrenciesAsync();

    foreach (var currency in currencies)
    {
        string balance = currency.Balance?.Amount ?? "0";
        Debug.Log($"{currency.Name}: {balance}");
    }
}
catch (ApiException ex)
{
    Debug.LogError($"Failed to get currencies: {ex.Message}");
}

Currency Object

Each currency contains the following properties:
PropertyTypeDescription
IdGuidUnique currency identifier
NamestringCurrency name (e.g., “Gold”, “Gems”)
Descriptionstring?Optional description
Iconstring?Icon emoji or URL
BalancePlayerBalanceInfo?Player’s balance (null if no balance exists)

Balance Object

PropertyTypeDescription
AmountstringBalance amount as decimal string (e.g., “100.00”)

Credit Currency

Add currency to a player’s balance:
try
{
    // Using decimal
    var balance = await client.CreditCurrencyAsync(currencyId, 100.00m);
    Debug.Log($"New balance: {balance.Amount}");

    // Or using string
    var balance = await client.CreditCurrencyAsync(currencyId, "50.00");
    Debug.Log($"New balance: {balance.Amount}");
}
catch (ApiException ex)
{
    Debug.LogError($"Failed to credit currency: {ex.Message}");
}

Parameters

ParameterTypeDescription
currencyIdGuidThe currency to credit
amountdecimal or stringAmount to add (must be positive)

Example: Currency Display UI

Here’s a complete example showing currencies in a UI:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Togacha;
using Org.OpenAPITools.Model;

public class CurrencyDisplay : MonoBehaviour
{
    [SerializeField] private Text currencyText;

    private TogachaClient _client;
    private List<CurrencyWithBalance> _currencies;

    async void Start()
    {
        _client = new TogachaClient(
            "https://togacha.com",
            "tgc_public_your_api_key"
        );

        // Login first
        var auth = await _client.LoginPlayerAsync("[email protected]", "password123");
        _client.SetAuthToken(auth.Token);

        // Then fetch currencies
        await RefreshCurrencies();
    }

    public async Task RefreshCurrencies()
    {
        _currencies = await _client.GetCurrenciesAsync();
        UpdateUI();
    }

    private void UpdateUI()
    {
        var display = "";
        foreach (var currency in _currencies)
        {
            string amount = currency.Balance?.Amount ?? "0";
            display += $"{currency.Icon} {currency.Name}: {amount}\n";
        }
        currencyText.text = display;
    }

    public async void AddGold(decimal amount)
    {
        var goldCurrency = _currencies.Find(c => c.Name == "Gold");
        if (goldCurrency != null)
        {
            await _client.CreditCurrencyAsync(goldCurrency.Id, amount);
            await RefreshCurrencies();
        }
    }
}

Error Handling

Common errors when working with currencies:
ErrorCauseSolution
401 UnauthorizedMissing or invalid tokenCall SetAuthToken() with a valid JWT
404 Not FoundCurrency doesn’t existVerify currency ID from GetCurrenciesAsync()
422 UnprocessableValidation errorEnsure amount is positive