Skip to content

Use a proxy ​

Configure REST and WebSocket connections separately. These examples are for Node.js; browser applications cannot configure Node socket agents. Keep proxy URLs and passwords on the server, outside source control and logs.

A proxy changes your network route, not the selected exchange environment, account permissions or rate limits.

HTTP or HTTPS proxy for REST ​

Axios accepts a proxy object in the REST client's second constructor argument:

ts
import { RestClient } from '@siebly/hyperliquid-api';

export const client = new RestClient(
  {},
  {
    proxy: { protocol: 'http', host: '127.0.0.1', port: 8080 },
    timeout: 20_000,
  },
);

Set protocol, host and port to your proxy, not the exchange. Use protocol: 'https' for a TLS connection to the proxy. For proxy authentication, add auth: { username, password } using values loaded from your environment or secret store.

In Node.js, Axios can also read HTTP_PROXY, HTTPS_PROXY and NO_PROXY when no explicit proxy is configured. Do not rely on those variables for WebSockets; configure their agent or constructor explicitly.

Proxy agents for REST ​

For an HTTP CONNECT proxy, install the agent in your project:

sh
npm install https-proxy-agent

Set PROXY_URL to a full HTTP or HTTPS proxy URL. If credentials are included in the URL, URL-encode the username and password components.

ts
import { HttpsProxyAgent } from 'https-proxy-agent';
import { RestClient } from '@siebly/hyperliquid-api';

const proxyUrl = process.env.PROXY_URL;
if (!proxyUrl) throw new Error('Set PROXY_URL to your HTTP or HTTPS proxy URL.');

const agent = new HttpsProxyAgent(proxyUrl);
const networkOptions = {
  proxy: false as const,
  httpsAgent: agent,
  timeout: 20_000,
};

export const client = new RestClient({ keepAlive: false }, networkOptions);

keepAlive: false is required here because the SDK enables its own REST HTTPS agent by default. proxy: false disables Axios's own proxy routing, including proxy environment variables, so the custom agent controls the route.

HTTP or HTTPS proxy for WebSockets ​

Install Node's WebSocket implementation and the proxy agent. TypeScript projects also need its declarations:

sh
npm install ws https-proxy-agent
npm install --save-dev @types/ws

Hyperliquid uses webSocketConstructor, not wsOptions.agent. This constructor gives every new or reconnected socket the same proxy agent. The metadata REST route is configured separately with requestOptions.proxy:

ts
import { HttpsProxyAgent } from 'https-proxy-agent';
import WebSocket from 'ws';
import { WebsocketClient, type WebSocketConstructor } from '@siebly/hyperliquid-api';

const proxyUrl = process.env.PROXY_URL;
if (!proxyUrl) throw new Error('Set PROXY_URL to your HTTP or HTTPS proxy URL.');
const proxy = new URL(proxyUrl);
if (proxy.protocol !== 'http:' && proxy.protocol !== 'https:') {
  throw new Error('This example requires an HTTP or HTTPS proxy.');
}
const agent = new HttpsProxyAgent(proxyUrl);

class ProxiedWebSocket extends WebSocket {
  constructor(url: string) {
    super(url, { agent });
  }
}

export const ws = new WebsocketClient({
  webSocketConstructor: ProxiedWebSocket as unknown as WebSocketConstructor,
  requestOptions: {
    proxy: {
      protocol: proxy.protocol.slice(0, -1),
      host: proxy.hostname,
      port: Number(proxy.port || (proxy.protocol === 'https:' ? 443 : 80)),
      ...(proxy.username || proxy.password
        ? { auth: {
            username: decodeURIComponent(proxy.username),
            password: decodeURIComponent(proxy.password),
          } }
        : {}),
    },
    timeout: 20_000,
  },
});

The type assertion lets the SDK use the Node ws constructor despite differences in their WebSocket event types. The same webSocketConstructor option configures WebsocketAPIClient's WebSocket transport.

SOCKS5 ​

Install socks-proxy-agent, replace the HttpsProxyAgent import with SocksProxyAgent, and construct the agent from your SOCKS proxy URL:

sh
npm install socks-proxy-agent
ts
import { SocksProxyAgent } from 'socks-proxy-agent';

const agent = new SocksProxyAgent('socks5h://127.0.0.1:1080');

socks5h:// resolves destination hostnames through the proxy. Keep proxy: false for REST and pass the agent as httpsAgent; Axios's proxy object does not support SOCKS.

For WebSockets, use that SOCKS agent inside the custom constructor. Do not copy the HTTP-only URL parser from the preceding example.

Metadata routing: configure the separate asset-metadata REST route with requestOptions.proxy using an HTTP/HTTPS proxy. Verify both metadata and socket traffic in your proxy logs. If your network requires SOCKS for every request, verify metadata routing before using helpers that resolve assets. For a direct RestClient, set keepAlive: false with the SOCKS agent as shown above.

Verify the route ​

Start with a public REST request such as client.getAllMids() and a public subscription using the configured clients. No API credentials are needed for these checks. Confirm both connections in your proxy logs; a successful REST request alone does not prove WebSocket routing.

  • A 407 response usually means proxy authentication is missing or rejected.
  • Connection timeouts can indicate the proxy cannot reach the exchange or does not allow CONNECT/WebSocket traffic.
  • Keep TLS certificate verification enabled. For an approved corporate proxy, use its trusted CA rather than disabling verification.
  • Extra latency can affect signed-request timing. Keep the machine clock synchronized and distinguish the HTTP timeout from the exchange's acceptance window.
  • Do not log whole request configurations, proxy URLs with credentials or signed request data.

See configuration, production usage and the Siebly proxy guide.

Built by Siebly. Open source SDKs for exchange APIs.