Getting Started
This guide will help you initialize the Moitribe SDK and understand the core concepts you'll use throughout your game.
Prerequisites
Before you begin, make sure you have:
- Installed the SDK in your project
- A valid game ID from the Moitribe dashboard
- Basic knowledge of JavaScript callbacks
Understanding the SDK API
The Moitribe SDK uses a consistent, callback-based API pattern:
MoitribeSDK(gameId, methodName, parameters, callback);
Parameters
| Parameter | Type | Description |
|---|---|---|
gameId | string | Your unique game identifier |
methodName | string | The SDK method to call (e.g., 'init', 'submitscore') |
parameters | object | Method-specific parameters |
callback | function | Optional callback function for async results |
Initialize the SDK
The first step in every game is to initialize the SDK. This establishes a connection to Moitribe and prepares the SDK for use.
JavaScript Example
import MoitribeSDK from '@veniso/moitribe-js';
// Initialize the SDK
MoitribeSDK('my-game-id', 'init', {
loginCallback: (result) => {
if (result.success) {
console.log('User is authenticated');
console.log('Player data:', result.playerdata);
// Player is signed in - start game
startGame(result.playerdata);
} else {
console.log('User is not authenticated');
// Show authentication options
showLoginScreen();
}
}
});
TypeScript Example
import MoitribeSDK, { GameInitParams, SignedInProfile } from '@veniso/moitribe-js';
// Define initialization parameters with types
const initParams: GameInitParams = {
loginCallback: (result: { success: boolean; playerdata?: SignedInProfile }) => {
if (result.success && result.playerdata) {
console.log('User is authenticated');
console.log('Player name:', result.playerdata.name);
console.log('Player ID:', result.playerdata.id);
// Player is signed in - start game
startGame(result.playerdata);
} else {
console.log('User is not authenticated');
// Show authentication options
showLoginScreen();
}
}
};
// Initialize the SDK
MoitribeSDK('my-game-id', 'init', initParams);
function startGame(player: SignedInProfile): void {
// Your game logic here
console.log(`Welcome, ${player.name}!`);
}
function showLoginScreen(): void {
// Show your authentication UI
}
Initialization Parameters
Required Parameters
None - you can initialize with an empty object: {}
Optional Parameters
| Parameter | Type | Description |
|---|---|---|
loginCallback | function | Called after initialization with authentication status |
playerid | string | Pre-existing player ID (for resuming sessions) |
noui | boolean | Must be true (UI removed in v1.4.2) |
The loginCallback is called automatically after initialization to indicate whether the player is authenticated. Use this to determine whether to show a login screen or start the game.
Understanding the Login Callback
The loginCallback receives a result object with the following structure:
{
success: boolean; // true if player is authenticated
playerdata?: SignedInProfile; // Player profile data (if authenticated)
}
Player Data Structure
When authenticated, playerdata contains:
{
id: string; // Player's unique ID
name: string; // Player's display name
profileicon: string; // Profile icon URL
profilehiresimg: string; // High-res profile image URL
bannerimgland: string; // Landscape banner image URL
bannerimgport: string; // Portrait banner image URL
title: string; // Player's title/rank
level: string; // Player's level
friends: string[]; // Array of friend IDs
}
Complete Example: Browser Game
Here's a complete example of initializing the SDK in a browser-based game:
<!DOCTYPE html>
<html>
<head>
<title>My Awesome Game</title>
</head>
<body>
<div id="game-container">
<div id="login-screen" style="display: none;">
<h1>Welcome to My Game</h1>
<button onclick="showEmailLogin()">Login with Email</button>
<button onclick="showGuestLogin()">Play as Guest</button>
</div>
<div id="game-screen" style="display: none;">
<h1>Welcome, <span id="player-name"></span>!</h1>
<div id="game-canvas"></div>
</div>
</div>
<script src="node_modules/@veniso/moitribe-js/dist/moitribe-sdk.min.js"></script>
<script>
// Initialize SDK
MoitribeSDK('my-game-id', 'init', {
loginCallback: function(result) {
if (result.success) {
// Player is authenticated
document.getElementById('player-name').textContent = result.playerdata.name;
document.getElementById('game-screen').style.display = 'block';
// Start game logic
startGame(result.playerdata);
} else {
// Player not authenticated - show login
document.getElementById('login-screen').style.display = 'block';
}
}
});
function startGame(playerData) {
console.log('Starting game for:', playerData.name);
// Your game initialization code here
}
function showEmailLogin() {
// Navigate to email login flow
// See: Authentication > Login with OTP
}
function showGuestLogin() {
// Navigate to guest login flow
// See: Authentication > Guest Login
}
</script>
</body>
</html>
Complete Example: Node.js Application
For server-side or Node.js games:
const MoitribeSDK = require('@veniso/moitribe-js');
// Initialize SDK
MoitribeSDK('my-game-id', 'init', {
loginCallback: (result) => {
if (result.success) {
console.log('Server initialized with player:', result.playerdata.id);
// Start server logic
startGameServer(result.playerdata);
} else {
console.log('No player authenticated - running in server mode');
// Start as anonymous server
startGameServer(null);
}
}
});
function startGameServer(playerData) {
// Your Node.js game server logic
console.log('Game server started');
if (playerData) {
console.log('Running as player:', playerData.name);
} else {
console.log('Running in server mode');
}
}
Next Steps by Feature
Now that you've initialized the SDK, explore the features you need:
Authentication
Start here if players need to log in:
- Authentication Overview - Understand authentication concepts
- Check Authentication - Check if user is logged in
- Login with OTP - Email/phone authentication
- Social Login - Google/Facebook login
- Guest Login - Anonymous play
Player Profiles
Manage player information:
- Profiles Overview - Profile management concepts
- Get Profile - Access player data
- Update Profile - Modify player information
Leaderboards
Add competitive rankings:
- Leaderboards Overview - Leaderboard concepts
- Submit Score - Post player scores
- Get Top Scores - Retrieve rankings
Real-Time Multiplayer
Build multiplayer games:
- RTM Overview - Real-time multiplayer introduction
- Standard Rooms - Fixed player matches
- Endless Rooms - Drop-in/drop-out gameplay
Tournaments
Create competitive events:
- Tournaments Overview - Tournament system introduction
- Join Tournament - Enter tournaments
- Submit Score - Post tournament scores
Common Patterns
Checking Authentication Status
After initialization, you can check authentication at any time:
MoitribeSDK('my-game-id', 'isAuthenticated', {}, (result) => {
if (result.success) {
console.log('User is authenticated');
} else {
console.log('User is not authenticated');
}
});
Error Handling
Always handle errors in callbacks:
MoitribeSDK('my-game-id', 'methodName', {}, (result) => {
if (result.success) {
// Success - handle result
console.log('Success:', result);
} else {
// Error - show message to user
console.error('Error:', result.message || 'Operation failed');
showErrorMessage(result.message);
}
});
Storing Game ID
Store your game ID in a constant to avoid typos:
const GAME_ID = 'my-game-id';
// Use throughout your code
MoitribeSDK(GAME_ID, 'init', {...});
MoitribeSDK(GAME_ID, 'submitscore', {...});
MoitribeSDK(GAME_ID, 'loadleaderboardmetadata', {...});
Troubleshooting
SDK Not Found
If you see "MoitribeSDK is not defined":
- Ensure the SDK is properly imported/loaded
- Check that the script tag is loaded before your code
- Verify the installation was successful
Login Callback Never Called
If loginCallback doesn't execute:
- Check browser console for errors
- Ensure game ID is correct
- Verify network connectivity
- Check that you're not blocking third-party cookies (if using web)
TypeScript Type Errors
If you have TypeScript errors:
- Ensure you've imported the types:
import type { GameInitParams } from '@veniso/moitribe-js' - Check your
tsconfig.jsonhas"esModuleInterop": true - Restart your TypeScript server
For more issues, see the Troubleshooting Guide
Summary
You've learned:
- ✅ How to initialize the SDK with
init - ✅ Understanding the login callback
- ✅ Handling authenticated and unauthenticated states
- ✅ Accessing player profile data
- ✅ Setting up for browser and Node.js environments
Continue to Authentication to learn how to authenticate players.