Skip to main content

Overview

The SDK provides three WebSocket-based event streaming methods:
  • listen - Subscribe to user-specific events (orders, positions, deposits, withdrawals)
  • listenToKalshiEvent - Subscribe to market ticker updates for a specific Kalshi event
  • listenToOrderbook - Subscribe to real-time orderbook snapshots and deltas for a specific market
All methods support automatic reconnection with exponential backoff.
To avoid ambiguity, we denote the smallest possible multiple of USDC (0.000001 USDC) as one uusdc, which stands for µUSDC (micro-USDC), and denote the smallest possible multiple of a contract (0.01 contract) as one ccontract (centicontract).User-facing USDC balances are specified as fixed-point strings (e.g. "1.2625" for USDC). Contract quantities in the API and SDK are specified as integer ccontracts strings (e.g. "1050" for 10.50 contracts at precision 2).

User Events

Subscribe to events for a specific user address.

listen

Parameters

string
required
User’s Ethereum address to listen for events
function
required
Callback function that receives events as they occur
function
Optional callback for handling WebSocket errors
function
Optional callback invoked when WebSocket connection is established
function
Optional callback invoked when WebSocket connection closes
boolean
Enable automatic reconnection (default: true). Uses exponential backoff with max 5 attempts.

Returns

A disconnect function to stop listening and close the WebSocket connection.

Event Types

The callback receives one of the following event types: Order Events:
  • order_placed - New order submitted
  • order_filled - Order matched and executed
  • order_cancelled - Order cancelled
Position Events:
  • position_minted - Position tokens withdrawn from exchange to wallet
  • position_burned - Position tokens deposited from wallet back to exchange
USDC Events:
  • usdc_deposited - USDC deposited to vault
  • usdc_withdrawn - USDC withdrawn from vault
Market Events:
  • market_opened - Market opened for trading
  • market_closed - Market closed for trading
  • market_settled - Market outcome determined

Example

Market Ticker Updates

Subscribe to real-time price and volume updates for a specific Kalshi event.

listenToKalshiEvent

Parameters

string
required
Kalshi event ticker (e.g., "KXFEDDECISION-26JAN")
function
required
Callback function that receives ticker updates for markets in the event
function
Optional callback for handling WebSocket errors
function
Optional callback invoked when WebSocket connection is established
function
Optional callback invoked when WebSocket connection closes
boolean
Enable automatic reconnection (default: true). Uses exponential backoff with max 5 attempts.

Returns

A disconnect function to stop listening and close the WebSocket connection.

Ticker Update Fields

string
Market ticker identifier
string
Current best bid price for YES side in µUSDC (integer string)
string
Current best ask price for YES side in µUSDC (integer string)
string
Current best bid price for NO side in µUSDC (integer string)
string
Current best ask price for NO side in µUSDC (integer string)
string
Last traded price in µUSDC (integer string)
number
Total trading volume
number
Current open interest (outstanding contracts)

Example

Orderbook Updates

Subscribe to real-time orderbook data for a specific market, including initial snapshots and incremental updates.

listenToOrderbook

Parameters

string
required
Kalshi market ticker (e.g., "KXBTC-100K-DEC31")
function
required
Callback function that receives orderbook updates (snapshots and deltas)
function
Optional callback for handling WebSocket errors
function
Optional callback invoked when WebSocket connection is established
function
Optional callback invoked when WebSocket connection closes
boolean
Enable automatic reconnection (default: true). Uses exponential backoff with max 5 attempts.

Returns

A disconnect function to stop listening and close the WebSocket connection.

Update Types

The callback receives one of two update types: Snapshot Update (OrderbookSnapshot):
  • Sent immediately upon connection
  • Contains the complete current orderbook state
  • Fields: type, market_ticker, yes, no
Delta Update (OrderbookDelta):
  • Sent for each orderbook change
  • Contains incremental updates to specific price levels
  • Fields: type, market_ticker, side, price_uusdc, delta

Example

Connection Management

All three methods return a disconnect function that should be called to properly clean up the WebSocket connection:

Automatic Reconnection

The SDK automatically handles connection failures with exponential backoff:
  • Initial reconnect delay: 1 second
  • Maximum reconnect delay: 30 seconds
  • Maximum reconnect attempts: 5
  • Heartbeat interval: 30 seconds
When the maximum number of reconnect attempts is reached, the connection will stop trying to reconnect. You can manually reconnect by calling the listen method again.

Error Handling

Important Notes

  • WebSocket connections are automatically kept alive with periodic heartbeat messages
  • Events are delivered in real-time as they occur on the blockchain or Kalshi platform
  • Multiple subscriptions can be active simultaneously (user events, market tickers, orderbooks)
  • Reconnection is enabled by default but can be disabled by setting reconnect: false
  • Always call the disconnect function to avoid memory leaks when cleaning up
  • For orderbook subscriptions, always process the initial snapshot before applying deltas
  • Orderbook delta updates are cumulative - add them to existing ccontracts, don’t replace