### Install Hyperliquid SDK with bun Source: https://github.com/nktkas/hyperliquid/blob/main/docs/README.md Use this command to install the Hyperliquid SDK using bun. ```sh bun add @nktkas/hyperliquid ``` -------------------------------- ### Install Hyperliquid SDK with deno Source: https://github.com/nktkas/hyperliquid/blob/main/docs/README.md Use this command to install the Hyperliquid SDK using deno. ```sh deno add jsr:@nktkas/hyperliquid ``` -------------------------------- ### Install Hyperliquid SDK Source: https://github.com/nktkas/hyperliquid/blob/main/README.md Choose your package manager to install the SDK. Supports npm, Deno, and Bun. ```bash npm i @nktkas/hyperliquid # npm / pnpm / yarn den add jsr:@nktkas/hyperliquid # Deno bun add @nktkas/hyperliquid # Bun ``` -------------------------------- ### Install Hyperliquid SDK with yarn Source: https://github.com/nktkas/hyperliquid/blob/main/docs/README.md Use this command to install the Hyperliquid SDK using yarn. ```sh yarn add @nktkas/hyperliquid ``` -------------------------------- ### Install Hyperliquid SDK with npm Source: https://github.com/nktkas/hyperliquid/blob/main/docs/README.md Use this command to install the Hyperliquid SDK using npm. ```sh npm i @nktkas/hyperliquid ``` -------------------------------- ### Install Hyperliquid SDK with pnpm Source: https://github.com/nktkas/hyperliquid/blob/main/docs/README.md Use this command to install the Hyperliquid SDK using pnpm. ```sh pnpm add @nktkas/hyperliquid ``` -------------------------------- ### Create and Trade with Sub-Account Source: https://github.com/nktkas/hyperliquid/blob/main/docs/guides/agent-wallets-and-vaults.md This example shows how to create a new sub-account and then place trade orders on its behalf using the `vaultAddress` mechanism. It also includes examples for transferring funds (USD and spot tokens) to the sub-account. ```typescript // Create const result = await client.createSubAccount({ name: "trading-bot" }); const subAccountAddress = result.response.data; // Trade on behalf of sub-account await client.order({ orders: [/* ... */], grouping: "na" }, { vaultAddress: subAccountAddress, }); // Transfer funds await client.subAccountTransfer({ subAccountUser: subAccountAddress, isDeposit: true, usd: 10e6, // 10 USD in microunits }); // Transfer spot tokens await client.subAccountSpotTransfer({ subAccountUser: subAccountAddress, isDeposit: true, token: "USDC:0xeb62eee3685fc4c43992febcd9e75443", amount: "100", // 100 USDC }); ``` -------------------------------- ### Quick start with ExplorerClient Source: https://github.com/nktkas/hyperliquid/blob/main/docs/README.md Initialize an ExplorerClient to look up blocks, transactions, and addresses. Requires an HttpTransport. ```ts import { ExplorerClient, HttpTransport } from "@nktkas/hyperliquid"; const transport = new HttpTransport(); const client = new ExplorerClient({ transport }); const block = await client.blockDetails({ height: 123 }); ``` -------------------------------- ### Quick start with SubscriptionClient Source: https://github.com/nktkas/hyperliquid/blob/main/docs/README.md Initialize a SubscriptionClient to receive real-time updates via WebSocket. Requires a WebSocketTransport. ```ts import { SubscriptionClient, WebSocketTransport } from "@nktkas/hyperliquid"; const transport = new WebSocketTransport(); const client = new SubscriptionClient({ transport }); await client.allMids((data) => { console.log(data.mids); }); ``` -------------------------------- ### Quick start with ExchangeClient Source: https://github.com/nktkas/hyperliquid/blob/main/docs/README.md Initialize an ExchangeClient to place orders, transfer funds, and manage accounts. Requires an HttpTransport and a wallet. ```ts import { ExchangeClient, HttpTransport } from "@nktkas/hyperliquid"; import { privateKeyToAccount } from "viem/accounts"; const wallet = privateKeyToAccount("0x..."); const transport = new HttpTransport(); const client = new ExchangeClient({ transport, wallet }); await client.order({ orders: [{ a: 0, b: true, p: "50000", s: "0.01", r: false, t: { limit: { tif: "Gtc" } }, }], grouping: "na", }); ``` -------------------------------- ### Install Hyperliquid SDK and polyfill for React Native Source: https://github.com/nktkas/hyperliquid/blob/main/docs/README.md Install the Hyperliquid SDK and the 'event-target-shim' package for React Native. Hermes lacks global EventTarget and Event, so polyfill them before importing the SDK. ```sh npm i @nktkas/hyperliquid event-target-shim ``` ```ts import { Event, EventTarget } from "event-target-shim"; if (!globalThis.EventTarget) globalThis.EventTarget = EventTarget; if (!globalThis.Event) globalThis.Event = Event; ``` -------------------------------- ### Quick start with InfoClient Source: https://github.com/nktkas/hyperliquid/blob/main/docs/README.md Initialize an InfoClient to read market data, account state, and order book. Requires an HttpTransport. ```ts import { HttpTransport, InfoClient } from "@nktkas/hyperliquid"; const transport = new HttpTransport(); const client = new InfoClient({ transport }); const mids = await client.allMids(); ``` -------------------------------- ### Connect with viem and MetaMask Source: https://github.com/nktkas/hyperliquid/blob/main/docs/guides/browser-wallets.md Connect to a browser wallet using viem and MetaMask. Requires `window.ethereum` to be available. This setup uses a JSON-RPC account. ```typescript import { ExchangeClient, HttpTransport } from "@nktkas/hyperliquid"; import { createWalletClient, custom } from "viem"; import { arbitrum } from "viem/chains"; const [account] = await window.ethereum!.request({ method: "eth_requestAccounts" }) as `0x${string}`[]; const wallet = createWalletClient({ account, chain: arbitrum, transport: custom(window.ethereum!) }); const transport = new HttpTransport(); const client = new ExchangeClient({ transport, wallet }); ``` -------------------------------- ### Get All Mids Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Retrieves all mid-prices. This configuration uses the OpenAPI builtin type. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-allMids ``` -------------------------------- ### Sign L1 Action with ethers Source: https://github.com/nktkas/hyperliquid/blob/main/docs/signing.md This example shows how to sign an L1 action using the ethers library with a private key wallet. Import `signL1Action` from `@nktkas/hyperliquid/signing` and `Wallet` from `ethers`. ```typescript import { signL1Action } from "@nktkas/hyperliquid/signing"; import { Wallet } from "ethers"; const wallet = new Wallet("0x..."); const action = { type: "cancel", cancels: [{ a: 0, o: 12345 }] }; const nonce = Date.now(); const signature = await signL1Action({ wallet, action, nonce }); await fetch("https://api.hyperliquid.xyz/exchange", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ action, signature, nonce }), }); ``` -------------------------------- ### Connect with ethers.js and BrowserProvider Source: https://github.com/nktkas/hyperliquid/blob/main/docs/guides/browser-wallets.md Connect to a browser wallet using ethers.js and BrowserProvider. Requires `window.ethereum` to be available. This setup uses a BrowserProvider. ```typescript import { ExchangeClient, HttpTransport } from "@nktkas/hyperliquid"; import { BrowserProvider } from "ethers"; const provider = new BrowserProvider(window.ethereum!); const wallet = await provider.getSigner(); const transport = new HttpTransport(); const client = new ExchangeClient({ transport, wallet }); ``` -------------------------------- ### Sign Multi-Sig User Signed in Browser with Viem Source: https://github.com/nktkas/hyperliquid/blob/main/docs/signing.md This example shows how to sign a multi-sig action within a browser environment using viem and the injected Ethereum provider. It requests user accounts and sets up the viem client. ```typescript import { signMultiSigUserSigned } from "@nktkas/hyperliquid/signing"; import { UsdSendTypes } from "@nktkas/hyperliquid/api/exchange"; import { createWalletClient, custom } from "viem"; import { arbitrum } from "viem/chains"; const [account] = await window.ethereum!.request({ method: "eth_requestAccounts" }) as `0x${string}`[]; const leader = createWalletClient({ account, chain: arbitrum, transport: custom(window.ethereum!), }); const multiSigUser = "0x..."; // the multi-sig account address const signers = [leader /* additional signers */] as const; const action = { type: "usdSend", signatureChainId: "0x66eee" as const, hyperliquidChain: "Mainnet" as const, // or "Testnet" destination: "0x...", amount: "100", time: Date.now(), }; const { action: multiSigAction, signature } = await signMultiSigUserSigned({ signers, multiSigUser, action, types: UsdSendTypes, }); await fetch("https://api.hyperliquid.xyz/exchange", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ action: multiSigAction, signature, nonce: action.time }), }); ``` -------------------------------- ### Importing Info Methods Directly Source: https://github.com/nktkas/hyperliquid/blob/main/docs/guides/tree-shaking.md Import individual info methods like `allMids` directly from the SDK. This example shows how to use `allMids` with an `HttpTransport` configuration. ```typescript import { HttpTransport } from "@nktkas/hyperliquid"; import { allMids } from "@nktkas/hyperliquid/api/info"; const transport = new HttpTransport(); const result = await allMids({ transport }); ``` -------------------------------- ### Sign User-Signed Action with Browser (ethers) Source: https://github.com/nktkas/hyperliquid/blob/main/docs/signing.md This example shows how to sign an 'approveAgent' action using ethers in a browser. It utilizes `BrowserProvider` and `getSigner` to interact with `window.ethereum`. ```typescript import { signUserSignedAction } from "@nktkas/hyperliquid/signing"; import { ApproveAgentTypes } from "@nktkas/hyperliquid/api/exchange"; import { BrowserProvider } from "ethers"; const provider = new BrowserProvider(window.ethereum!); const wallet = await provider.getSigner(); const action = { type: "approveAgent", signatureChainId: "0x66eee" as const, hyperliquidChain: "Mainnet", // or "Testnet" agentAddress: "0x...", agentName: "Agent", nonce: Date.now(), }; const signature = await signUserSignedAction({ wallet, action, types: ApproveAgentTypes }); await fetch("https://api.hyperliquid.xyz/exchange", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ action, signature, nonce: action.nonce }), }); ``` -------------------------------- ### Importing Explorer Methods Directly Source: https://github.com/nktkas/hyperliquid/blob/main/docs/guides/tree-shaking.md Import individual explorer methods like `blockDetails` directly from the SDK. This example shows how to use `blockDetails` with an `HttpTransport` and specific block height. ```typescript import { HttpTransport } from "@nktkas/hyperliquid"; import { blockDetails } from "@nktkas/hyperliquid/api/explorer"; const transport = new HttpTransport(); const block = await blockDetails({ transport }, { height: 123 }); ``` -------------------------------- ### Importing Exchange Methods Directly Source: https://github.com/nktkas/hyperliquid/blob/main/docs/guides/tree-shaking.md Import individual exchange methods like `order` directly from the SDK. This example demonstrates using `order` with `HttpTransport` and a wallet configured via `viem`. ```typescript import { HttpTransport } from "@nktkas/hyperliquid"; import { order } from "@nktkas/hyperliquid/api/exchange"; import { privateKeyToAccount } from "viem/accounts"; const transport = new HttpTransport(); const wallet = privateKeyToAccount("0x..."); await order( { transport, wallet }, { orders: [{ a: 0, b: true, p: "50000", s: "0.01", r: false, t: { limit: { tif: "Gtc" } }, }], grouping: "na", }, ); ``` -------------------------------- ### Get Delegator Summary Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Retrieves a summary for a delegator. This configuration uses the OpenAPI builtin type. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-delegatorSummary ``` -------------------------------- ### Get Delegations Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Retrieves delegation information. This configuration uses the OpenAPI builtin type. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-delegations ``` -------------------------------- ### Execute Trades with ExchangeClient Source: https://github.com/nktkas/hyperliquid/blob/main/README.md Use the ExchangeClient for trading operations. Requires wallet setup using viem's privateKeyToAccount. Place orders, update leverage, or initiate withdrawals. ```typescript // 1. Import modules import { ExchangeClient, HttpTransport } from "@nktkas/hyperliquid"; import { privateKeyToAccount } from "viem/accounts"; // 2. Set up client with wallet and transport const wallet = privateKeyToAccount("0x..."); const transport = new HttpTransport(); const exchange = new ExchangeClient({ transport, wallet }); // 3. Execute an action // Place an order const result = await exchange.order({ orders: [{ a: 0, b: true, p: "95000", s: "0.01", r: false, t: { limit: { tif: "Gtc" } }, }], grouping: "na", }); // Update leverage await exchange.updateLeverage({ asset: 0, isCross: true, leverage: 5 }); // Initiate a withdrawal request await exchange.withdraw3({ destination: "0x...", amount: "1" }); ``` -------------------------------- ### Importing Subscription Methods Directly Source: https://github.com/nktkas/hyperliquid/blob/main/docs/guides/tree-shaking.md Import individual subscription methods like `allMids` directly from the SDK. This example uses `WebSocketTransport` and includes a callback to process incoming data. ```typescript import { WebSocketTransport } from "@nktkas/hyperliquid"; import { allMids } from "@nktkas/hyperliquid/api/subscription"; const transport = new WebSocketTransport(); const subscription = await allMids({ transport }, (data) => { console.log(data.mids); }); ``` -------------------------------- ### Sign Multi-Sig L1 Action with Ethers Source: https://github.com/nktkas/hyperliquid/blob/main/docs/signing.md This example demonstrates signing a multi-sig L1 action using the ethers.js library. Ensure you have defined the multi-sig user, an array of ethers Wallets as signers (with the first being the leader), the action, and a nonce. ```typescript import { signMultiSigL1 } from "@nktkas/hyperliquid/signing"; import { Wallet } from "ethers"; const multiSigUser = "0x..."; // the multi-sig account address const signers = [ new Wallet("0x..."), // leader new Wallet("0x..."), ] as const; const action = { type: "scheduleCancel", time: Date.now() + 10_000 }; const nonce = Date.now(); const { action: multiSigAction, signature } = await signMultiSigL1({ signers, multiSigUser, signatureChainId: "0x66eee", action, nonce, }); await fetch("https://api.hyperliquid.xyz/exchange", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ action: multiSigAction, signature, nonce }), }); ``` -------------------------------- ### Get All Perp Metas Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Retrieves metadata for all perpetual futures. This configuration uses the OpenAPI builtin type. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-allPerpMetas ``` -------------------------------- ### Sign Multi-Sig L1 Action in Browser with Ethers Source: https://github.com/nktkas/hyperliquid/blob/main/docs/signing.md This example demonstrates signing a multi-sig L1 action in a browser using ethers.js and the injected Ethereum provider. It obtains a signer from the provider and includes it in the list of signers for the multi-sig operation. ```typescript import { signMultiSigL1 } from "@nktkas/hyperliquid/signing"; import { BrowserProvider } from "ethers"; const provider = new BrowserProvider(window.ethereum!); const leader = await provider.getSigner(); const multiSigUser = "0x..."; // the multi-sig account address const signers = [leader /* additional signers */] as const; const action = { type: "scheduleCancel", time: Date.now() + 10_000 }; const nonce = Date.now(); const { action: multiSigAction, signature } = await signMultiSigL1({ signers, multiSigUser, signatureChainId: "0x66eee", action, nonce, }); await fetch("https://api.hyperliquid.xyz/exchange", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ action: multiSigAction, signature, nonce }), }); ``` -------------------------------- ### Get Frontend Open Orders Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Retrieves open orders visible to the frontend. This configuration uses the OpenAPI builtin type. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-frontendOpenOrders ``` -------------------------------- ### OpenAPI Specification for Token Details Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Configures access to the OpenAPI specification for token details. This setup excludes models and download links. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-tokenDetails ``` -------------------------------- ### OpenAPI Specification for User to Multi-Sig Signers Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Configures a dependency on the OpenAPI specification for mapping user accounts to multi-signature signers. This setup excludes models and download links. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-userToMultiSigSigners ``` -------------------------------- ### OpenAPI Specification for User Fills by Time Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Configures access to the OpenAPI specification for user fills filtered by time. This setup excludes models and download links. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-userFillsByTime ``` -------------------------------- ### Initialize ExchangeClient with Viem Wallet Source: https://github.com/nktkas/hyperliquid/blob/main/docs/clients.md Shows how to set up an ExchangeClient using a viem wallet for signing. This client requires a wallet and works with any transport. ```typescript import { ExchangeClient, HttpTransport } from "@nktkas/hyperliquid"; import { privateKeyToAccount } from "viem/accounts"; const wallet = privateKeyToAccount("0x..."); const transport = new HttpTransport(); const client = new ExchangeClient({ transport, wallet }); await client.order({ orders: [/* ... */], grouping: "na" }); ``` -------------------------------- ### Sign USD Send Multi-Signature Transaction Source: https://github.com/nktkas/hyperliquid/blob/main/docs/signing.md Example of signing a USD send multi-signature transaction and submitting it to the Hyperliquid API. Ensure the `signers` array includes all necessary participants and the `action` object is correctly populated. ```typescript const signers = [leader /* additional signers */] as const; const action = { type: "usdSend", signatureChainId: "0x66eee" as const, hyperliquidChain: "Mainnet" as const, // or "Testnet" destination: "0x...", amount: "100", time: Date.now(), }; const { action: multiSigAction, signature } = await signMultiSigUserSigned({ signers, multiSigUser, action, types: UsdSendTypes, }); await fetch("https://api.hyperliquid.xyz/exchange", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ action: multiSigAction, signature, nonce: action.time }), }); ``` -------------------------------- ### Get Wallet Address and Chain ID Source: https://github.com/nktkas/hyperliquid/blob/main/docs/signing.md Use these helper functions to retrieve the wallet address and chain ID. They work with any supported wallet type, including viem accounts. ```typescript import { getWalletAddress, getWalletChainId } from "@nktkas/hyperliquid/signing"; import { privateKeyToAccount } from "viem/accounts"; const wallet = privateKeyToAccount("0x..."); const address = await getWalletAddress(wallet); // "0x..." const chainId = await getWalletChainId(wallet); // "0xa4b1" ``` -------------------------------- ### Custom Signer for Multi-Sig User Signed Source: https://github.com/nktkas/hyperliquid/blob/main/docs/signing.md Example of a custom signer implementation for the signMultiSigUserSigned function. This allows for custom signing logic, such as using a hardware wallet or a specific signing service. ```typescript import { signMultiSigUserSigned } from "@nktkas/hyperliquid/signing"; import { UsdSendTypes } from "@nktkas/hyperliquid/api/exchange"; import type { AbstractViemLocalAccount } from "@nktkas/hyperliquid/signing"; const leader: AbstractViemLocalAccount = { address: "0x...", async signTypedData({ domain, types, primaryType, message }) { return "0x..."; }, }; const multiSigUser = "0x..."; // the multi-sig account address ``` -------------------------------- ### Get Funding History Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Retrieves the funding history. This configuration uses the OpenAPI builtin type. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-fundingHistory ``` -------------------------------- ### Initialize ExchangeClient with Browser Viem Wallet Source: https://github.com/nktkas/hyperliquid/blob/main/docs/clients.md Demonstrates initializing an ExchangeClient for browser environments using viem and window.ethereum. Requires user account access. ```typescript import { ExchangeClient, HttpTransport } from "@nktkas/hyperliquid"; import { createWalletClient, custom } from "viem"; import { arbitrum } from "viem/chains"; const [account] = await window.ethereum!.request({ method: "eth_requestAccounts" }) as `0x${string}`[]; const wallet = createWalletClient({ account, chain: arbitrum, transport: custom(window.ethereum!) }); const transport = new HttpTransport(); const client = new ExchangeClient({ transport, wallet }); await client.order({ orders: [/* ... */], grouping: "na" }); ``` -------------------------------- ### Get Delegator Rewards Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Retrieves rewards for a delegator. This configuration uses the OpenAPI builtin type. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-delegatorRewards ``` -------------------------------- ### Initialize ExchangeClient with Browser Ethers Wallet Source: https://github.com/nktkas/hyperliquid/blob/main/docs/clients.md Shows how to set up an ExchangeClient in a browser using ethers.js and window.ethereum. Obtains a signer from the provided provider. ```typescript import { ExchangeClient, HttpTransport } from "@nktkas/hyperliquid"; import { BrowserProvider } from "ethers"; const provider = new BrowserProvider(window.ethereum!); const wallet = await provider.getSigner(); const transport = new HttpTransport(); const client = new ExchangeClient({ transport, wallet }); await client.order({ orders: [/* ... */], grouping: "na" }); ``` -------------------------------- ### Get Delegator History Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Retrieves the history of a delegator. This configuration uses the OpenAPI builtin type. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-delegatorHistory ``` -------------------------------- ### Run All Tests Source: https://github.com/nktkas/hyperliquid/blob/main/CONTRIBUTING.md Execute all tests for the SDK. Ensure your development environment is set up correctly before running. ```bash deno test -A ``` -------------------------------- ### Get Historical Orders Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Retrieves historical order data. This configuration uses the OpenAPI builtin type. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-historicalOrders ``` -------------------------------- ### Get Extra Agents Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Retrieves information about extra agents. This configuration uses the OpenAPI builtin type. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-extraAgents ``` -------------------------------- ### Initialize ExchangeClient with Ethers Wallet Source: https://github.com/nktkas/hyperliquid/blob/main/docs/clients.md Illustrates setting up an ExchangeClient with an ethers.js Wallet. This client requires a wallet and is compatible with any transport. ```typescript import { ExchangeClient, HttpTransport } from "@nktkas/hyperliquid"; import { Wallet } from "ethers"; const wallet = new Wallet("0x..."); const transport = new HttpTransport(); const client = new ExchangeClient({ transport, wallet }); await client.order({ orders: [/* ... */], grouping: "na" }); ``` -------------------------------- ### Get Exchange Status Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Retrieves the current status of the exchange. This configuration uses the OpenAPI builtin type. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-exchangeStatus ``` -------------------------------- ### Get Clearinghouse State Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Retrieves the current state of the clearinghouse. This configuration uses the OpenAPI builtin type. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-clearinghouseState ``` -------------------------------- ### Read Data with InfoClient Source: https://github.com/nktkas/hyperliquid/blob/main/README.md Set up an InfoClient to query Hyperliquid data. Import necessary modules, initialize the transport and client, then make calls to retrieve mids, open orders, or L2 book snapshots. ```typescript // 1. Import module import { HttpTransport, InfoClient } from "@nktkas/hyperliquid"; // 2. Set up client with transport const transport = new HttpTransport(); const info = new InfoClient({ transport }); // 3. Query data // Retrieve mids for all coins const mids = await info.allMids(); // Retrieve a user's open orders const openOrders = await info.openOrders({ user: "0x..." }); // L2 book snapshot const book = await info.l2Book({ coin: "BTC" }); ``` -------------------------------- ### Get Candle Snapshot Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Retrieves a snapshot of candle data. This configuration uses the OpenAPI builtin type. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-candleSnapshot ``` -------------------------------- ### Initialize ExchangeClient with Custom Wallet Interface Source: https://github.com/nktkas/hyperliquid/blob/main/docs/clients.md Illustrates initializing an ExchangeClient with a custom wallet object that meets the required interface, including signTypedData and address. Useful for custom signing logic. ```typescript import { ExchangeClient, HttpTransport } from "@nktkas/hyperliquid"; import type { AbstractViemLocalAccount } from "@nktkas/hyperliquid/signing"; const wallet: AbstractViemLocalAccount = { address: "0x...", async signTypedData({ domain, types, primaryType, message }) { // Your EIP-712 signing logic (HSM, MPC, remote signer, etc.) return "0x..."; }, }; const transport = new HttpTransport(); const client = new ExchangeClient({ transport, wallet }); await client.order({ orders: [/* ... */], grouping: "na" }); ``` -------------------------------- ### Get Approved Builders Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Retrieves a list of approved builders. This configuration uses the OpenAPI builtin type. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-approvedBuilders ``` -------------------------------- ### Initialize Exchange and Info Clients with HttpTransport Source: https://github.com/nktkas/hyperliquid/blob/main/docs/transports.md Shows how to set up InfoClient and ExchangeClient using HttpTransport. A single transport instance can be reused across multiple clients for efficiency. ```typescript import { ExchangeClient, HttpTransport, InfoClient } from "@nktkas/hyperliquid"; import { privateKeyToAccount } from "viem/accounts"; const wallet = privateKeyToAccount("0x..."); const transport = new HttpTransport(); const info = new InfoClient({ transport }); const exchange = new ExchangeClient({ wallet, transport }); // ^^^^^^^^^ // A single transport instance can be reused with any client const mids = await info.allMids(); ``` -------------------------------- ### Initialize ExplorerClient with WebSocketTransport Source: https://github.com/nktkas/hyperliquid/blob/main/docs/clients.md Shows how to initialize the ExplorerClient using a WebSocketTransport for real-time updates from the RPC WebSocket URL. Requires importing ExplorerClient and WebSocketTransport. ```typescript import { ExplorerClient, WebSocketTransport } from "@nktkas/hyperliquid"; const transport = new WebSocketTransport({ url: "wss://rpc.hyperliquid.xyz/ws" }); const client = new ExplorerClient({ transport }); const sub = await client.explorerBlock((data) => { console.log(data); }); ``` -------------------------------- ### Get L2 Book Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Retrieves the Level 2 order book. This configuration uses the OpenAPI builtin type. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-l2Book ``` -------------------------------- ### Get Active Asset Data Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Retrieves data for active assets. This configuration uses the OpenAPI builtin type. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-activeAssetData ``` -------------------------------- ### Get Gossip Root IPs Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Retrieves the root IP addresses for gossip. This configuration uses the OpenAPI builtin type. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-gossipRootIps ``` -------------------------------- ### Initialize Multi-sig Exchange Client Source: https://github.com/nktkas/hyperliquid/blob/main/docs/clients.md Sets up an ExchangeClient for multi-signature accounts, requiring multiple signers for transaction approval. The leader collects and submits the final transaction. ```typescript import { ExchangeClient, HttpTransport } from "@nktkas/hyperliquid"; import { privateKeyToAccount } from "viem/accounts"; const multiSigUser = "0x..."; // the multi-sig account address const signers = [ privateKeyToAccount("0x..."), // leader — signs the wrapper privateKeyToAccount("0x..."), ] as const; const transport = new HttpTransport(); const client = new ExchangeClient({ transport, signers, multiSigUser }); // Use the client as usual await client.order({ orders: [/* ... */], grouping: "na" }); ``` -------------------------------- ### Get Gossip Priority Auction Status Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Retrieves the status of the gossip priority auction. This configuration uses the OpenAPI builtin type. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-gossipPriorityAuctionStatus ``` -------------------------------- ### Get All Borrow Lend Reserve States Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Retrieves the states for all borrow and lend reserves. This configuration uses the OpenAPI builtin type. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-allBorrowLendReserveStates ``` -------------------------------- ### Get Aligned Quote Token Info Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Retrieves information about aligned quote tokens. This configuration uses the OpenAPI builtin type. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-alignedQuoteTokenInfo ``` -------------------------------- ### Initialize InfoClient with HttpTransport Source: https://github.com/nktkas/hyperliquid/blob/main/docs/clients.md Demonstrates how to initialize an InfoClient using an HttpTransport. This client is read-only and can be used with any transport. ```typescript import { HttpTransport, InfoClient } from "@nktkas/hyperliquid"; const transport = new HttpTransport(); const client = new InfoClient({ transport }); const book = await client.l2Book({ coin: "ETH" }); ``` -------------------------------- ### Sign L1 Action with viem Source: https://github.com/nktkas/hyperliquid/blob/main/docs/signing.md Use this snippet to sign an L1 action using the viem library with a private key account. Ensure you have imported the necessary functions from `@nktkas/hyperliquid/signing` and `viem/accounts`. ```typescript import { signL1Action } from "@nktkas/hyperliquid/signing"; import { privateKeyToAccount } from "viem/accounts"; const wallet = privateKeyToAccount("0x..."); const action = { type: "cancel", cancels: [{ a: 0, o: 12345 }] }; const nonce = Date.now(); const signature = await signL1Action({ wallet, action, nonce }); await fetch("https://api.hyperliquid.xyz/exchange", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ action, signature, nonce }), }); ``` -------------------------------- ### Get Borrow Lend User State Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Retrieves the borrow and lend state for a specific user. This configuration uses the OpenAPI builtin type. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-borrowLendUserState ``` -------------------------------- ### Initialize SubscriptionClient with WebSocketTransport Source: https://github.com/nktkas/hyperliquid/blob/main/docs/transports.md Shows how to initialize a SubscriptionClient using WebSocketTransport, which is required for live data streams. The promise resolves when the subscription is connected. ```typescript import { SubscriptionClient, WebSocketTransport } from "@nktkas/hyperliquid"; const transport = new WebSocketTransport(); const subs = new SubscriptionClient({ transport }); // ^^^^^^^^^^^^^^^^ // Unlike other clients, it supports only `WebSocketTransport` await subs.allMids((data) => { console.log(data.mids); }); // Promise is resolved when the subscription is connected ``` -------------------------------- ### Get Borrow Lend Reserve State Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Retrieves the state for a specific borrow and lend reserve. This configuration uses the OpenAPI builtin type. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-borrowLendReserveState ``` -------------------------------- ### OpenAPI Specification for Spot Deployment State Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md This configuration sets up a dependency on the OpenAPI specification for spot deployment state information. It specifies that models and download links are not needed. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-spotDeployState ``` -------------------------------- ### OpenAPI Subscription: User Fills Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Sets up an OpenAPI subscription for user-specific trade fills. This provides details on orders that have been successfully executed for a user. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-subscription-userFills ``` -------------------------------- ### Sign L1 Action in Browser with viem Source: https://github.com/nktkas/hyperliquid/blob/main/docs/signing.md Sign an L1 action within a browser environment using viem and the user's injected Ethereum provider (e.g., MetaMask). This requires importing `signL1Action`, `createWalletClient`, `custom`, and `arbitrum`. ```typescript import { signL1Action } from "@nktkas/hyperliquid/signing"; import { createWalletClient, custom } from "viem"; import { arbitrum } from "viem/chains"; const [account] = await window.ethereum!.request({ method: "eth_requestAccounts" }) as `0x${string}`[]; const wallet = createWalletClient({ account, chain: arbitrum, transport: custom(window.ethereum!) }); const action = { type: "cancel", cancels: [{ a: 0, o: 12345 }] }; const nonce = Date.now(); const signature = await signL1Action({ wallet, action, nonce }); await fetch("https://api.hyperliquid.xyz/exchange", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ action, signature, nonce }), }); ``` -------------------------------- ### Approve and Trade with Agent Wallet Source: https://github.com/nktkas/hyperliquid/blob/main/docs/guides/agent-wallets-and-vaults.md This snippet demonstrates how to approve an agent wallet once using a browser wallet (like MetaMask) and then perform trades using the agent's private key without further popups. It requires importing necessary modules from '@nktkas/hyperliquid' and 'viem'. ```typescript import { ExchangeClient, HttpTransport } from "@nktkas/hyperliquid"; import { createWalletClient, custom } from "viem"; import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"; import { arbitrum } from "viem/chains"; // Browser wallet (e.g., MetaMask) const [account] = await window.ethereum!.request({ method: "eth_requestAccounts" }) as `0x${string}`[]; const wallet = createWalletClient({ account, chain: arbitrum, transport: custom(window.ethereum!) }); const transport = new HttpTransport(); const client = new ExchangeClient({ transport, wallet }); // Agent — persist the key to reuse the agent const agentPrivateKey = generatePrivateKey(); const agentSigner = privateKeyToAccount(agentPrivateKey); // 1. Approve agent once (triggers browser wallet popup) await client.approveAgent({ agentAddress: agentSigner.address, agentName: "browser-agent", }); // 2. Trade with agent (no popups) const agentClient = new ExchangeClient({ transport, wallet: agentSigner }); await agentClient.order({ orders: [/* ... */], grouping: "na" }); ``` -------------------------------- ### OpenAPI Subscription: Spot Asset Contexts Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Configures an OpenAPI subscription for spot asset contexts. This provides information specific to spot market assets. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-subscription-spotAssetCtxs ``` -------------------------------- ### OpenAPI Subscription: Trades Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Sets up an OpenAPI subscription for trade data. This is crucial for monitoring executed trades in real-time. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-subscription-trades ``` -------------------------------- ### Initialize HttpTransport or WebSocketTransport Source: https://github.com/nktkas/hyperliquid/blob/main/docs/transports.md Demonstrates initializing either HttpTransport or WebSocketTransport with common options like testnet and timeout. The choice depends on whether live data streams (subscriptions) are needed. ```typescript import { HttpTransport, WebSocketTransport } from "@nktkas/hyperliquid"; const transport = new HttpTransport({ isTestnet: true, timeout: 30_000 }); // ^^^^^^^^^^^^^ // or `WebSocketTransport` ``` -------------------------------- ### Sign Multi-Sig L1 Action in Browser with Viem Source: https://github.com/nktkas/hyperliquid/blob/main/docs/signing.md This snippet shows how to sign a multi-sig L1 action within a browser environment using viem and the injected Ethereum provider. It requires requesting user accounts and setting up a viem wallet client for the leader. ```typescript import { signMultiSigL1 } from "@nktkas/hyperliquid/signing"; import { createWalletClient, custom } from "viem"; import { arbitrum } from "viem/chains"; const [account] = await window.ethereum!.request({ method: "eth_requestAccounts" }) as `0x${string}`[]; const leader = createWalletClient({ account, chain: arbitrum, transport: custom(window.ethereum!), }); const multiSigUser = "0x..."; // the multi-sig account address const signers = [leader /* additional signers */] as const; const action = { type: "scheduleCancel", time: Date.now() + 10_000 }; const nonce = Date.now(); const { action: multiSigAction, signature } = await signMultiSigL1({ signers, multiSigUser, signatureChainId: "0x66eee", action, nonce, }); await fetch("https://api.hyperliquid.xyz/exchange", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ action: multiSigAction, signature, nonce }), }); ``` -------------------------------- ### Sign User-Signed Action with Browser (viem) Source: https://github.com/nktkas/hyperliquid/blob/main/docs/signing.md Sign an 'approveAgent' action using viem within a browser environment. This requires access to `window.ethereum` and uses `createWalletClient`. ```typescript import { signUserSignedAction } from "@nktkas/hyperliquid/signing"; import { ApproveAgentTypes } from "@nktkas/hyperliquid/api/exchange"; import { createWalletClient, custom } from "viem"; import { arbitrum } from "viem/chains"; const [account] = await window.ethereum!.request({ method: "eth_requestAccounts" }) as `0x${string}`[]; const wallet = createWalletClient({ account, chain: arbitrum, transport: custom(window.ethereum!) }); const action = { type: "approveAgent", signatureChainId: "0x66eee" as const, hyperliquidChain: "Mainnet", // or "Testnet" agentAddress: "0x...", agentName: "Agent", nonce: Date.now(), }; const signature = await signUserSignedAction({ wallet, action, types: ApproveAgentTypes }); await fetch("https://api.hyperliquid.xyz/exchange", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ action, signature, nonce: action.nonce }), }); ``` -------------------------------- ### Subscription All DEXs Asset Contexts Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Configuration for subscribing to all DEXs asset contexts. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-subscription-allDexsAssetCtxs ``` -------------------------------- ### Run Code Style and Linting Checks Source: https://github.com/nktkas/hyperliquid/blob/main/CONTRIBUTING.md After making code changes, run this command to ensure adherence to style guidelines and catch potential issues. This command also checks dependencies and documentation. ```bash deno task check ``` -------------------------------- ### OpenAPI Specification for Spot Metadata and Asset Contexts Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Configures access to the OpenAPI specification for spot metadata and asset contexts. It specifies that models and download links are not required. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-info-spotMetaAndAssetCtxs ``` -------------------------------- ### OpenAPI Subscription: Best Bid/Offer Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md Configures an OpenAPI subscription for Best Bid and Offer (BBO) data. Use this to subscribe to the latest BBO updates. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-subscription-bbo ``` -------------------------------- ### OpenAPI Specification for Order Placement Source: https://github.com/nktkas/hyperliquid/blob/main/docs/SUMMARY.md This snippet configures an OpenAPI specification for placing orders on the exchange. ```yaml type: builtin:openapi props: models: false downloadLink: false dependencies: spec: ref: kind: openapi spec: hl-exchange-order ``` -------------------------------- ### Get Spot Pair ID for Info Endpoints Source: https://github.com/nktkas/hyperliquid/blob/main/docs/utilities.md Use getSpotPairId to obtain the identifier expected by Hyperliquid info endpoints and subscriptions for spot markets. This may be an '@' alias or the original 'BASE/QUOTE' format for legacy pairs. ```typescript converter.getSpotPairId("HYPE/USDC"); // "@107" converter.getSpotPairId("PURR/USDC"); // "PURR/USDC" — legacy pair ``` -------------------------------- ### Sign Multi-Sig L1 Action with Viem Source: https://github.com/nktkas/hyperliquid/blob/main/docs/signing.md Use this snippet to sign a multi-sig L1 action using the viem library. It requires defining the multi-sig user, an array of signers (where the first is the leader), the action to be signed, and a nonce. ```typescript import { signMultiSigL1 } from "@nktkas/hyperliquid/signing"; import { privateKeyToAccount } from "viem/accounts"; const multiSigUser = "0x..."; // the multi-sig account address const signers = [ privateKeyToAccount("0x..."), // leader — signs the wrapper privateKeyToAccount("0x..."), ] as const; const action = { type: "scheduleCancel", time: Date.now() + 10_000 }; const nonce = Date.now(); const { action: multiSigAction, signature } = await signMultiSigL1({ signers, multiSigUser, signatureChainId: "0x66eee", action, nonce, }); await fetch("https://api.hyperliquid.xyz/exchange", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ action: multiSigAction, signature, nonce }), }); ``` -------------------------------- ### Create SymbolConverter Instance Source: https://github.com/nktkas/hyperliquid/blob/main/docs/utilities.md Initialize SymbolConverter asynchronously using create() with a transport. This is the recommended approach for most use cases. ```typescript import { HttpTransport } from "@nktkas/hyperliquid"; import { SymbolConverter } from "@nktkas/hyperliquid/utils"; const transport = new HttpTransport(); const converter = await SymbolConverter.create({ transport }); ``` -------------------------------- ### Get Size Decimals for Formatting Source: https://github.com/nktkas/hyperliquid/blob/main/docs/utilities.md Retrieve the szDecimals for a given symbol using getSzDecimals. This value is crucial for formatting prices and sizes accurately. For spot markets, it uses the base token's szDecimals. Outcome markets default to 5. ```typescript import { formatPrice, formatSize } from "@nktkas/hyperliquid/utils"; const szDecimals = converter.getSzDecimals("BTC")!; // 5 formatPrice("97123.456789", szDecimals); // "97123" formatSize("0.00123456789", szDecimals); // "0.00123" ```