WebSocket API

Stream real-time x402 payment data, transaction events, and network activity via high-performance WebSocket connections.

Overview

WebSocket URL: wss://api.yuzu402.dev/data

Connection Limits:

  • Standard: 5 concurrent connections, 50 messages/second

  • Premium: 20 concurrent connections, 200 messages/second

Connecting

JavaScript/TypeScript

const ws = new WebSocket('wss://api.yuzu402.dev/data');

ws.onopen = () => {
  console.log('Connected to Yuzu402 WebSocket');

  // Subscribe to payments
  ws.send(JSON.stringify({
    method: 'subscribe',
    keys: ['payments']
  }));
};

ws.onmessage = (event) => {
  const data = JSON.parse(event.data);
  console.log('Received:', data);
};

ws.onerror = (error) => {
  console.error('WebSocket error:', error);
};

ws.onclose = (event) => {
  console.log('Connection closed:', event.code, event.reason);
};

Python

Authentication

For WebSocket connections, include your API key in the initial connection message:

You'll receive a confirmation:

Subscription Methods

Subscribe to Channels

Available Channels:

  • payments - All payment transactions across the network

  • verifications - Payment verification events

  • settlements - Payment settlement confirmations

  • {WALLET_ADDRESS} - Events for a specific wallet address

Success Response:

Subscribe to Specific Wallet

Unsubscribe

Event Types

Payment Event

Emitted when a payment transaction occurs on the x402 network.

Verification Event

Emitted when a payment is verified by a facilitator.

Settlement Event

Emitted when a payment is settled and confirmed on-chain.

Error Event

Advanced Usage

Reconnection Logic

React Hook Example

Best Practices

  1. Implement Reconnection: Handle connection drops gracefully

  2. Authenticate Early: Send authentication immediately after connecting

  3. Manage Subscriptions: Track subscriptions for reconnection

  4. Handle Errors: Monitor error events and log them

  5. Rate Limiting: Respect message rate limits

  6. Heartbeat: Implement ping/pong for connection health

  7. Clean Disconnection: Close connections properly when done

Heartbeat/Ping-Pong

Keep the connection alive with periodic pings:

Error Codes

Code
Description

AUTHENTICATION_FAILED

Invalid API key

SUBSCRIPTION_FAILED

Invalid subscription key

RATE_LIMIT_EXCEEDED

Too many messages

CONNECTION_LIMIT

Too many concurrent connections

Next Steps

Last updated