Back to SDK Docs
JavaScript SDK
TypeScript-first SDK for web games and apps
v2.1.0TypeScriptReact HooksESM / CJS / CDN
Installation
Install via npm, yarn, pnpm, or use the CDN for browser builds.
Package Installation
# npm
npm install @xp-sdk/core
# yarn
yarn add @xp-sdk/core
# pnpm
pnpm add @xp-sdk/core
# CDN (browser)
<script src="https://cdn.funstack.io/sdk/v2/xp-sdk.min.js"></script>Initialization
Initialize the SDK with your API key. The SDK is fully typed with TypeScript.
SDK Initialization
import { XPSDK } from '@xp-sdk/core';
// Initialize the SDK
const xp = new XPSDK({
apiKey: 'your-api-key',
environment: 'testnet', // or 'production'
debug: true,
});
// Wait for SDK to be ready
await xp.init();
console.log('XP SDK initialized!');Authentication
Support both guest logins and Web3 wallet connections. Guest accounts can be upgraded to wallet accounts.
Authentication Examples
// Guest login (device fingerprint)
const player = await xp.auth.loginAsGuest();
console.log('Player ID:', player.id);
// Wallet login (EIP-1193)
const wallet = await xp.auth.connectWallet();
console.log('Wallet:', wallet.address);
// Check auth state
if (xp.auth.isLoggedIn) {
const balance = await xp.getXPBalance();
console.log('XP Balance:', balance);
}
// Logout
await xp.auth.logout();XP & Progression
Track events, award XP, and manage player progression. All operations are async and return typed results.
Progression Tracking
// Track game events
await xp.progression.trackEvent('level_complete', {
level: 5,
score: 12500,
time: 120.5,
});
// Award XP (server-validated)
const result = await xp.progression.awardXP(100, 'Boss defeated');
console.log('New balance:', result.newBalance);
// Get player stats
const stats = await xp.progression.getStats();
console.log(`Level: ${stats.level}, Total XP: ${stats.totalXP}`);
// Check achievements
const achievements = await xp.progression.getAchievements();
achievements.forEach(a => console.log(a.name, a.unlocked));Rewarded Ads
Show rewarded video ads with async/await or callback-style APIs.
Rewarded Ads Integration
// Check if ad is ready
const adReady = await xp.ads.isRewardedAdReady();
// Show rewarded ad
if (adReady) {
const result = await xp.ads.showRewardedAd({
placement: 'level_complete',
onStart: () => console.log('Ad started'),
onComplete: (reward) => console.log('Earned', reward.xp, 'XP'),
onError: (err) => console.error('Ad error:', err),
});
}
// Preload next ad
xp.ads.preloadRewardedAd();React Hooks
First-class React support with hooks for all SDK features. Install @xp-sdk/react separately.
React Integration
import { XPProvider, useXP, useAuth, useBalance } from '@xp-sdk/react';
// Wrap your app
function App() {
return (
<XPProvider apiKey="your-api-key">
<GameUI />
</XPProvider>
);
}
// Use hooks in components
function GameUI() {
const { trackEvent } = useXP();
const { player, login, logout } = useAuth();
const { balance, refresh } = useBalance();
return (
<div>
<p>XP: {balance}</p>
<button onClick={() => trackEvent('click')}>
Track Event
</button>
</div>
);
}