Configure an HTTP REST Proxy
Some private WebSocket subscriptions fetch a token over REST before connecting. The socket can use the proxy while that HTTP request still takes the default route. Configure each client that opens a connection.
Proxy configuration selects a network route. It does not change account permissions, exchange availability, or the request budget required by the application.
REST networking options are the second constructor argument in this Bybit example. Save it as rest-proxy.mjs; it requires bybit-api, https-proxy-agent, and a reachable HTTP proxy:
rest-proxy.mjs
import { HttpsProxyAgent } from 'https-proxy-agent';
import { RestClientV5 } from 'bybit-api';
if (!process.env.HTTP_PROXY_URL) throw new Error('Set HTTP_PROXY_URL');
const agent = new HttpsProxyAgent(process.env.HTTP_PROXY_URL);
const client = new RestClientV5({}, { proxy: false, httpAgent: agent, httpsAgent: agent });
const response = await client.getTickers({ category: 'linear', symbol: 'BTCUSDT' });
if (response.retCode !== 0) throw new Error(response.retMsg);
console.log(response.result.list);
proxy: false selects the supplied agents instead of Axios proxy discovery. The example instantiates the agent before using it and makes a public request, so its first routing check does not require exchange credentials.
The SDK's REST proxy example documents the constructor boundary. Native Axios proxy configuration is another supported path; do not combine independent proxy mechanisms without checking which one owns the connection.
Configure a SOCKS WebSocket Agent
This Binance public stream requires binance, socks-proxy-agent, and a reachable SOCKS proxy:
ws-proxy.mjs
import { SocksProxyAgent } from 'socks-proxy-agent';
import { WebsocketClient } from 'binance';
if (!process.env.SOCKS_PROXY_URL) throw new Error('Set SOCKS_PROXY_URL');
const agent = new SocksProxyAgent(process.env.SOCKS_PROXY_URL);
const client = new WebsocketClient({ wsOptions: { agent } });
client.on('message', (message) => {
if (!Array.isArray(message) && 'e' in message && message.e === '24hrMiniTicker') {
console.log({ symbol: message.s, event: message.e });
}
});
client.on('exception', () => console.error('WebSocket proxy connection failed'));
const stop = () => client.closeAll();
process.once('SIGINT', stop);
setTimeout(stop, 20_000);
await client.subscribe('btcusdt@miniTicker', 'main');
The SDK SOCKS example uses wsOptions.agent for the socket. This is a different constructor boundary from the REST client's second argument.
Include Internal REST Requests
Some authenticated stream workflows obtain a connection token or session through REST. Where the selected client exposes requestOptions for that work, configure its HTTP agents as well as wsOptions.agent. The exchange tutorial and the installed client's types establish which option applies.
A successful public socket tests only that socket route. Check the required token request separately before diagnosing the private failure as a signature problem. Keep proxy credentials and complete connection URLs out of logs.
Diagnose Added Delay
Measure request duration and clock error separately after introducing a proxy. Extra queuing or network transit can consume a request's validity window. A larger window cannot repair an unreachable proxy, wrong credential environment, or unsupported exchange route. The timestamp diagnostic covers that distinction.
Related articles
Continue from here