Websocket

WebSocket endpoints are available for real-time data streaming on the Hyperliquid cryptocurrency exchange. The WebSocket URL for the mainnet is wss://api.hyperliquid.xyz/ws, and for the testnet, it is wss://api.hyperliquid-testnet.xyz/ws.

Connection

To connect to the WebSocket API, establish a WebSocket connection to the respective URL based on your desired network. Once connected, you can start sending subscription messages to receive real-time data updates.

Example from command line:

$ wscat -c  wss://api.hyperliquid.xyz/ws
Connected (press CTRL+C to quit)
>  { "method": "subscribe", "subscription": { "type": "trades", "coin": "SOL" } }
< {"channel":"subscriptionResponse","data":{"method":"subscribe","subscription":{"type":"trades","coin":"SOL"}}}

Note: this doc uses Typescript for defining many of the message types. If you prefer to use python, you can checkout the equivalent types in the python sdk here and example connection code here.

Subscription Messages

To subscribe to specific data feeds, you need to send a subscription message. The subscription message format is as follows:

{
  "method": "subscribe",
  "subscription": { ... }
}

The subscription object contains the details of the specific feed you want to subscribe to. Choose from the following subscription types and provide the corresponding properties:

  1. allMids:

    • Subscription message: { "type": "allMids" }

    • Data format: AllMids

  2. notification:

    • Subscription message: { "type": "notification", "user": "<address>" }

    • Data format: Notification

  3. webData2

    • Subscription message: { "type": "webData2", "user": "<address>" }

    • Data format: WebData2

  4. candle:

    • Subscription message: { "type": "candle", "coin": "<coin_symbol>", "interval": "<candle_interval>" }

    • Data format: Candle[]

  5. l2Book:

    • Subscription message: { "type": "l2Book", "coin": "<coin_symbol>" }

    • Data format: WsBook

  6. trades:

    • Subscription message: { "type": "trades", "coin": "<coin_symbol>" }

    • Data format: WsTrade[]

  7. orderUpdates:

    • Subscription message: { "type": "orderUpdates", "user": "<address>" }

    • Data format: WsOrder[]

  8. userEvents:

    • Subscription message: { "type": "user", "user": "<address>" }

    • Data format: WsUserEvent

  9. userFills:

    • Subscription message: { "type": "userFills", "user": "<address>" }

    • Data format: WsUserFills

  10. userFundings:

    • Subscription message: { "type": "userFundings", "user": "<address>" }

    • Data format: WsUserFundings

  11. userNonFundingLedgerUpdates:

    • Subscription message: { "type": "userNonFundingLedgerUpdates", "user": "<address>" }

    • Data format: WsUserNonFundingLedgerUpdates

Data Formats

The server will respond to successful subscriptions with a message containing the channel property set to "subscriptionResponse", along with the data field providing the original subscription. The server will then start sending messages with the channel property set to the corresponding subscription type e.g. "allMids" and the data field providing the subscribed data.

The data field format depends on the subscription type:

  • AllMids: All mid prices.

    • Format: AllMids { mids: Record<string, string> }

  • Notification: A notification message.

    • Format: Notification { notification: string }

  • WebData2: Aggregate information about a user, used primarily for the frontend.

    • Format: WebData2

  • WsTrade[]: An array of trade updates.

    • Format: WsTrade[]

  • WsBook: Order book snapshot updates.

    • Format: WsBook { coin: string; levels: [Array<WsLevel>, Array<WsLevel>]; time: number; }

  • WsOrder: User order updates.

    • Format: WsOrder[]

  • WsUserEvent: User events that are not order updates

    • Format: WsUserEvent { "fills": [WsFill] | "funding": WsUserFunding | "liquidation": WsLiquidation | "nonUserCancel": [WsNonUserCancel] }

  • WsUserFills : Fills snapshot followed by streaming fills

  • WsUserFundings : Funding payments snapshot followed by funding payments on the hour

  • WsUserNonFundingLedgerUpdates: Ledger updates not including funding payments: withdrawals, deposits, transfers, and liquidations

For the streaming user endpoints such as WsUserFills,WsUserFundings the first message has isSnapshot: true and the following streaming updates have isSnapshot: false.

Data Type Definitions

Here are the definitions of the data types used in the WebSocket API:

interface WsTrade {
  coin: string;
  side: string;
  px: string;
  sz: string;
  hash: string;
  time: number;
  tid: number; // ID unique across all assets
}

// Snapshot feed, pushed on each block that is at least 0.5 since last push
interface WsBook {
  coin: string;
  levels: [Array<WsLevel>, Array<WsLevel>];
  time: number;
}

interface WsLevel {
  px: string; // price
  sz: string; // size
  n: number; // number of orders
}

interface Notification {
  notification: string;
}

interface AllMids {
  mids: Record<string, string>;
}

interface Candle {
  t: number; // open millis
  T: number; // close millis
  s: string; // coin
  i: string; // interval
  o: number; // open price
  c: number; // close price
  h: number; // high price
  l: number; // low price
  v: number; // volume (base unit)
  n: number; // number of trades
}

