Skip to content

Commit e6d5f26

Browse files
committed
Aug 2024 docs updates - add Pi Developer Ad Network docs
1 parent 10e3020 commit e6d5f26

File tree

3 files changed

+393
-77
lines changed

3 files changed

+393
-77
lines changed

SDK_reference.md

Lines changed: 141 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,24 @@ Add the following `script` tags to all pages where you need to call the Pi Apps
66

77
```html
88
<script src="https://sdk.minepi.com/pi-sdk.js"></script>
9-
<script>Pi.init({ version: "2.0" })</script>
9+
<script>
10+
Pi.init({ version: "2.0" });
11+
</script>
1012
```
1113

1214
The config object passed to the init function accepts the following keys:
13-
* `version` (string, required) - this is required to ensure compatibility of your app with newer SDK versions that might bring
15+
16+
- `version` (string, required) - this is required to ensure compatibility of your app with newer SDK versions that might bring
1417
breaking changes (in which case breaking changes will be implemented under a higher version number)
15-
* `sandbox`: (boolean, optional) - this enables you to configure the SDK to run in the sandbox.
18+
- `sandbox`: (boolean, optional) - this enables you to configure the SDK to run in the sandbox.
1619

1720
### Using the SDK in sandbox mode:
1821

1922
```html
2023
<script src="https://sdk.minepi.com/pi-sdk.js"></script>
21-
<script>Pi.init({ version: "2.0", sandbox: true })</script>
24+
<script>
25+
Pi.init({ version: "2.0", sandbox: true });
26+
</script>
2227
```
2328

2429
You may now run your app in the sandbox environment (https://sandbox.minepi.com), provided you've configured
@@ -31,15 +36,13 @@ to configure this and view your Sandbox URL.
3136
> `Pi.init({ version: "2.0", sandbox: <%= process.env.NODE_ENV !== 'production' %> })`. This depends on your
3237
> setup, but running the Pi SDK in sandbox mode will generally happen whenever your app is running in development.
3338
34-
3539
## Authentication
3640

3741
> **Warning:** The user information obtained with this method should not be passed to your backend and should
3842
> only be used for presentation logic (e.g displaying the user’s username).
3943
> **On your backend, use the Platform API as the source of truth.** You can verify the user's
4044
> identity by requesting the /me endpoint from your backend, using the access token obtained with this method.
4145
42-
4346
```typescript
4447
Pi.authenticate(scopes: Array<Scope>, onIncompletePaymentFound: Function<PaymentDTO>): Promise<AuthResult>
4548
```
@@ -48,12 +51,12 @@ Return value:
4851

4952
```typescript
5053
type AuthResult = {
51-
accessToken: string,
54+
accessToken: string;
5255
user: {
53-
uid: string,
54-
username: string
55-
}
56-
}
56+
uid: string;
57+
username: string;
58+
};
59+
};
5760
```
5861

5962
### `scopes`
@@ -63,12 +66,12 @@ Available scopes: `username`, `payments`, `wallet_address`
6366
Here is a breakdown of various keys available on the `AuthResult['user']` object, and the scopes required for those keys
6467
to be present:
6568

66-
| Field | Description | Required Scope |
67-
| -------------: | ------------- | :-------------: |
68-
| `uid` | An app-local identifier for the user. This is specific to this user, and this app. It will change if the user revokes the permissions they granted to your app. | (none) |
69-
| `username` | The user's Pi username. | `username` |
70-
| `payments` | Required if your app needs to make payments between your app and the users | `payments` |
71-
| `wallet_address` | The wallet address of the user who authenticates on your app. | `wallet_address` |
69+
| Field | Description | Required Scope |
70+
| ---------------: | --------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------: |
71+
| `uid` | An app-local identifier for the user. This is specific to this user, and this app. It will change if the user revokes the permissions they granted to your app. | (none) |
72+
| `username` | The user's Pi username. | `username` |
73+
| `payments` | Required if your app needs to make payments between your app and the users | `payments` |
74+
| `wallet_address` | The wallet address of the user who authenticates on your app. | `wallet_address` |
7275

7376
### `onIncompletePaymentFound`
7477

@@ -85,7 +88,6 @@ When this callback is invoked, it is your responsibility to complete the corresp
8588
likely send the payment DTO to your server, and process it according to your business logic). You'll need to do
8689
so before you can request a new payment from the user.
8790

88-
8991
## Payments
9092

