Privy Server Wallet Agent¶
This example demonstrates using Privy's server-side wallet infrastructure with x402 for agentic payments.
Overview¶
The @openlibx402/privy package provides a secure way for AI agents and backend services to make x402 payments without managing raw private keys. Instead, Privy's server wallet API handles key management, signing, and security in the cloud.
Prerequisites¶
- Privy Account: Create an account at https://privy.io
- Server Wallet: Create a Solana server wallet in the Privy dashboard
- Express.js Server: The express-server example must be running
- Wallet with Funds: Your Privy server wallet needs SOL and USDC
Setup¶
-
Navigate to the example:
-
Install dependencies:
-
Configure environment:
-
Edit
.envwith your Privy credentials:
Running¶
Getting Privy Credentials¶
App ID and App Secret¶
- Go to https://dashboard.privy.io
- Select your app (or create a new one)
- Navigate to Settings → API Keys
- Copy your App ID and App Secret
Server Wallet ID¶
- In the Privy dashboard, go to Wallets → Server Wallets
- Click Create Wallet
- Select Solana as the chain type
- Copy the wallet ID (format:
wallet-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
Code Example¶
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 (no private keys locally)
- Broadcast: Submits signed transaction to Solana network
- Retry: Retries request with payment authorization header
- Success: Server verifies payment and returns data
Project Structure¶
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
Troubleshooting¶
"Wallet not found" Error¶
- Verify your
PRIVY_WALLET_IDis correct - Ensure the wallet exists in your Privy dashboard
- Check your
PRIVY_APP_IDandPRIVY_APP_SECRETare correct
"Insufficient Funds" Error¶
- Fund your server wallet with USDC tokens
- Get devnet USDC from a faucet
"Payment amount exceeds maximum" Error¶
- Increase
maxPaymentAmountin config