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 platform

  • newTokens - Newly created tokens

  • priceUpdates - 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

  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