Transports

A Transport is the layer responsible for executing requests to Hyperliquid servers.

HttpTransport

Executes requests via HTTP POST to the Hyperliquid API.

Import

import { HttpTransport } from "@nktkas/hyperliquid";

Usage

import { HttpTransport, InfoClient } from "@nktkas/hyperliquid";

const transport = new HttpTransport();
const client = new InfoClient({ transport });

const mids = await client.allMids();

Parameters

isTestnet (optional)

  • Type: boolean

  • Default: false

Use testnet endpoints instead of mainnet.

const transport = new HttpTransport({
  isTestnet: true,
});

timeout (optional)

  • Type: number | null

  • Default: 10000

Request timeout in milliseconds. Set to null to disable.

const transport = new HttpTransport({
  timeout: 30000,
});

apiUrl (optional)

  • Type: string | URL

  • Default: https://api.hyperliquid.xyz (mainnet) or https://api.hyperliquid-testnet.xyz (testnet)

Custom API URL for info and exchange requests.

const transport = new HttpTransport({
  apiUrl: "https://custom-api.example.com",
});

rpcUrl (optional)

  • Type: string | URL

  • Default: https://rpc.hyperliquid.xyz (mainnet) or https://rpc.hyperliquid-testnet.xyz (testnet)

Custom RPC URL for explorer requests (block details, transactions).

const transport = new HttpTransport({
  rpcUrl: "https://custom-rpc.example.com",
});

fetchOptions (optional)

Custom options passed to the underlying fetch call.

const transport = new HttpTransport({
  fetchOptions: {
    headers: {
      "X-Custom-Header": "value",
    },
  },
});

WebSocketTransport

WebSocket connection for subscriptions and POST requests.

Import

import { WebSocketTransport } from "@nktkas/hyperliquid";

Usage

Subscription:

import { SubscriptionClient, WebSocketTransport } from "@nktkas/hyperliquid";

const transport = new WebSocketTransport();
const client = new SubscriptionClient({ transport });

const subscription = await client.allMids((data) => {
  console.log(data.mids);
});

POST request:

import { InfoClient, WebSocketTransport } from "@nktkas/hyperliquid";

const transport = new WebSocketTransport();
const client = new InfoClient({ transport });

const mids = await client.allMids();

Reconnection

When connection drops, the transport automatically reconnects with exponential backoff (max 10 seconds) and 3 attempts.

After reconnection, all active subscriptions are automatically restored if resubscribe: true (default).

Parameters

isTestnet (optional)

  • Type: boolean

  • Default: false

Use testnet endpoints instead of mainnet.

const transport = new WebSocketTransport({
  isTestnet: true,
});

timeout (optional)

  • Type: number | null

  • Default: 10000

Request timeout in milliseconds. Set to null to disable.

const transport = new WebSocketTransport({
  timeout: 30000,
});

url (optional)

  • Type: string | URL

  • Default: wss://api.hyperliquid.xyz/ws (mainnet) or wss://api.hyperliquid-testnet.xyz/ws (testnet)

Custom WebSocket URL.

const transport = new WebSocketTransport({
  url: "wss://custom-api.example.com/ws",
});

reconnect (optional)

  • Type: object

  • Default: { maxRetries: 3, connectionTimeout: 10000, reconnectionDelay: (attempt) => Math.min(~~(1 << attempt) * 150, 10000) }

Reconnection policy. See ReconnectingWebSocketOptions.

const transport = new WebSocketTransport({
  reconnect: {
    maxRetries: 10,
    reconnectionDelay: 1000, // fixed 1s delay
  },
});

resubscribe (optional)

  • Type: boolean

  • Default: true

Automatically restore active subscriptions after reconnection.

const transport = new WebSocketTransport({
  resubscribe: false, // handle resubscription manually
});

Last updated