WebSocket API
Stream real-time trading data, price feeds, and market events via high-performance WebSocket connections.
Overview
WebSocket URL: wss://turnpike.dev/api/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://turnpike.dev/api/data');
ws.onopen = () => {
console.log('Connected to Turnpike WebSocket');
// Subscribe to trades
ws.send(JSON.stringify({
method: 'subscribe',
keys: ['trades']
}));
};
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:
trades- All token trades across the platformnewTokens- Newly created tokenspriceUpdates- Real-time price updates{MINT_ADDRESS}- Events for a specific token
Success Response:
Subscribe to Specific Token
Unsubscribe
Event Types
Trade Event
Emitted when a token trade occurs.
New Token Event
Emitted when a new token is created.
Price Update Event
Emitted when token price changes significantly.
Error Event
Advanced Usage
Reconnection Logic
React Hook Example
Best Practices
Implement Reconnection: Handle connection drops gracefully
Authenticate Early: Send authentication immediately after connecting
Manage Subscriptions: Track subscriptions for reconnection
Handle Errors: Monitor error events and log them
Rate Limiting: Respect message rate limits
Heartbeat: Implement ping/pong for connection health
Clean Disconnection: Close connections properly when done
Heartbeat/Ping-Pong
Keep the connection alive with periodic pings:
Error Codes
AUTHENTICATION_FAILED
Invalid API key
SUBSCRIPTION_FAILED
Invalid subscription key
RATE_LIMIT_EXCEEDED
Too many messages
CONNECTION_LIMIT
Too many concurrent connections
Next Steps
View Lightning API for trading endpoints
Check Examples for complete implementations
Learn about Error Handling
Last updated