Overview
Bridge402 Examples¶
This folder contains example implementations for integrating with the Bridge402 API using x402 payments.
Solana WebSocket Example¶
The Solana Example demonstrates how to connect to the Bridge402 news WebSocket stream using Solana (x402) payments.
Features¶
- ✅ Solana Payment Integration: Pays invoice using Solana x402 exact scheme
 - ✅ WebSocket Connection: Connects to Bridge402 news stream
 - ✅ Automatic Session Extension: Extends sessions when expiry warnings are received
 - ✅ Real-time News Processing: Receives and processes news messages as they arrive
 - ✅ Error Handling: Robust error handling for payment and connection issues
 
Prerequisites¶
- Node.js >= 18
 - Install dependencies:
 
Configuration¶
Set the following environment variables:
# Required
BASE_URL=https://bridge402.tech
SOLANA_RPC=https://api.mainnet-beta.solana.com
KEYPAIR_PATH=path/to/your/keypair.json
# OR
KEYPAIR_JSON='[1,2,3,...]'  # JSON array of secret key
# Optional
SOL_USDC_MINT=EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v
SOL_FEE_PAYER=<fee_payer_address>
FACILITATOR_URL=https://facilitator.payai.network
Usage¶
# Set environment variables
export BASE_URL="https://bridge402.tech"
export SOLANA_RPC="https://api.mainnet-beta.solana.com"
export KEYPAIR_PATH="test-keypair.json"
# Run the example
node prod-test-sol.js
How It Works¶
- Get Invoice: Requests a payment invoice from 
/connect?duration_min=5&network=sol - Pay Invoice: Builds and signs a Solana transaction using x402 exact scheme
 - Connect WebSocket: Connects to the news stream using the access token
 - Process Messages: Receives news messages and handles expiry warnings
 - Auto-Extend: Automatically extends the session when 
expiry_soonmessages are received 
Key Functions¶
buildExactPaymentTx()- Builds the Solana transaction for x402 exact paymentgetInvoice()- Requests payment invoice from Bridge402payInvoice()- Signs and submits payment transactionextendSession()- Extends an existing session when needed
Message Types¶
The WebSocket receives different message types:
status: 'connected'- Connection confirmationtype: 'expiry_soon'- Session expiry warning (triggers auto-extension)type: 'session_extended'- Server acknowledgment of session extension- News messages - Real-time news updates from Tree of Alpha
 
Error Handling¶
The example handles: - Payment failures (insufficient funds, invalid signatures) - Connection issues (WebSocket disconnections) - Network errors (RPC failures, API errors) - Missing keypair (helpful error messages)
Generating a Test Keypair¶
To generate a test keypair for development:
node -e "const { Keypair } = require('@solana/web3.js'); const kp = Keypair.generate(); console.log(JSON.stringify(Array.from(kp.secretKey)))"
Save the output to a file:
⚠️ Warning: Never use production keypairs or commit keypair files to version control.
Support¶
For questions about this example or the Bridge402 API:
- Check the main API documentation
 - Review the example code comments
 - Join our Discord community