This page describes subscribing to data streams using the WebSocket API.
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:
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.
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:
interfaceWsTrade { coin:string; side:string; px:string; sz:string; hash:string; time:number; tid:number; // ID unique across all assets users: [string,string]}// Snapshot feed, pushed on each block that is at least 0.5 since last pushinterfaceWsBook { coin:string; levels: [Array<WsLevel>,Array<WsLevel>]; time:number;}interfaceWsLevel { px:string; // price sz:string; // size n:number; // number of orders}interfaceNotification { notification:string;}interfaceAllMids { mids:Record<string,string>;}interfaceCandle { 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}typeWsUserEvent= {"fills":WsFill[]} | {"funding":WsUserFunding} | {"liquidation":WsLiquidation} | {"nonUserCancel":WsNonUserCancel[]};interfaceWsUserFills { isSnapshot?:boolean; user:string; fills:Array<WsFill>;}interfaceWsFill { 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 liquidation?:FillLiquidation; feeToken:string; // the token the fee was paid in builderFee?:string; // amount paid to builder, also included in fee}interfaceFillLiquidation { liquidatedUser?:string; markPx:number; method:"market"|"backstop";}interfaceWsUserFunding { time:number; coin:string; usdc:string; szi:string; fundingRate:string;}interfaceWsLiquidation { lid:number; liquidator:string; liquidated_user:string; liquidated_ntl_pos:string; liquidated_account_value:string;}interfaceWsNonUserCancel { coin:String; oid:number;}interfaceWsOrder { order:WsBasicOrder; status:string; // Possible values: open, filled, canceled, triggered, rejected, marginCanceled statusTimestamp:number;}interfaceWsBasicOrder { coin:string; side:string; limitPx:string; sz:string; oid:number; timestamp:number; origSz:string; cloid:string|undefined;}interfaceWsActiveAssetCtx { coin:string; ctx:PerpsAssetCtx;}interfaceWsActiveSpotAssetCtx { coin:string; ctx:SpotAssetCtx;}typeSharedAssetCtx= { dayNtlVlm:number; prevDayPx:number; markPx:number; midPx?:number;};typePerpsAssetCtx=SharedAssetCtx& { funding:number; openInterest:number; oraclePx:number;};typeSpotAssetCtx=SharedAssetCtx& { circulatingSupply:number;};interfaceWsActiveAssetData { user:string; coin:string; leverage:Leverage; maxTradeSzs: [number,number]; availableToTrade: [number,number];}interfaceWsTwapSliceFill { fill:WsFill; twapId:number;}interfaceWsUserTwapSliceFills { isSnapshot?:boolean; user:string; twapSliceFills:Array<WsTwapSliceFill>;}interfaceTwapState { coin:string; user:string; side:string; sz:number; executedSz:number; executedNtl:number; minutes:number; reduceOnly:boolean; randomize:boolean; timestamp:number;}typeTwapStatus="activated"|"terminated"|"finished"|"error";interfaceWsTwapHistory { state:TwapState; status: { status:TwapStatus; description:string; }; time:number;}interfaceWsUserTwapHistory { isSnapshot?:boolean; user:string; history:Array<WsTwapHistory>;}
To unsubscribe from a specific data feed on the Hyperliquid WebSocket API, you need to send an unsubscribe message with the following format:
{"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.