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-liveAuthentication 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
| Field | Description |
|---|---|
| event | Always "trade" |
| timestamp | Unix timestamp of the trade |
| datetime | ISO datetime of the trade |
| tokenAddress | Address of the token being traded |
| quoteToken | Address of the quote token (e.g., SOL) |
| type | Trade type ("BUY" or "SELL") |
| price.sol | Price in SOL |
| price.usd | Price in USD |
| amount.token | Amount of tokens traded |
| amount.quote | Amount of quote token (e.g., SOL) |
| amount.usd | Value in USD |
| transaction.hash | Transaction hash |
| transaction.blockNumber | Block number |
| market.protocol | Trading protocol (e.g., "PUMP_AMM") |
| market.poolAddress | Address 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
| Type | Description |
|---|---|
| BUY | SOL was used to purchase the token |
| SELL | Token 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