9193
Create a new payment:
@@ -115,13 +117,12 @@ the payment and submit the blockchain transaction, or reject it.
115117
> **Warning: concurrent payments:**
116118
>
117119
> When creating a new payment, if there is already an open payment with your app for the current user:
118-
> * If the user has not yet made the blockchain transaction, the open payment will be cancelled.
119-
> * If the user has already made the blockchain transaction, the new payment will be rejected
120-
> (`onError` will be called) and the `onIncompletePaymentFound` callback that was passed to the `authenticate`
121-
> method will be called with the existing payment (use this callback to resolve the situation, e.g by sending
122-
> the previous payment to your server for server-side completion).
123-
124-
120+
>
121+
> - If the user has not yet made the blockchain transaction, the open payment will be cancelled.
122+
> - If the user has already made the blockchain transaction, the new payment will be rejected
123+
> (`onError` will be called) and the `onIncompletePaymentFound` callback that was passed to the `authenticate`
124+
> method will be called with the existing payment (use this callback to resolve the situation, e.g by sending
125+
> the previous payment to your server for server-side completion).
125126
126127
### `paymentData` keys:
127128

@@ -189,65 +190,75 @@ This is called when an error occurs and the payment cannot be made. If the payme
189190
created, the second argument will be present and you may use it to investigate the error.
190191
Otherwise, only the first argument will be provided.
191192

192-
193193
### Type `PaymentDTO`
194194

195195
This type is for the arguments that are passed to `onIncompletePaymentFound` and `onError`.
196196

197197
```typescript
198198
type PaymentDTO = {
199199
// Payment data:
200-
identifier: string, // payment identifier
201-
user_uid: string, // user's app-specific ID
202-
amount: number, // payment amount
203-
memo: string, // a string provided by the developer, shown to the user
204-
metadata: Object, // an object provided by the developer for their own usage
205-
from_address: string, // sender address of the blockchain transaction
206-
to_address: string, // recipient address of the blockchain transaction
207-
direction: Direction, // direction of the payment
208-
created_at: string, // payment's creation timestamp
209-
network: AppNetwork, // a network of the payment
210-
200+
identifier: string; // payment identifier
201+
user_uid: string; // user's app-specific ID
202+
amount: number; // payment amount
203+
memo: string; // a string provided by the developer, shown to the user
204+
metadata: Object; // an object provided by the developer for their own usage
205+
from_address: string; // sender address of the blockchain transaction
206+
to_address: string; // recipient address of the blockchain transaction
207+
direction: Direction; // direction of the payment
208+
created_at: string; // payment's creation timestamp
209+
network: AppNetwork; // a network of the payment
210+
211211
// Status flags representing the current state of this payment
212212
status: {
213-
developer_approved: boolean, // Server-Side Approval
214-
transaction_verified: boolean, // blockchain transaction verified
215-
developer_completed: boolean, // server-Side Completion
216-
cancelled: boolean, // cancelled by the developer or by Pi Network
217-
user_cancelled: boolean, // cancelled by the user
218-
},
219-
213+
developer_approved: boolean; // Server-Side Approval
214+
transaction_verified: boolean; // blockchain transaction verified
215+
developer_completed: boolean; // server-Side Completion
216+
cancelled: boolean; // cancelled by the developer or by Pi Network
217+
user_cancelled: boolean; // cancelled by the user
218+
};
219+
220220
// Blockchain transaction data:
221-
transaction: null | { // This is null if no transaction has been made yet
222-
txid: string, // id of the blockchain transaction
223-
verified: boolean, // true if the transaction matches the payment, false otherwise
224-
_link: string, // a link to the operation on the Blockchain API
225-
},
226-
}
221+
transaction: null | {
222+
// This is null if no transaction has been made yet
223+
txid: string; // id of the blockchain transaction
224+
verified: boolean; // true if the transaction matches the payment, false otherwise
225+
_link: string; // a link to the operation on the Blockchain API
226+
};
227+
};
227228
```
228229

229230
### Type `Direction`
230231

231232
A developer can check the direction of the payment with this type.
232233

233234
```typescript
234-
type Direction = "user_to_app" | "app_to_user"
235+
type Direction = "user_to_app" | "app_to_user";
235236
```
236237

237238
### Type `AppNetwork`
238239

239240
Shows which network the payment is being made on.
240241

241242
```typescript
242-
type AppNetwork = "Pi Network" | "Pi Testnet"
243+
type AppNetwork = "Pi Network" | "Pi Testnet";
243244
```
244245

245246
### Type `Scope`
246247

247248
Scopes you can request to users.
248249

249250
```typescript
250-
type Scope = "username" | "payments" | "wallet_address"
251+
type Scope = "username" | "payments" | "wallet_address";
252+
```
253+
254+
## Native features list
255+
256+
Use this method to get a list of native features available for specific version of Pi Browser your user is using. Some SDK features may require particular features to work properly.
257+
258+
```typescript
259+
type NativeFeature = "inline_media" | "request_permission" | "ad_network";
260+
261+
Pi.nativeFeaturesList(): Promise<Array<NativeFeature>>;
251262
```
252263

