Guides

WebSocket

Stream live data (trades, order books, mempool, social feeds) through the same key and balance. Pikeway proxies the socket and bills the session when it closes.

Connect

Streaming endpoints are marked WS on an API's marketplace page. Connect to the gateway instead of the provider, keeping the provider's path and query:

GETwss://gw.pikeway.ai/ws/<slug>/<path>
javascript
import WebSocket from 'ws';

// Server-side: send the key as a header
const ws = new WebSocket('wss://gw.pikeway.ai/ws/<slug>/<path>', {
  headers: { Authorization: `Bearer ${process.env.PIKEWAY_KEY}` },
});

ws.on('open', () => ws.send(JSON.stringify({ op: 'subscribe', channel: 'trades' })));
ws.on('message', (data) => console.log(data.toString()));
ws.on('close', (code, reason) => {
  if (code === 4402) console.log('balance or key limit ran out');
  if (code === 4408) console.log('max session length reached, reconnect');
});
Authentication
Authorization: Bearer pk-…
Preferred for servers and agents.
?key=pk-…
For browsers, which can't set WebSocket headers. The gateway removes the key before forwarding to the provider.

Subprotocols you request are passed through to the provider. The handshake response includes X-Pikeway-Call-Id, which identifies the session in your call log.

Pricing

A session can have up to three price components, set by the provider and shown on the endpoint:

per connectionUSD
Charged once per session. Defaults to the endpoint's normal price. Waived while your subscription plan still has included calls.
per messageUSD
Charged for each message the provider sends you. Messages you send are free.
per minuteUSD
Charged for connected time; a started minute counts as a full minute.

The running cost is tracked live and the whole session is charged once, when it closes. If the provider's server refuses the connection, you get an HTTP error from the handshake and nothing is charged.

Limits and close codes

  • Before connecting, your balance (or the key's remaining limit, whichever is lower) must cover the connection fee, otherwise the handshake fails with 402.
  • At most 10 concurrent connections per key; the 11th gets 429.
  • Sessions last up to 60 minutes unless the provider sets a different maximum (up to 24 hours). Reconnect when they end.
Close codes
4402
Your balance or the key's spend limit ran out mid-stream. The message that crossed the limit is not delivered or charged. Top up and reconnect.
4408
Maximum session length reached. Reconnect to continue.

Handshake errors

401
Missing, invalid or revoked key.
403
Key restricted by IP or site, or the API requires a subscription.
404
Unknown API, or no WebSocket endpoint matches the path.
426
The request wasn't a WebSocket upgrade.
429
Rate limit or the 10-connection cap.
Embedding a stream in a web page? Combine ?key= with a browser key and a spending limit, so a key copied from your page can't be used elsewhere.