Skip to content

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

  1. Node.js >= 18
  2. Install dependencies:
    npm install undici ws dotenv @solana/web3.js @solana/spl-token
    

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

  1. Get Invoice: Requests a payment invoice from /connect?duration_min=5&network=sol
  2. Pay Invoice: Builds and signs a Solana transaction using x402 exact scheme
  3. Connect WebSocket: Connects to the news stream using the access token
  4. Process Messages: Receives news messages and handles expiry warnings
  5. Auto-Extend: Automatically extends the session when expiry_soon messages are received

Key Functions

  • buildExactPaymentTx() - Builds the Solana transaction for x402 exact payment
  • getInvoice() - Requests payment invoice from Bridge402
  • payInvoice() - Signs and submits payment transaction
  • extendSession() - Extends an existing session when needed

Message Types

The WebSocket receives different message types:

  • status: 'connected' - Connection confirmation
  • type: '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:

# Create test-keypair.json
echo '[1,2,3,...]' > test-keypair.json

⚠️ Warning: Never use production keypairs or commit keypair files to version control.

Support

For questions about this example or the Bridge402 API:

  1. Check the main API documentation
  2. Review the example code comments
  3. Join our Discord community