About lagged-js-sdk
A JavaScript SDK integration skill for games using Lagged, covering player data, ads, scores, and achievements.
Key features
- 01SDK loading and initialization
- 02Player profile lookup
- 03Interstitial and rewarded ads
- 04Leaderboard score submission
- 05Achievement unlocking
Use cases
- 01Adding Lagged player identity to a game
- 02Showing ads at gameplay transitions
- 03Submitting end-of-game scores
- 04Unlocking achievements after milestones
Install
Copy the command and run it in a terminal.
npx skills add https:/ / github.com/ hungify/ playables-skills --skill lagged-js-sdkAfter installing, type/lagged-js-sdkin Claude Code to confirm it loaded.
Details
From the project READMELagged JavaScript SDK
window.LaggedAPI is exposed after its loader script finishes. Initialization, player lookup, ads, leaderboards and achievements all hang off LaggedAPI.
Overview
Supported features: initialization handshake, player profile lookup, interstitial ads, rewarded ads, leaderboard score submission, and achievement unlocks.
Integration Checklist
- Load SDK — inject
lagged.jsand wait forwindow.LaggedAPI - Init with credentials — call
sdk.init(devId, publisherId)with ids from the Lagged dashboard - Check player auth — call
sdk.User.get;id > 0means signed-in,id = 0means guest - Wire ads at scene transitions — show interstitials between levels; gate rewarded ads on player action
- Submit scores on game over — call
sdk.Scores.savewith the board id and final score - Unlock achievements — call
sdk.Achievements.savewhen criteria are met
Installation
The SDK URL is https://lagged.com/api/rev-share/lagged.js. Dynamically inject the script tag, then poll for window.LaggedAPI before proceeding.
const SDK_URL = 'https://lagged.com/api/rev-share/lagged.js';
async function loadLaggedSDK(timeoutMs = 10000) {
await new Promise((resolve, reject) => {
const s = Object.assign(document.createElement('script'), {
src: SDK_URL,
async: true,
onload: resolve,
onerror: () => reject(new Error('Failed to load Lagged SDK'))
});
document.head.appendChild(s);
});
const started = Date.now();
while (typeof window.LaggedAPI === 'undefined') {
if (Date.now() - started > timeoutMs)
throw new Error('LaggedAPI did not appear within ' + timeoutMs + 'ms');
await new Promise((r) => setTimeout(r, 50));
}
return window.LaggedAPI;
}
Initialization
LaggedAPI.init requires both a developer id and a publisher id from the Lagged dashboard. Right after init, query the current player to read their id, name and avatar.
Wrap initLagged in a try/catch at the call site — on failure, disable leaderboard, achievement and ad calls so the game remains playable without SDK support.
async function initLagged(devId, publisherId) {
if (!devId || !publisherId) throw new Error('devId and publisherId are required');
const sdk = await loadLaggedSDK();
sdk.init(devId, publisherId);
const player = await new Promise((resolve) => {
sdk.User.get((response) => {
resolve(response && response.user ? response.user : {});
});
});
// id > 0 = signed-in player; id = 0 = guest
const profile = {
isAuthorized: player.id > 0,
id: player.id || null,
name: player.name || null,
avatar: player.avatar || null
};
console.debug('[Lagged] init complete', profile);
return { sdk, profile };
}
Player
Player data is read once during initialization via LaggedAPI.User.get(callback). The callback receives { user: { id, name, avatar } }. The full retrieval logic is demonstrated in the initLagged function above. If you need a guest fallback, generate a local id and persist it in localStorage.
Advertisement
Interstitial
LaggedAPI.APIAds.show(onClosed) displays an interstitial. Pause simulation and mute audio before the call; resume in onClosed.
function showInterstitial(sdk, { onOpened, onClosed } = {}) {
onOpened?.();
sdk.APIAds.show(() => {
onClosed?.();
});
}
Rewarded
LaggedAPI.GEvents.reward(canShowReward, rewardSuccess) runs a two-phase rewarded ad:
canShowReward(success, showAdFn)— ifsuccessistrue, callshowAdFn()to display the ad; otherwise surface a fallback.rewardSuccess(success)—truemeans the player earned the reward;falsemeans dismissed early or failed.
function showRewarded(sdk, { onOpened, onRewarded, onClosed, onFailed } = {}) {
onOpened?.();
sdk.GEvents.reward(
(success, showAdFn) => {
if (success) showAdFn();
else onFailed?.('unavailable');
},
(success) => {
if (success) {
onRewarded?.();
onClosed?.();
} else onFailed?.('not_completed');
}
);
}
Leaderboards
Each board is identified by a string id configured on the Lagged dashboard. There is no API to fetch entries or open a native popup.
function submitScore(sdk, boardId, score) {
return new Promise((resolve, reject) => {
sdk.Scores.save({ score, board: boardId }, (response) => {
if (response && response.success) resolve(response);
else reject((response && response.errormsg) || 'leaderboard_failed');
});
});
}
Achievements
LaggedAPI.Achievements.save(achievements, callback) unlocks one or many achievements in a single call. Display state is owned by Lagged — there is no list/get API.
function unlockAchievement(sdk, achievement) {
if (!achievement) return Promise.reject(new Error('achievement is required'));
return new Promise((resolve, reject) => {
const payload = Array.isArray(achievement) ? achievement : [achievement];
sdk.Achievements.save(payload, (response) => {
if (response && response.success) resolve(response);
else reject((response && response.errormsg) || 'achievement_failed');
});
});
}