Initialize the Client
Before authenticating players, create a TogachaClient instance with your API key:
using Togacha;
var client = new TogachaClient(
"https://togacha.com",
"tgc_public_your_api_key"
);
Register a New Player
Create a new player account with email and password:
try
{
var response = await client.RegisterPlayerAsync(
"[email protected]",
"password123",
"password123" // password confirmation
);
// Store the token for authenticated requests
client.SetAuthToken(response.Token);
Debug.Log("Player registered successfully!");
}
catch (ApiException ex)
{
Debug.LogError($"Registration failed: {ex.Message}");
}
Parameters
| Parameter | Type | Description |
|---|
email | string | Player’s email address |
password | string | Password (minimum 8 characters) |
passwordConfirmation | string | Must match password |
Login an Existing Player
Authenticate a returning player:
try
{
var response = await client.LoginPlayerAsync(
"[email protected]",
"password123"
);
// Store the token for authenticated requests
client.SetAuthToken(response.Token);
Debug.Log("Player logged in successfully!");
}
catch (ApiException ex)
{
Debug.LogError($"Login failed: {ex.Message}");
}
Managing Auth Tokens
Set Token
After registration or login, set the token to authenticate subsequent requests:
client.SetAuthToken(response.Token);
You must call SetAuthToken() before making authenticated API calls like GetCurrenciesAsync() or CreditCurrencyAsync().
Clear Token (Logout)
To log out a player and clear the authentication token:
Persisting Sessions
The SDK doesn’t persist tokens automatically. To maintain sessions across app restarts, save the token to PlayerPrefs or a secure storage:
// Save token after login/register
PlayerPrefs.SetString("togacha_token", response.Token);
PlayerPrefs.Save();
// Restore token on app start
string savedToken = PlayerPrefs.GetString("togacha_token", null);
if (!string.IsNullOrEmpty(savedToken))
{
client.SetAuthToken(savedToken);
}
For production apps, consider using Unity’s secure storage or platform-specific keychain APIs instead of PlayerPrefs.
Next Steps
Currencies & Balances
Learn how to work with currencies and player balances