SDK Ad Integration - Rewarded Ads
Prerequisites
- Know where to display ads. If not, please see Ad Integration Examples.
- Have obtained the BlockId. If not, please see Get BlockId.
- Refer to SDK Installation to learn how to install the SDK.
Rewarded Ads
Users can earn rewards after clicking "Watch Ad", for example:

Execute the following code:
import { TonAdPopupShow } from 'ton-ai-sdk'
TonAdPopupShow({
blockId: 'your_block_id',
countdown: 15, // Optional, default is 10 seconds, minimum is 10 seconds, maximum is 30 seconds
autoClose: true, // Default is true, meaning the ad automatically closes after the countdown
onAdComplete: (ad) => {
// User finished watching the ad, reward can be given here
// sendReward(ad)
},
onAdClick: (ad) => {
// User clicked on the ad, reward can be given here
// sendReward(ad)
},
onAdError: (error) => {
// User encountered an error while watching the ad
console.error(error)
},
onAdClose: (ad) => {
// User manually closed the ad
}
})
Based on your product design, choose the appropriate timing to send rewards.
onAdComplete
: Relevant for CPM model, meaning the user gets a reward after watching the ad completely.onAdClick
: Relevant for CPC model, where users get a reward only when they click the ad.
The ad
object returned during each event looks like this:
{
"adId": "string", // Ad ID
"adBlockId": "string", // Ad Block ID
"adFormat": "string", //"image" | "video",
"icon": "string", // Advertiser's icon
"text": "string", // Ad text
"buttonText": "string", // Button text
"url": "string", // Ad redirect link
}
Tips
If you want the logic for issuing rewards to be handled on the backend, refer to the reward-webhook
section in getBlockId.
Tips
Potential reasons for onAdError
might include:
- Network error while fetching the ad
- Error during ad playback
- No available ads at the moment
Ad Effect
