The Pi Network JS SDK provides a module for integrating native ads
into your Pi app via the window.Pi.Ads namespace. This API enables
you to show Pi-managed ad units in your web frontend using simple,
promise-based methods. For a detailed explanation, please refer to the platform guide: Ads
This guide demonstrates how to integrate the Pi SDK into many application frameworks. This example shows how to initialize the Pi SDK, authenticate a Pioneer, and create a payment request inside a Pi app.
The pi-sdk-js
package
is part of the “Ten Minutes to Transactions” effort described in this
video.
If you, or your GenAI agent, are planning to use the Pi SDK modules in this documentation for your app, it is highly suggested that you use this package rather than implement transaction processing by hand with the core Pi SDK. The three way handshake between client, server, and the Pi servers required is provded for you.
Note: Pi SDK authentication and payment features require the application to run inside the Pi Browser.
interstitial: Full-screen ads, typically shown between user actions (e.g., between game levels).rewarded: Ads that, once watched, allow the app to reward the user (e.g., unlock content, give discounts).window.Pi.Ads.isAdReady(type)
window.Pi.Ads.requestAd(type)
window.Pi.Ads.showAd(type)type: 'interstitial' |
'rewarded' |
window.Pi.Ads Methods.isAdReady(type)Pi.Ads.isAdReady('interstitial' | 'rewarded') : Promise<result>true if ad is available, otherwise false..requestAd(type)Pi.Ads.requestAd('interstitial' | 'rewarded') : Promise<result>.showAd(type)isAdReady(type) returns true.Pi.Ads.showAd('interstitial' | 'rewarded') : Promise<result>// Example: Show a rewarded ad, then give user a bonus
Pi.Ads.isAdReady('rewarded').then(function(ready) {
if (ready) {
Pi.Ads.showAd('rewarded').then(function(result) {
// Ad finished! Give the user a reward
grantUserBonus();
}).catch(function(err) {
// User canceled, ad failed, or unavailable
alert('Could not show ad: ' + err.message);
});
} else {
// Ad not ready; maybe preload
Pi.Ads.requestAd('rewarded');
}
});.catch to handle errors (ad not ready, request denied, unsupported type).'interstitial' and 'rewarded'. Any other string will reject with an error.isAdReady(type) before showing an ad.requestAd(type) at app launch to preload ads for lowest latency.