The official JavaScript SDK for LuduStack, enabling easy integration of gamification features into your JavaScript applications.
Install the SDK using npm or yarn:
npm install @ludustack/sdk
Initialize the SDK with your API key and game ID:
import { LuduStack } from '@ludustack/sdk';
// Initialize the SDK - gameId is required
const sdk = new LudustackSDK({
apiKey: 'your-api-key',
gameId: 'your-game-id' // Required - all operations will use this game
});
Give points to a player:
await sdk.operations.givePoints(
'player-123', // Player ID
100, // Points to add
'level_completed' // Optional: The reason for the points
);
Process achievement progress or completion:
// Process achievement progress
await sdk.operations.processAchievement(
'player-123', // Player ID
'first_login', // Achievement ID
{
progress: 1, // Progress amount (optional)
forceComplete: false, // Force completion (optional)
operation: 'progress' // Operation type
}
);
// Force complete an achievement
await sdk.operations.processAchievement(
'player-123',
'achievement-id',
{ forceComplete: true }
);
Retrieve a leaderboard and its entries:
// Get leaderboard by ID
const leaderboard = await sdk.leaderboards.getById('leaderboard-123');
Create, update, and manage game state:
// Create a new game
const newGame = await sdk.games.create({
name: 'My Game',
description: 'A great game'
});
// Update game status
await sdk.games.updateStatus(newGame.id, 'active');
The SDK uses standard Promise-based error handling:
try {
// Give points to a player
await sdk.operations.givePoints(
'player-123', // Player ID
100, // Points to add
'level_completed' // Optional: The reason for the points
);
console.log('Points added successfully');
} catch (error) {
console.error('Error adding points:', error.message);
// Handle specific error codes
if (error.code === 'invalid_player') {
// Handle invalid player error
}
}
The SDK supports these configuration options:
const sdk = new LudustackSDK({
apiKey: 'your-api-key', // Required
gameId: 'your-game-id', // Required - all operations will use this game
});
The SDK configuration includes the API version:
// Initialize the SDK
const gamification = new LuduStack({
apiKey: 'your-api-key',
gameId: 'your-game-id',
// Current API version: v2
});
For React applications, check out our React SDK documentation.
For full API reference, visit the API Reference section.