importorg.openlibx402.client.X402Client;importorg.openlibx402.core.errors.*;importorg.openlibx402.core.models.*;importorg.p2p.solanaj.core.Account;importokhttp3.Response;publicclassManualPaymentExample{publicstaticvoidmain(String[]args){// Load Solana account (securely in production!)byte[]secretKey=loadSecretKey();Accountaccount=newAccount(secretKey);// Create clienttry(X402Clientclient=newX402Client(account,"https://api.devnet.solana.com",true// allowLocal for development only)){Stringurl="https://api.example.com/premium-data";// Make initial requesttry{Responseresponse=client.get(url);System.out.println("Success: "+response.body().string());}catch(PaymentRequiredErrore){System.out.println("Payment required!");// Get payment request detailsPaymentRequestrequest=e.getPaymentRequest();System.out.println("Amount: "+request.getMaxAmountRequired());System.out.println("Asset: "+request.getAssetType());System.out.println("Description: "+request.getDescription());// Create paymentPaymentAuthorizationauth=client.createPayment(request);System.out.println("Payment created: "+auth.getSignature());// Retry request with payment authorizationResponseretryResponse=client.get(url,auth);System.out.println("Success: "+retryResponse.body().string());}}catch(InsufficientFundsErrore){System.err.println("Insufficient funds!");System.err.println("Required: "+e.getRequiredAmount());System.err.println("Available: "+e.getAvailableAmount());}catch(X402Errore){System.err.println("X402 Error: "+e.getCode());System.err.println("Message: "+e.getMessage());}catch(Exceptione){e.printStackTrace();}}privatestaticbyte[]loadSecretKey(){// In production, load from secure storageStringkeyEnv=System.getenv("SOLANA_SECRET_KEY");if(keyEnv!=null){returnjava.util.Base64.getDecoder().decode(keyEnv);}// For demo: generate random accountSystem.out.println("⚠️ Using random account for demo");returnnewAccount().getSecretKey();}}
importorg.openlibx402.client.X402AutoClient;importorg.openlibx402.core.errors.*;importorg.p2p.solanaj.core.Account;importokhttp3.Response;publicclassAutoPaymentExample{publicstaticvoidmain(String[]args){byte[]secretKey=loadSecretKey();Accountaccount=newAccount(secretKey);// Build auto-client with payment limitstry(X402AutoClientclient=newX402AutoClient.Builder(account).rpcUrl("https://api.devnet.solana.com").maxPaymentAmount("1.0")// Max 1 USDC per request.maxRetries(3)// Retry up to 3 times.allowLocal(true)// Development only.build()){// Client automatically handles 402 and retriesStringurl="https://api.example.com/premium-data";Responseresponse=client.get(url);System.out.println("Success: "+response.body().string());}catch(InsufficientFundsErrore){System.err.println("Insufficient funds for payment");}catch(PaymentRequiredErrore){System.err.println("Payment required but max retries exceeded");}catch(Exceptione){e.printStackTrace();}}privatestaticbyte[]loadSecretKey(){StringkeyEnv=System.getenv("SOLANA_SECRET_KEY");if(keyEnv!=null){returnjava.util.Base64.getDecoder().decode(keyEnv);}returnnewAccount().getSecretKey();}}
importorg.openlibx402.client.X402Client;importorg.p2p.solanaj.core.Account;importokhttp3.Response;publicclassPostRequestExample{publicstaticvoidmain(String[]args){Accountaccount=newAccount(loadSecretKey());try(X402Clientclient=newX402Client(account,null,true)){Stringurl="https://api.example.com/process";// JSON request bodyStringjsonBody="{"+"\"query\": \"Analyze this data\","+"\"options\": {\"format\": \"json\"}"+"}";try{// Make POST requestResponseresponse=client.post(url,jsonBody);System.out.println("Result: "+response.body().string());}catch(PaymentRequiredErrore){// Handle paymentPaymentAuthorizationauth=client.createPayment(e.getPaymentRequest());// Retry with paymentResponseretryResponse=client.post(url,jsonBody,auth);System.out.println("Result: "+retryResponse.body().string());}}catch(Exceptione){e.printStackTrace();}}privatestaticbyte[]loadSecretKey(){returnnewAccount().getSecretKey();}}