SDK
This SDK provides secure bidirectional communication between the game and the native clients (iOS, Android), helping third‑party developers invoke native capabilities.
Global API name exposed: window.GameTokSDK
Capabilities provided:
| Action | Description |
|---|---|
| GET_PROFILE | Get the current user profile |
| PURCHASE | Initiate a purchase (e.g., in‑app purchase) |
| STORAGE_SET | Write to local key‑value storage (save settings, progress, etc.) |
| STORAGE_GET | Read from local key‑value storage (retrieve previously saved data) |
| ADD_SCORE | Submit score (for leaderboard/sharing, etc.) |
Integration Examples
The following examples show how to include the SDK in a page and call its capabilities.
1.1 Include Code
Method 1: include the bundled script directly via <script>
<script src="https://play.letskix.com/res/game/sdk-js/GameTokSDK.js"></script>
<script>
// Available globally
console.log('GameTokSDK version loaded:', !!window.GameTokSDK);
// ... existing code ...
</script>1.2 Usage Examples
All examples use Promises with then/catch. On success a unified response object is returned; on failure a Promise.reject error is thrown.
1.2.1 Get Profile
// Usage example
GameTokSDK.getProfile()
.then(({ action, error, data }) => {
console.log('Profile retrieved successfully:', data);
})
.catch((e) => {
console.error('Failed to get profile:', e);
});Response example (JSON):
{
"action": "GET_PROFILE",
"error": false,
"data": {
"uid": 1234567,
"avatar": "https://profile-file-pre.lobah.net/files/2024/11/15/3/fd1c01ac6640466f92a59c0aaa6b0112_lp_profile_img_frame_male_17.png",
"userName": "Tim tim",
"userCoins": 0,
"level": 0,
"gender": 0,
"testAccount": true,
"guest": true
}
}1.2.2 In‑App Purchase (e.g., buy an item)
/**
* In‑app purchase
* @param productId The product ID, which must be pre‑defined in the developer console (https://developer.lobah.net/)
*/
GameTokSDK.purchase({ productId: 'HAB.WATER.10.COINS' })
.then(({ data }) => {
//noted: the success or failure of the purchase needs to be determined based on the returned code value here
console.log('Purchase completed:', data);
})
.catch((e) => {
console.error('error trap:', e);
});Response example (JSON):
{
"action": "PURCHASE",
"error": false,
"data": {
"purchaseResultCode": 0,
"testAccount": false,
"userBalance": 22514
}
}
Result codes for `purchaseResultCode`:
0: Purchase succeeded
11: Incorrect product_id
12: Insufficient user coins
13: Duplicate or invalid reference_id
14: User is a guest; guests cannot purchase
16: Incorrect product_id
20: Other error1.2.3 Store Key‑Value
/**
* Store a key‑value pair
* @param key Custom key to store
* @param value Value to store; can be a string or an object
*/
GameTokSDK.storageSet({ key: 'settings', value: { theme: 'dark', volume: 0.8 } })
.then(() => {
console.log('Stored successfully');
})
.catch((e) => {
console.error('Store failed:', e);
});Response example (JSON):
{
"action": "STORAGE_SET",
"error": false,
"data": null
}1.2.4 Read Key‑Value
/**
* Read a key‑value pair
* @param key The predefined key to read
*/
GameTokSDK.storageGet({ key: 'settings' })
.then((result) => {
console.log('Read successfully:', result.data.value); // May be an object or a string
})
.catch((e) => {
console.error('Read failed:', e);
});Response example (JSON):
{
"action": "STORAGE_GET",
"error": false,
"data": {
"value": {
"theme": "dark",
"volume": 0.8
}
}
}1.2.5 Submit Score
/**
* Different games may need to submit a score, a level, or a stage.
* If submitting a score, pass { score: 300, scoreType: 'score', remark: 'score' }.
* If submitting a level, pass { score: 1, scoreType: 'level', remark: 'level' }.
* @param score The value to submit; must be a positive integer
* @param scoreType The data type (business‑defined description of the uploaded data)
* @param remark Custom description (can be the same as scoreType)
*/
GameTokSDK.addScore({ score: 10, scoreType: 'score', remark: 'score' })
.then(() => {
console.log('Score submitted successfully');
})
.catch((e) => {
console.error('Score submission failed:', e);
});Response example (JSON):
{
"action": "ADD_SCORE",
"error": false,
"data": null
}Parameters & Return Structure
Common call parameter examples (reference; actual params depend on capability):
- getProfile: `` (usually no extra params)
- purchase:
{ productId: string } - storageSet:
{ key: string, value: any } - storageGet:
{ key: string } - addScore:
{ score: number, scoreType: string }
Unified success response structure:
{
action: string; // Action name for this call (e.g., 'GET_PROFILE')
error: false; // false on success (on failure the promise rejects directly)
data: any; // Native response payload; fields depend on the specific action
}- Failure behavior:
- The Promise rejects directly with an
Errorobject. - Recommended to log the error in
.catchand retry or prompt the user.
- The Promise rejects directly with an
Important Notes
To ensure the SDK works properly, do not overwrite or delete the following key global objects:
window.GameTokSDK(main SDK interface)
Example (incorrect usage; do not copy):
// Dangerous! Will break iOS or Android communication
window.GameTokSDK = {};
window.GameTokSDK = null;
delete window.GameTokSDK;Complete Example
// Get user profile
GameTokSDK.getProfile({})
.then((resp) => {
console.log('Profile:', resp.data);
})
.catch((e) => {
console.error('Failed to get profile:', e);
});
// Store data
GameTokSDK.storageSet({ key: 'config', value: { lang: 'zh-CN' } })
.then(() => {
console.log('Stored successfully');
})
.catch((e) => {
console.error('Store failed:', e);
});
// Read data
GameTokSDK.storageGet({ key: 'config' })
.then((resp) => {
console.log('Config:', resp.data.value);
})
.catch((e) => {
console.error('Read failed:', e);
});
// Purchase example
GameTokSDK.purchase({ productId: 'HAB.WATER.10.COINS' })
.then((resp) => {
console.log('Purchase:', resp.data);
})
.catch((e) => {
console.error('Purchase failed:', e);
});
// Add score example
GameTokSDK.addScore({ score: 5, scoreType: 'score' })
.then(() => {
console.log('Score added');
})
.catch((e) => {
console.error('Score submission failed:', e);
});