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:
| Property | Type | Description |
|---|
Id | Guid | Unique currency identifier |
Name | string | Currency name (e.g., “Gold”, “Gems”) |
Description | string? | Optional description |
Icon | string? | Icon emoji or URL |
Balance | PlayerBalanceInfo? | Player’s balance (null if no balance exists) |
Balance Object
| Property | Type | Description |
|---|
Amount | string | Balance 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
| Parameter | Type | Description |
|---|
currencyId | Guid | The currency to credit |
amount | decimal or string | Amount 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:
| Error | Cause | Solution |
|---|
| 401 Unauthorized | Missing or invalid token | Call SetAuthToken() with a valid JWT |
| 404 Not Found | Currency doesn’t exist | Verify currency ID from GetCurrenciesAsync() |
| 422 Unprocessable | Validation error | Ensure amount is positive |