@0205miss/pi_net_sdk
v2.0.2
Published
Pi Network SDK for interacting with Pi Network APIs
Maintainers
Readme
Pi Network SDK
A TypeScript SDK for interacting with the Pi Network APIs, enabling payments, wallet transactions, and more.
Installation
npm i @0205miss/pi_net_sdkUsage
Below are examples demonstrating how to use the Pi Network SDK. All examples use the named export piSDK, which is a pre-instantiated instance of the PiSDK class. You can also import the PiSDK class to create your own instances if needed.
Initializing the SDK
Initialize the SDK with your desired configuration (e.g., version and sandbox mode).
import { piSDK } from '@0205miss/pi_net_sdk';
async function initSDK() {
try {
await piSDK.init({
version: '2.0',
sandbox: true // Use sandbox mode for testing
});
console.log('Pi SDK initialized successfully');
} catch (error) {
console.error('Initialization failed:', error);
}
}
initSDK();Authenticating a User
Authenticate a user with specific scopes (e.g., payments, username) and handle incomplete payments.
import { piSDK } from '@0205miss/pi_net_sdk';
async function authenticateUser() {
try {
const scopes = ['payments', 'username'];
const onIncompletePaymentFound = (payment: any) => {
console.log('Incomplete payment found:', payment);
// Handle incomplete payment (e.g., notify user or resume flow)
};
const authResult = await piSDK.authenticate(scopes, onIncompletePaymentFound);
console.log('Authenticated user:', authResult.user.uid);
console.log('Access token:', authResult.accessToken);
} catch (error) {
console.error('Authentication failed:', error);
}
}
authenticateUser();Creating a Payment
Create a payment flow, including callbacks for server approval, completion, errors, and cancellation.
import { piSDK } from '@0205miss/pi_net_sdk';
async function createPayment() {
try {
// Ensure SDK is initialized and user is authenticated with 'payments' scope
await piSDK.init({ version: '2.0', sandbox: true });
await piSDK.authenticate(['payments'], (payment) => console.log('Incomplete payment:', payment));
const paymentData = {
amount: 10, // Amount in Pi
memo: 'Test payment',
metadata: { orderId: '12345' },
uid: 'user123' // User ID from authentication
};
const callbacks = {
onReadyForServerApproval: (paymentId: string) => {
console.log('Ready for server approval:', paymentId);
// Send paymentId to your server for approval
},
onReadyForServerCompletion: (paymentId: string, txid: string) => {
console.log('Ready for server completion:', paymentId, txid);
// Send paymentId and txid to your server to complete the transaction
},
onError: (error: Error, payment?: any) => {
console.error('Payment error:', error, payment);
},
onCancel: (paymentId: string) => {
console.log('Payment cancelled:', paymentId);
}
};
const paymentFlow = piSDK.createPayment(paymentData, callbacks);
console.log('Payment flow started');
} catch (error) {
console.error('Payment creation failed:', error);
}
}
createPayment();Submitting a Wallet Transaction
Submit a transaction using a Stellar XDR string (e.g., for blockchain operations).
import { piSDK } from '@0205miss/pi_net_sdk';
async function submitTransaction() {
try {
// Ensure SDK is initialized
await piSDK.init({ version: '2.0', sandbox: true });
// Example XDR (replace with actual XDR from your application)
const xdr = 'AAAA...'; // Stellar transaction XDR
const result = await piSDK.Wallet!.submitTransaction(xdr);
console.log('Transaction submitted:', result);
} catch (error) {
console.error('Transaction submission failed:', error);
}
}
submitTransaction();Opening a Share Dialog
Prompt the user to share content via the Pi Network.
import { piSDK } from '@0205miss/pi_net_sdk';
async function openShareDialog() {
try {
await piSDK.init({ version: '2.0', sandbox: true });
await piSDK.openShareDialog('My App', 'Check out my awesome app on Pi Network!');
console.log('Share dialog opened');
} catch (error) {
console.error('Failed to open share dialog:', error);
}
}
openShareDialog();Using the PiSDK Class Directly
If you need multiple SDK instances, import and instantiate the PiSDK class.
import { PiSDK } from '@0205miss/pi_net_sdk';
async function customInstance() {
const customSDK = new PiSDK();
try {
await customSDK.init({ version: '2.0', sandbox: false });
console.log('Custom SDK instance initialized');
} catch (error) {
console.error('Initialization failed:', error);
}
}
customInstance();Features
- Authenticate users with Pi Network.
- Create and manage payments.
- Submit wallet transactions.
- Access native features like QR code scanning, ads, and share dialogs.
- Track user interactions and locations.
- Support for both sandbox and production environments.
Requirements
- Node.js >= 18.x
- A Pi Network developer account
- TypeScript (optional, for type safety)
Development Setup
Clone the repository:
git clone https://github.com/0205miss/pi_net_sdk.git cd pi_net_sdkInstall dependencies:
npm installBuild the project:
npm run build
Contributing
Contributions are welcome! Please submit issues and pull requests to https://github.com/0205miss/pi_net_sdk.
License
MIT