253264
## Share dialog
@@ -261,5 +272,80 @@ Pi.openShareDialog(title: string, message: string): void;
261272
Use this method to open a native Share dialog (provided by the phone's OS), enabling your users to share
262273
content from your app with their friends.
263274

264-
* `title`: the title of the message being shared
265-
* `message`: the message that will be sent when the user picks a target app in the Share flow
275+
- `title`: the title of the message being shared
276+
- `message`: the message that will be sent when the user picks a target app in the Share flow
277+
278+
## Ads
279+
280+
SDK contains the `Ads` module (object) which allows developers to display ads to the users. It provides several methods for both interstitial and rewarded ads. All of the methods take advantage of Promise-based asynchronicity.
281+
282+
```typescript
283+
type AdType = "interstitial" | "rewarded";
284+
```
285+
286+
### Show ad
287+
288+
Use `showAd(adType: "interstitial" | "rewarded")` method to display an ad to a user. It returns a promise which resolves with an object of type `ShowAdResponse`:
289+
290+
```typescript
291+
type ShowAdResponse =
292+
| {
293+
type: "interstitial";
294+
result: "AD_CLOSED" | "AD_DISPLAY_ERROR" | "AD_NETWORK_ERROR" | "AD_NOT_AVAILABLE";
295+
}
296+
| {
297+
type: "rewarded";
298+
result: "AD_REWARDED" | "AD_CLOSED" | "AD_DISPLAY_ERROR" | "AD_NETWORK_ERROR" | "AD_NOT_AVAILABLE" | "ADS_NOT_SUPPORTED" | "USER_UNAUTHENTICATED";
299+
adId?: string;
300+
};
301+
302+
Pi.Ads.showAd(adType: AdType): Promise<ShowAdResponse>
303+
304+
```
305+
306+
- `"AD_NOT_AVAILABLE"` indicates the ad failed to load,
307+
- `"AD_CLOSED"` indicates the ad was successfully displayed and closed,
308+
- `"AD_REWARDED"` indicates the ad was successfully displayed and rewarded (only applicable for `rewarded` ads),
309+
- `"AD_DISPLAY_ERROR"` indicates the ad was successfully loaded but failed to be displayed,
310+
- `"AD_NETWORK_ERROR"` indicates that a user might have encountered network connection issues,
311+
- `"ADS_NOT_SUPPORTED"` - indicates that app version used by uesr does not support ads,
312+
- `"USER_UNAUTHENTICATED"` - indicates that user is not authenticated therefore rewarded ad cannot be display (only for `rewarded` ads).
313+
314+
The Pi Browser internally manages the ads availability strategy, automatically loading initial ads and reloading them whenever displayed. This ensures that ads are consistently ready for display. However, in rare cases, loading ads may be interrupted or the third party app may use `showAd()` methods too quickly (even before the next ad is ready). To allow developers handle these cases, the Pi SDK provides additional methods: `isAdReady()` and `requestAd()`.
315+
316+
</br>
317+
318+
> **Notes:**
319+
>
320+
> - If your application was approved to Pi Developer Ad Network, the response from `Pi.Ads.showAd('rewarded')` will contain additional `adId` fields, which allows you to [verify the rewarded status using Pi Platform API](ads.md#rewarded-ads-status-verification-with-pi-platform-api).
321+
> - If your application wasn't approved to Pi Developer Ad Network, the `adId` field will be missing from `Pi.Ads.showAd('rewarded')` response. You should not grant rewards to users without verifying the `adId` using [Pi Platform API](platform_API.md#verify-a-rewarded-ad-status).
322+
323+
### Check if ad is ready
324+
325+
Use `isAdReady(adType: "interstitial" | "rewarded")` method to check if an ad of specific type is available. This method returns a promise which resolves with object of type `IsAdReadyResponse`.
326+
327+
```typescript
328+
type IsAdReadyResponse = {
329+
type: "interstitial" | "rewarded";
330+
ready: boolean;
331+
};
332+
333+
Pi.Ads.isAdReady(adType: AdType): Promise<IsAdReadyResponse>
334+
```
335+
336+
The Pi Browser internally manages the ads availability strategy, automatically loading initial ads and reloading them whenever displayed. This ensures that ads are consistently ready for display. However, in rare cases, loading ads may be interrupted or the third party app may use `showAd()` methods too quickly (even before the next ad is ready). To address such cases, the Pi SDK provides this method to allow developers to check the availability of ads explicitly.
337+
338+
### Request ad
339+
340+
Use `requestAd(adType: "interstitial" | "rewarded")` method to request an interstitial or a rewarded ad. It returns a promise which resolves with an object of type `RequestAdResponse`:
341+
342+
```typescript
343+
type RequestAdResponse = {
344+
type: "interstitial" | "rewarded";
345+
result: "AD_LOADED" | "AD_FAILED_TO_LOAD" | "AD_NOT_AVAILABLE";
346+
};
347+
348+
Pi.Ads.requestAd(adType: AdType): Promise<RequestAdResponse>
349+
```
350+
351+
As with the Ads availability strategy, the Pi Browser internally manages the process of requesting new ads to replace the displayed ones. While it's not guaranteed that an ad will be available at all time, developers can use `requestAd()` method to manually retry the ad request in case a promise returned by the `isAdReady()` resolved with `false`.

0 commit comments

Comments
 (0)