Stream Real-Time Whale Swaps with WebSockets
Polling a whale-activity endpoint on an interval has two problems: you miss whatever happens between polls, and you burn through your daily call budget checking for events that aren’t there yet. A WebSocket push feed solves both — the server sends you a swap event the moment it happens, and nothing when it doesn’t. This playbook covers the real-time whale swap feed: large on-chain DEX swaps detected on BSC and Avalanche, pushed to you as they occur.
The problem: polling misses events
If you poll a whale-events endpoint every 30 seconds, you only ever see a snapshot — two swaps that both happened in that window look identical to one, and a big swap that lands right after your poll waits up to 30 seconds before you know about it. For a live alert bot or a dashboard ticker, that lag and that call overhead both matter. A persistent WebSocket connection removes both problems: the server pushes each qualifying swap the instant it’s detected, and idle time costs you nothing.
This is the same tradeoff every real-time integration eventually runs into: REST polling is simple to write but scales badly with how often you actually need fresh data, while a persistent socket connection is a bit more code up front in exchange for genuinely real-time delivery. For a feed like whale swaps — where individual events are what you care about, not periodic snapshots — the socket is the better fit, and this playbook covers the full connection lifecycle so you don’t have to reverse-engineer it from trial and error.
Prerequisites
- Python 3.8+ with
pip install requests websockets, or any browser for the JS variant - A Trader-tier API key ($29/mo, 3000 calls/day, all symbols) — see below
The ticket-then-connect flow
You can’t put a long-lived API key directly in a wss:// URL
— browsers can’t set custom headers on a WebSocket handshake, so the key would end up
sitting in server logs and browser history. Instead, the flow is a two-step handshake:
- POST your API key to
/v1/ws/ticketand get back a single-use ticket good for 60 seconds. - Open the WebSocket with that ticket in the query string, before it expires.
Step 1 — mint a ticket
Step 2 — connect
Only Trader tier and above may actually open this socket. A free-tier ticket will connect the handshake but the server immediately responds:
Python: mint a ticket and stream
This script mints a ticket, connects with the websockets library,
and prints every swap event above a configurable USD threshold. It mints a fresh ticket and reconnects
automatically if the connection drops, since each ticket is single-use.
The ticket-then-connect flow above only completes for Trader, Pro, and Enterprise accounts. $29/mo gets you the live socket plus all symbols and 3000 calls/day on the REST API.
See pricing →JavaScript: browser WebSocket variant
The same two-step flow works from a browser using the native
WebSocket object. One caveat: browsers don’t expose the HTTP status code of a failed
WebSocket handshake, so a 402 from an under-tier key just looks like the socket closing without ever
sending a hello frame — the code below notes this.
Expected output
Right after connecting, expect a single hello frame describing your
session:
Then, roughly every 20 seconds of inactivity, a heartbeat keeps the connection alive:
And whenever a swap of at least $500 is detected on BSC or Avalanche, a
swap frame arrives with the full event:
Only swaps at or above $500 notional are broadcast at all — smaller swaps never reach the socket, so there’s no need to filter noise below that floor yourself.
What to build next
- No key yet, or just need public display?
GET /v1/stream/public-swapsis a public Server-Sent-Events stream of the same swap event JSON, with no auth and no tier requirement — a good fit for a read-only browser ticker without needing WebSocket tickets at all. - Route
swapframes into a Telegram or Discord bot for real-time whale alerts above a chosen USD threshold. - Track
swapper_labelover time to notice when a labeled wallet (exchange, fund, or known address) becomes unusually active on a chain. - Pair this feed with the REST whale endpoints to look up historical context for a swapper the moment they show up on the live feed.
- Log every
swapframe to a local database so you have your own queryable history of large swaps, instead of re-fetching a REST endpoint for the same window later.
The reconnect-with-a-fresh-ticket pattern in the Python script above is worth keeping even once the feed is stable in production: network blips and periodic upstream restarts are normal for any long-lived connection, and a single-use, short-lived ticket means there’s no long-lived credential sitting in a URL to worry about if a client is compromised.
Full endpoint reference is in the API documentation, and more integration patterns are in the code playbooks hub.
Unlock the live whale swap socket
Trader tier ($29/mo) includes the WebSocket feed, all symbols, and 3000 calls/day on the REST API. Start on the free tier to explore the REST endpoints first, then upgrade when you’re ready to go real-time.
View Trader plan — $29/mo