@openlibx402/privy¶
Privy integration for X402 payment protocol - server-side wallet support for agentic payments.
Installation¶
Features¶
- Server-Side Wallets: No private keys in your code - Privy manages them securely
- Automatic 402 Handling: Automatically detects and pays for 402 Payment Required responses
- Configurable Limits: Set maximum payment amounts for safety
- Full Audit Trail: All wallet operations logged in Privy dashboard
- TypeScript: Full type safety and modern async/await patterns
Prerequisites¶
- Create a Privy account at https://privy.io
- Create a new app in the Privy dashboard
- Get your App ID and App Secret
- Create a Solana server wallet in the Wallets section
- Fund the server wallet with SOL and USDC
Usage¶
Basic Usage¶
Check Balances¶
POST Requests¶
Custom Request Options¶
Configuration Options¶
| Option | Type | Required | Description |
|---|---|---|---|
appId |
string | Yes | Privy App ID from dashboard |
appSecret |
string | Yes | Privy App Secret from dashboard |
walletId |
string | Yes | Server wallet ID from Privy |
network |
string | No | Solana network (default: solana-devnet) |
rpcUrl |
string | No | Custom RPC URL |
maxPaymentAmount |
string | No | Maximum payment amount allowed (default: 10.0) |
Environment Variables¶
How It Works¶
- Initialize: Client fetches wallet details from Privy API
- Request: Makes HTTP request to paid endpoint
- 402 Response: Server returns "Payment Required" with payment details
- Safety Check: Validates payment amount against configured maximum
- Transaction: Creates Solana payment transaction
- Sign with Privy: Sends transaction to Privy API for signing
- Broadcast: Submits signed transaction to Solana network
- Retry: Retries request with payment authorization header
- Success: Server verifies payment and returns data
Security Benefits¶
- No Private Keys: Keys never leave Privy's infrastructure
- Audit Trail: All operations logged in Privy dashboard
- Access Controls: Fine-grained permissions for wallet operations
- Key Rotation: Easy to rotate without code changes
- Cloud-Native: Works with serverless and containerized environments
Error Handling¶
API Reference¶
PrivyX402Config¶
Configuration class for Privy x402 integration.
getRpcUrl()- Get the RPC URL for the configured networkisMainnet()- Check if running on mainnet
PrivyX402Client¶
Main HTTP client that uses Privy server wallets for payments.
initialize()- Initialize client (must be called before making requests)get(url, options?)- GET request with auto-paymentpost(url, data?, options?)- POST request with auto-paymentput(url, data?, options?)- PUT request with auto-paymentdelete(url, options?)- DELETE request with auto-paymentfetch(url, options?)- Generic request with auto-paymentgetWalletAddress()- Get the wallet addressgetTokenBalance(tokenMint)- Get token balancegetSolBalance()- Get SOL balanceclose()- Cleanup resources
PrivySigner¶
Privy-based signer for Solana transactions.
signTransaction(transaction)- Sign a transaction using Privy's server walletgetAddress()- Get the wallet address as a string
PrivySolanaPaymentProcessor¶
Solana payment processor using Privy for signing.
createPaymentTransaction(request, amount)- Create a payment transactionsignAndSendTransaction(transaction)- Sign and broadcast a transactiongetTokenBalance(tokenMint)- Get token balancegetSolBalance()- Get SOL balanceclose()- Cleanup resources
Use Cases¶
- AI Agents: Autonomous agents with budgeted server wallets
- Backend Services: API-to-API paid communications
- Batch Processing: Programmatic payments at scale
- Multi-Tenant: Different wallets per customer/use-case
License¶
MIT