Quick start
The InstFun platform gives H5 games hosted on instfun.games access to accounts, cloud saves, leaderboards, wallets, ads and analytics. The SDK offers two runtime modes and your game code does not need to tell them apart: with a backend, data is server-authoritative; without one, it falls back to browser storage.
Current SDK version: 1.2.0.
Include the SDK
Games hosted by the platform can load the single-file CDN bundle (same origin, no build step, global window.Instfun):
<script src="/sdk/v1/instfun.js"></script>Third-party source integrations built on the instfun platform can use the same global object; type definitions are provided separately.
Initialize
const sdk = await Instfun.init({ gameId: "my-game", gameVersion: "0.1.0" });gameId must match the gameId in the game-info.json inside your published package; in remote mode it also determines account data and leaderboard ownership.
Read the account
const user = await sdk.account.getCurrentUser();A guest account is created automatically; binding an email lets players recover it on another device (see the Integration guide).
Cloud saves and records
const progress = await sdk.game.loadProgress();
await sdk.game.start({ level: progress?.data.level ?? 1 });
await sdk.game.saveProgress({ level: 4, bestScore: 8320 }, { merge: true });
await sdk.game.finish({ result: "complete", score: 8320, submitScore: true });Saves are local-first in both modes; in remote mode an offline result carries pendingSync: true and is reconciled by sdk.sync().
Leaderboard
const board = await sdk.leaderboard.get({ boardId: "global" });Local mode returns the on-device best (localOnly: true); remote mode returns the global board (scope: "global") and falls back to the local board when offline.
Analytics and ads
sdk.analytics.track("level_complete", { level: 4 });
const ad = await sdk.ads.showRewarded({ placement: "DOUBLE_REWARD" });track never throws; a rewarded ad grants its reward through the SDK idempotently after a full view — do not add currency yourself.
Reconcile offline data
await sdk.sync();sync() uploads the analytics queue and retries offline saves, records and leaderboard submissions. Call it after startup, when the page returns to the foreground, and when the network recovers.
Next steps
- Runtime modes, accounts, email binding, save sync and offline behavior: see the Integration guide
- Every method and type: see the API reference
- Packaging and submission: see Publishing
InstFun 平台为托管在 instfun.games 的 H5 游戏提供账号、云存档、排行榜、钱包、广告与埋点能力。SDK 提供本地与远端两种运行模式,游戏代码无需区分:有后端时数据在服务端权威,无后端时自动使用浏览器本地存储。
当前 SDK 版本:1.2.0。
引入 SDK
平台托管的游戏可直接引用 CDN 单文件(与页面同源,无需构建,全局对象为 window.Instfun):
<script src="/sdk/v1/instfun.js"></script>第三方源码接入(在 instfun 平台构建的环境)也可以直接使用该全局对象,类型定义另行提供。
初始化
const sdk = await Instfun.init({ gameId: "my-game", gameVersion: "0.1.0" });gameId 必须与上架包内 game-info.json 的 gameId 一致;远端模式下它同时决定账号数据与排行榜归属。
读取账号
const user = await sdk.account.getCurrentUser();游客账号自动创建;绑定邮箱后可在换设备时找回(见「接入指南」)。
云存档与记录
const progress = await sdk.game.loadProgress();
await sdk.game.start({ level: progress?.data.level ?? 1 });
await sdk.game.saveProgress({ level: 4, bestScore: 8320 }, { merge: true });
await sdk.game.finish({ result: "complete", score: 8320, submitScore: true });两种模式下存档都是本地优先;远端模式离线时结果带 pendingSync: true,由 sdk.sync() 补偿。
排行榜
const board = await sdk.leaderboard.get({ boardId: "global" });本地模式返回本机最佳(localOnly: true),远端模式返回全球总榜(scope: "global"),断网时回退本机榜。
埋点与广告
sdk.analytics.track("level_complete", { level: 4 });
const ad = await sdk.ads.showRewarded({ placement: "DOUBLE_REWARD" });track 永不抛错;激励广告完整观看后由 SDK 幂等发奖,游戏不要自行加币。
补偿离线数据
await sdk.sync();sync() 会上报埋点队列并重试离线的存档、记录与榜单提交,建议在启动完成、页面回到前台与网络恢复时各调用一次。
下一步
- 运行模式、账号与邮箱绑定、存档同步与离线行为:见「接入指南」
- 全部方法与类型:见「API 速查」
- 打包与提审:见「上架流程」