type WsUserEvent = WsFill[] | WsUserFunding | WsLiquidation | WsNonUserCancel[];

interface WsUserFill {
  coin: string;
  px: string; // price
  sz: string; // size
  side: string;
  time: number;
  startPosition: string;
  dir: string; // used for frontend display
  closedPnl: string;
  hash: string; // L1 transaction hash
  oid: number; // order id
  crossed: boolean; // whether order crossed the spread (was taker)
  fee: string; // negative means rebate
  tid: number; // unique trade id
}

interface WsUserFunding {
  time: number;
  coin: string;
  usdc: string;
  szi: string;
  fundingRate: string;
}

interface WsLiquidation {
  lid: number;
  liquidator: string;
  liquidated_user: string;
  liquidated_ntl_pos: string;
  liquidated_account_value: string;
}

interface WsNonUserCancel {
  coin: String;
  oid: number;
}

interface WsOrder {
  order: WsBasicOrder;
  status: string; // Possible values: open, filled, canceled, triggered, rejected, marginCanceled
  statusTimestamp: number;
}

interface WsBasicOrder {
  coin: string;
  side: string;
  limitPx: string;
  sz: string;
  oid: number;
  timestamp: number;
  origSz: string;
  cloid: string | undefined;
}
WsUserNonFundingLedgerUpdates
interface WsUserNonFundingLedgerUpdate {
  time: number;
  hash: string;
  delta: WsLedgerUpdate;
}

type WsLedgerUpdate = 
  | WsDeposit
  | WsWithdraw 
  | WsInternalTransfer 
  | WsSubAccountTransfer 
  | WsLedgerLiquidation 
  | WsVaultDelta 
  | WsVaultWithdrawal
  | WsVaultLeaderCommission;
  
interface WsDeposit {
  type: "deposit";
  usdc: number;
}

interface WsWithdraw {
  type: "withdraw";
  usdc: number;
  nonce: number;
  fee: number;
}

interface WsInternalTransfer {
  type: "internalTransfer";
  usdc: number;
  user: string;
  destination: string;
  fee: number;
}

interface WsSubAccountTransfer {
  type: "subAccountTransfer";
  usdc: number;
  user: string;
  destination: string;
}

interface WsLedgerLiquidation {
  type: "liquidation";
  // NOTE: for isolated positions this is the isolated account value
  accountValue: number;
  leverageType: "Cross" | "Isolated";
  liquidatedPositions: Array<LiquidatedPosition>;
}

interface LiquidatedPosition {
  coin: string;
  szi: number;
}

interface WsVaultDelta {
  type: "vaultCreate" | "vaultDeposit" | "vaultDistribution";
  vault: string;
  usdc: number;
}

interface WsVaultWithdrawal {
  type: "vaultWithdraw";
  vault: string;
  user: string;
  requestedUsd: number;
  commission: number;
  closingCost: number;
  basis: number;
  netWithdrawnUsd: number;
}

interface WsVaultLeaderCommission {
  type: "vaultLeaderCommission";
  user: string;
  usdc: number;
}

Please note that the above data types are in TypeScript format, and their usage corresponds to the respective subscription types.

Examples

Here are a few examples of subscribing to different feeds using the subscription messages:

  1. Subscribe to all mid prices:

    { "method": "subscribe", "subscription": { "type": "allMids" } }
  2. Subscribe to notifications for a specific user:

    { "method": "subscribe", "subscription": { "type": "notification", "user": "<address>" } }
  3. Subscribe to web data for a specific user:

    { "method": "subscribe", "subscription": { "type": "webData", "user": "<address>" } }
  4. Subscribe to candle updates for a specific coin and interval:

    { "method": "subscribe", "subscription": { "type": "candle", "coin": "<coin_symbol>", "interval": "<candle_interval>" } }
  5. Subscribe to order book updates for a specific coin:

    { "method": "subscribe", "subscription": { "type": "l2Book", "coin": "<coin_symbol>" } }
  6. Subscribe to trades for a specific coin:

    { "method": "subscribe", "subscription": { "type": "trades", "coin": "<coin_symbol>" } }

Unsubscribing from WebSocket Feeds

To unsubscribe from a specific data feed on the Hyperliquid WebSocket API, you need to send an unsubscribe message with the following format:

jsonCopy code{
  "method": "unsubscribe",
  "subscription": { ... }
}

The subscription object should match the original subscription message that was sent when subscribing to the feed. This allows the server to identify the specific feed you want to unsubscribe from. By sending this unsubscribe message, you inform the server to stop sending further updates for the specified feed.

Please note that unsubscribing from a specific feed does not affect other subscriptions you may have active at that time. To unsubscribe from multiple feeds, you can send multiple unsubscribe messages, each with the appropriate subscription details.

Timeouts and Heartbeats

The server will close any connection if it hasn't sent a message to it in the last 60 seconds. If you are subscribing to a channel that doesn't receive messages every 60 seconds, you can send heartbeat messages to keep your connection alive. The format for these messages are:

{ "method": "ping" }

The server will respond with:

{ "channel": "pong" }

Last updated