Catalog npm
PACKAGE EXPLORERView in registry

@0205miss/pi_net_sdk

npm

Pi Network SDK for interacting with Pi Network APIs

blockchainpaymentspi-networksdk
Versions collected3
Direct dependencies5From release 2.0.2
Last observed20 Oct, 22:00 UTC

README

Current package documentation

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_sdk

Usage

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

  1. Clone the repository:

    git clone https://github.com/0205miss/pi_net_sdk.git
    cd pi_net_sdk
    
  2. Install dependencies:

    npm install
    
  3. Build 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

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Versions 3

Collected releases
VersionPublishedDirect dependenciesOpen version
2.0.2Registry latest6 Jun 20255
2.0.16 Jun 20255
2.0.06 Jun 20255

Package advisories

0
No matching advisories collected

This describes our collected data; it is not a security assessment.

Repository

PENDING
0205miss/pi_net_sdk

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 22:00 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package:7aa5f0c637402cf228d9a8b0a7db51273abaf8cdc4744e9346b8d627a01d73ef