Trades Live WebSocket

Real-time trade data service that provides live updates for token trades via WebSocket connections.

WebSocket Endpoint

wss://api.solapi.live/ws/trades-live

Authentication is required using the x-api-key header.

Connection and Subscription

1
Connect to the WebSocket endpoint

You'll receive a welcome message:

{
  "type": "connected",
  "message": "Connected to TradesLive WebSocket. Send a JSON object with \"tokenAddress\"."
}

2
Send subscription message

{
  "tokenAddress": "YOUR_TOKEN_ADDRESS"
}

3
Receive confirmation

{
  "type": "subscriptionConfirmed",
  "message": "Subscribed to TOKEN_ADDRESS"
}

4
Receive real-time trade data

{
  "event": "trade",
  "timestamp": 1746027341,
  "datetime": "2025-04-30T15:35:41.000Z",
  "tokenAddress": "TOKEN_ADDRESS",
  "quoteToken": "So11111111111111111111111111111111111111112",
  "type": "BUY",
  "price": {
    "sol": 1.1187111217184e-7,
    "usd": 0.00001597687288482148
  },
  "amount": {
    "token": 17698939096615,
    "quote": 1980000001,
    "usd": 282.773700142815
  },
  "transaction": {
    "hash": "4ncJXs7rHd57WgLqtUSAm9FtSU5rYQrPf4VgZTCGxxiN8N5QWgjG9iT2v5izXn74jdHJmowUbmCn75nLjE4X8mk2",
    "blockNumber": 336925418
  },
  "market": {
    "protocol": "PUMP_AMM",
    "poolAddress": "5aRxAKpvaAYgUzmDDEfFVwYoBNq7vWPdwX1EQNWoB7Uj"
  }
}

Trade Data Fields

FieldDescription
eventAlways "trade"
timestampUnix timestamp of the trade
datetimeISO datetime of the trade
tokenAddressAddress of the token being traded
quoteTokenAddress of the quote token (e.g., SOL)
typeTrade type ("BUY" or "SELL")
price.solPrice in SOL
price.usdPrice in USD
amount.tokenAmount of tokens traded
amount.quoteAmount of quote token (e.g., SOL)
amount.usdValue in USD
transaction.hashTransaction hash
transaction.blockNumberBlock number
market.protocolTrading protocol (e.g., "PUMP_AMM")
market.poolAddressAddress of the liquidity pool

Example Usage

import WebSocket from 'ws';

const TOKEN_ADDRESS = 'Dfa6ja8wC18CKHem5QDuS3iV1H2ktqDkJqdgjWhJpump';
const API_KEY = 'your-api-key-here';

console.log(`Starting TradesLive WebSocket client for token: ${TOKEN_ADDRESS}`);

// Create WebSocket connection with API key
const ws = new WebSocket('wss://api.solapi.live/ws/trades-live', {
    headers: {
        'x-api-key': API_KEY
    }
});

ws.on('open', () => {
    console.log('Connected to WebSocket server');
    // Send subscription message
    const message = {
        tokenAddress: TOKEN_ADDRESS
    };
    ws.send(JSON.stringify(message));
});

ws.on('message', (data) => {
    try {
        const message = JSON.parse(data.toString());
        
        // Handle different message types
        if (message.type === 'connected' || message.type === 'subscriptionConfirmed') {
            console.log(message.message);
        } else if (message.type === 'error') {
            console.error('Error:', message.message);
        } else if (message.event === 'trade') {
            // Format trade data nicely
            console.log('\nNew Trade:');
            console.log('==========');
            console.log(`Type: ${message.type}`);
            console.log(`Time: ${message.datetime}`);
            
            // Price information
            console.log('\nPrice:');
            console.log(`SOL: ${message.price.sol}`);
            console.log(`USD: $${message.price.usd}`);
            
            // Amount information
            console.log('\nAmount:');
            console.log(`Tokens: ${message.amount.token}`);
            console.log(`Quote (SOL): ${message.amount.quote}`);
            console.log(`USD Value: $${message.amount.usd}`);
            
            // Transaction details
            console.log('\nTransaction:');
            console.log(`Hash: ${message.transaction.hash}`);
            console.log(`Block: ${message.transaction.blockNumber}`);
            
            // Market information
            console.log('\nMarket:');
            console.log(`Protocol: ${message.market.protocol}`);
            console.log(`Pool: ${message.market.poolAddress}`);
            
            console.log('\n' + '-'.repeat(50));
        }
    } catch (error) {
        console.error('Error parsing message:', error.message);
    }
});

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

ws.on('close', (code, reason) => {
    console.log('Disconnected');
});

// Handle SIGINT for graceful shutdown
process.on('SIGINT', () => {
    console.log('Closing connection...');
    ws.close();
    process.exit(0);
});

Error Handling

If an error occurs, you'll receive a message in this format:

{
  "type": "error",
  "message": "Error description"
}

Common errors:

  • Missing tokenAddress in subscription message
  • Invalid JSON format
  • Unauthorized API key
  • Invalid token address format
  • Token not found or no liquidity pools available

Trade Types

TypeDescription
BUYSOL was used to purchase the token
SELLToken was sold for SOL

Notes

  • The WebSocket connection will send trades in real-time as they occur on-chain
  • All token amounts are returned in the token's smallest unit
  • USD prices are approximate and based on current market rates
  • Connection may be terminated after 24 hours of inactivity
  • Maximum connections per API key is limited to 5 concurrent connections