facebook

A Dev Guide to Placing Orders using Hyperliquid API

Posted By : Mohd

Jul 08, 2025

Hyperliquid is a high-performance decentralized perpetual exchange offering low-latency trading. For developers building algorithmic strategies, bots, or integrations, Hyperliquid exposes both REST and WebSocket APIs. This guide provides a step-by-step walk-through of how to place a basic order using the REST API in Node.js.

 

This script demonstrates how to place a limit sell order for the HYPE token on the Hyperliquid testnet using JavaScript. It utilizes the ethers library for wallet management and the @nktkas/hyperliquid SDK to interact with Hyperliquid's API.

 

Prerequisites:

 

  • Node.js and npm
  • ethers and @nktkas/hyperliquid npm packages

 

Imports and Setup:

 

import * as hl from '@nktkas/hyperliquid';
import { ethers } from 'ethers';

const privateKey = 'YOUR_PRIVATE_KEY';

const transport = new hl.HttpTransport({ isTestnet: true });
const wallet = new ethers.Wallet(privateKey);
const exchangeClient = new hl.ExchangeClient({
 wallet,
 transport,
 isTestnet: true,
});
const infoClient = new hl.InfoClient({ transport });

 

HttpTransport: Establishes a connection to the Hyperliquid API. The isTestnet flag ensures you're connected to the testnet environment.

 

ethers.Wallet: Creates a wallet instance using your private key. This wallet is used to sign transactions.

 

ExchangeClient: Handles order placements and other trading operations.

 

InfoClient: Provides access to market metadata and other informational endpoints.

 

Also, Explore | Creating a Custom Hyperliquid Dashboard: Track Live Holdings in React

 

Main function:

 

async function main() {
 try {
   const [meta] = await infoClient.metaAndAssetCtxs();
   const assetIndex = meta.universe.findIndex(
     (asset) => asset.name === 'HYPE'
   );
   if (assetIndex === -1) {
     throw new Error('BTC asset not found.');
   }
   const orderParams = {
     orders: [
       {
         a: assetIndex,
         b: false, // true for buy, false for sell
         p: '100', // Limit price as a string
         s: '0.1', // Order size as a string
         r: false, // Reduce-only flag
         t: {
           limit: {
             tif: 'Gtc', // Time-in-force: Good 'til canceled
           },
         },
       },
     ],
     grouping: 'na', // No grouping
   };
   const response = await exchangeClient.order(orderParams);
   console.log('Order placed successfully:', response);
 } catch (error) {
   console.error('Error placing order:', error);
   console.error(
     'Error details:',
     error.response ? error.response.response.data : error.message
   );
 }
}

 

  • Retrieve Market Metadata: metaAndAssetCtxs() fetches metadata about available trading pairs.
  • Find Asset Index: Searches for the index of the HYPE token within the available assets. This index is required to specify the asset in the order parameters.
  • Order Parameters (orderParams):
    • a: Asset index for HYPE.
    • b: Boolean indicating order side; false for sell.
    • p: Limit price set to $100.
    • s: Order size of 0.1 HYPE.
    • r: false, indicating it's not a reduce-only order.
    • t: Specifies a limit order with a time-in-force of 'Good 'til canceled' (Gtc).
    • grouping: Set to 'na', indicating no specific grouping for the order.
  • Place Order: exchangeClient.order(orderParams) sends the order to the Hyperliquid exchange.
  • Error Handling: Catches and logs any errors that occur during the order placement process.

 

You may also like | How to Fetch Transaction History on Ethereum using Web3.py

 

Important considerations:

 

  • Account Initialization: Ensure that the wallet address derived from your private key has been initialized on Hyperliquid. Without this, you'll encounter the error:

 

User or API Wallet does not exist.

 

  • Collateral Requirements: Even for spot trades, Hyperliquid mandates a minimum amount of USDC collateral in your perps account. For example, selling 0.1 HYPE at $100 requires at least $10 USDC.
  • Testnet vs. Mainnet: The script is configured for the testnet (isTestnet: true). Ensure you're interacting with the intended network.
  • Error Messages: Pay close attention to error messages returned by the API. They provide valuable insights into issues like insufficient margin or incorrect asset indices.

 

Also, Discover | Stablecoin Development with CDP (Collateralized Debt Positions)
 

Conclusion

 

In conclusion, this guide provides a straightforward approach to placing limit sell orders on the Hyperliquid testnet using the REST API in Node.js. By leveraging the @nktkas/hyperliquid SDK and ethers.js for wallet management, you can seamlessly integrate order placement into your algorithmic strategies or trading bots. However, remember to pay attention to essential considerations like wallet initialization, collateral requirements, and the network you're interacting with. With this setup, you're equipped to execute trades efficiently while building on Hyperliquid's low-latency, high-performance decentralized exchange.

 

If you are planning to launch your own DEX like Hyperliquid, connect with our skilled blockchain developers to get started. 

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

July 9, 2025 at 12:17 pm

Your comment is awaiting moderation.

bg bg

What's Trending in Tech

bg

Our Offices

India

INDIA

DG-18-009, Tower B,
Emaar Digital Greens, Sector 61,
Gurugram, Haryana
122011.
Unit- 117-120, First Floor,
Welldone Tech Park,
Sector 48, Sohna road,
Gurugram, Haryana
122018.
USA

USA

30N, Gloud St STR E, Sheridan, Wyoming (USA) - 82801
Singapore

SINGAPORE

10 Anson Road, #13-09, International Plaza Singapore 079903.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.