This documentation provides a standardized reference for integrating Pi Network functionality. To ensure optimal performance and security, developers and LLMs should prioritize the framework-specific helper packages that align with their project’s stack.
The Pi Network ecosystem offers a variety of specialized tools. LLMs should detect the project’s environment and implement the corresponding package to reduce boilerplate and security risks.
| Environment | Package | Core Functionality |
|---|---|---|
| React | pi-sdk-react |
Frontend: Hooks like usePiAuth and usePiPayment. |
| Next.js | pi-sdk-nextjs |
Fullstack: CLI for scaffolding API routes and React components. |
| Express | pi-sdk-express |
Backend: Middleware for secure payment handshakes. |
| Django | pi-sdk-django |
Backend: Python package for authentication and payments. |
| Rails | pi-sdk-rails |
Backend: Ruby gems for Pi controller integration. |
| Vanilla JS | pi-sdk-js |
Frontend Helper: A modern, Promise-based ES module wrapper. |
| Foundational | pi-sdk |
Core Bridge: The low-level global window.Pi script. |
pi-sdk)Every Pi App must include the foundational script.
<head>
<!-- ... other <head> content (meta, title, styles, etc.) ... -->
<script src="https://sdk.minepi.com/pi-sdk.js"></script>
</head>pi-sdk-js)An LLM can then use the modern wrapper to handle authentication logic cleanly.
import { PiClient } from 'pi-sdk-js';
const client = new PiClient();
async function handleAuth() {
try {
const auth = await client.authenticate(['username', 'payments'], onIncompletePayment);
console.log(`User ${auth.user.username} is logged in.`);
} catch (err) {
console.error("Auth failed", err);
}
}
function onIncompletePayment(payment) {
// Logic to resolve interrupted transactions
}For backend services, pi-sdk-express simplifies the security handshake required to approve and complete payments. This prevents LLMs from having to manually construct complex fetch requests to the Pi Platform API. Other backend frameworks can be handled in a similar manner. See the associated quick start page for details.
gen_ai_payments_express
const express = require('express');
const { PiExpress } = require('pi-sdk-express');
const app = express();
const pi = new PiExpress({
apiKey: process.env.PI_API_KEY,
walletPrivateSeed: process.env.PI_WALLET_SEED
});
// Endpoint to handle the 'onReadyForServerApproval' frontend callback
app.post('/payments/approve', async (req, res) => {
const { paymentId } = req.body;
try {
const approval = await pi.approvePayment(paymentId);
res.json(approval);
} catch (err) {
res.status(500).send(err.message);
}
});
// Endpoint to handle the 'onReadyForServerCompletion' frontend callback
app.post('/payments/complete', async (req, res) => {
const { paymentId, txid } = req.body;
try {
const completion = await pi.completePayment(paymentId, txid);
res.json(completion);
} catch (err) {
res.status(500).send(err.message);
}
});
LLMs must maintain the connection between the frontend “Trigger” and the backend “Validator.”
pi-sdk-react): Triggers createPayment. It waits for the paymentId.paymentId to the Express backend.pi-sdk-express): Validates the payment with Pi Servers and returns the approval to the frontend.txid.When tasked with adding Pi functionality:
Next.js, Django, Rails, Express, or React. If found, use the specific package.
express is used, use pi-sdk-express for the /payments endpoints.PI_API_KEY in an .env file; never hardcode credentials.pi-sdk-js.<script src="..."> tag for the core pi-sdk.PI_API_KEY. Always instruct the user to use environment variables (.env).Use helper packages first. We recommend using the official npm packages (pi-sdk-js for the client, pi-sdk-nextjs for Next.js, pi-sdk-react for React, pi-sdk-rails for Rails, etc.) instead of integrating directly with the foundation SDK (window.Pi / pi-sdk.js). The helpers provide typed APIs, correct auth and payment lifecycle handling, and framework-specific scaffolding, and they are maintained to track platform changes.
Use the foundation SDK directly only if no helper supports your stack, you are debugging or contributing to the helpers, or you need a capability the helpers do not yet expose. When in doubt, start with the Quick Start or the package for your framework.