Utilities

Comprehensive utilities for building on r1x. All utilities are TypeScript-typed and documented.

x402 Payment Utilities

src/lib/x402.ts

Core x402 protocol utilities for payment quotes, verification, and settlement with PayAI facilitator.

Available Functions

  • generatePaymentQuote() - Create payment quotes with fee calculation
  • verifyPaymentWithFacilitator() - Verify payments via PayAI
  • settlePaymentWithFacilitator() - Settle verified payments
  • calculateFeeDistribution() - Calculate platform fees
  • create402Response() - Generate HTTP 402 Payment Required responses
  • parsePaymentProof() - Parse payment proofs from headers or body

Wallet Integration

src/lib/wallet.ts

Base network wallet utilities for connecting wallets, transferring USDC, and managing approvals.

Available Functions

  • connectWallet() - Connect MetaMask or other Web3 wallets
  • transferUSDC() - Transfer USDC tokens on Base
  • approveUSDC() - Approve USDC spending
  • getUSDCBalance() - Check USDC balance
  • formatUSDC() - Format USDC amounts for display

PayAI Service Sync

src/lib/payai-sync.ts

Synchronize services from PayAI facilitator to your database. Handles service discovery and normalization.

Available Functions

  • fetchPayAIServices() - Fetch services from PayAI facilitator
  • syncPayAIServices() - Sync services to database
  • normalizePayAIService() - Normalize PayAI service format
  • extractCategory() - Automatically categorize services

Panel Authentication

src/lib/panel-auth.ts

Authentication utilities for user and platform panels. Wallet-based access control.

Available Functions

  • verifyAdminAccess() - Verify admin wallet addresses
  • getUserFromRequest() - Extract user address from request

Fee Transfer

src/lib/fee-transfer.ts

On-chain fee transfer utilities. Transfers platform fees to fee recipient address.

Available Functions

  • transferFee() - Transfer fees on-chain
  • batchTransferFees() - Batch transfer multiple fees

Agent Payment Handler

src/lib/agent-payment.ts

Utilities for r1x Agent to handle x402 payments. Processes payment requests and guides users.

Available Functions

  • handlePaymentRequest() - Process agent payment requests
  • generatePaymentMessage() - Create payment guidance messages

Usage Examples

Generate Payment Quote

import { generatePaymentQuote } from '@/lib/x402';

const quote = await generatePaymentQuote(
  '1.5', // Amount in USDC
  '0x...', // Merchant address
  { feePercentage: 5 } // Fee config
);

// Returns PaymentQuote with amount, token, merchant, deadline, nonce

Connect Wallet

import { connectWallet } from '@/lib/wallet';

const wallet = await connectWallet();
// Automatically switches to Base network if needed
// Returns { address, chainId, provider }

Sync PayAI Services

import { syncPayAIServices } from '@/lib/payai-sync';

const result = await syncPayAIServices();
// { synced: 10, errors: 0 }
// Services are automatically synced to database
Type Safety
All utilities are fully typed with TypeScript. Import types from @/lib/types/x402 for PaymentQuote, PaymentProof, and other x402 types.
server.r1xlabs.com