8th July 2025
36 min read
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.
Hyperliquid offers fast, API-based access to decentralized perpetual trading. Developers commonly use it to:
Its design supports both simple scripts and complex trading systems with low latency.
This guide is for developers who want to:
If you're familiar with JavaScript and basic API workflows, you'll find this walkthrough easy to follow.
import * as hl from '@nktkas/hyperliquid';
import { ethers } from 'ethers';
const privateKey = 'YOUR_PRIVATE_KEY';
// Set up a connection to the Hyperliquid API (using the testnet environment)
const transport = new hl.HttpTransport({ isTestnet: true });
// Initialize a wallet instance using the provided private key to sign transactions
const wallet = new ethers.Wallet(privateKey);
// Set up the ExchangeClient for placing orders and handling trading operations
const exchangeClient = new hl.ExchangeClient({
wallet,
transport,
isTestnet: true,
});
// Set up the InfoClient to retrieve market data and other relevant information
const infoClient = new hl.InfoClient({ transport });
HttpTransport : Establishes a connection to the Hyperliquid API. The isTestnet flag ensures you're working with the testnet environment.
ethers.Wallet : Creates a wallet instance using your private key. This wallet is used for signing transactions.
ExchangeClient : Manages order placements and various trading operations.
InfoClient : Provides access to market metadata and informational endpoints.
Also, Explore | Creating a Custom Hyperliquid Dashboard: Track Live Holdings in React
As highlighted in the official Hyperliquid API documentation, integrating with their testnet environment is crucial for ensuring stable deployments. We've followed these guidelines extensively when deploying automated strategies for our clients, ensuring a smooth and risk-free integration before moving to production.
async function main() {
try {
// Retrieve market metadata for available assets and trading pairs
const [meta] = await infoClient.metaAndAssetCtxs();
// Find the index of the 'HYPE' asset within the available assets
const assetIndex = meta.universe.findIndex(
(asset) => asset.name === 'HYPE'
);
// Check if the HYPE asset exists
if (assetIndex === -1) {
throw new Error('HYPE asset not found.');
}
// Define order parameters
const orderParams = {
orders: [
{
a: assetIndex, // Asset index for HYPE
b: false, // false for sell, true for buy
p: '100', // Limit price (string format)
s: '0.1', // Order size (string format)
r: false, // No reduce-only flag
t: {
limit: {
tif: 'Gtc', // Time-in-force: 'Good 'til canceled'
},
},
},
],
grouping: 'na', // No specific grouping for the order
};
// Place the order on the Hyperliquid exchange
const response = await exchangeClient.order(orderParams);
console.log('Order placed successfully:', response);
} catch (error) {
// Error handling: Log any issues that occur during the order placement process
console.error('Error placing order:', error);
console.error(
'Error details:',
error.response ? error.response.response.data : error.message
);
}
}
Error Handling:
Any errors encountered during the order process are caught, logged, and detailed error messages are provided for debugging.
You may also like | How to Fetch Transaction History on Ethereum using Web3.py
Test in a Sandbox Environment:
Use a sandbox or testnet environment to simulate real-world scenarios and detect issues early.
Ensure that the wallet address derived from your private key has been initialized on Hyperliquid.
Without proper initialization, you may encounter the following error:
User or API Wallet does not exist.
Collateral is required, even for spot trades.
Hyperliquid mandates a minimum amount of USDC collateral in your perpetuals account.
For example, to sell 0.1 HYPE at $100, you'll need at least $10 USDC as collateral.
The script is configured for testnet (isTestnet: true).
Ensure that you're interacting with the correct network based on your environment"”testnet for testing and mainnet for live transactions.
Pay close attention to error messages returned by the API.
These messages offer valuable insights into issues such as insufficient margin or incorrect asset indices.
Also, Discover | Stablecoin Development with CDP (Collateralized Debt Positions)
This guide outlines a simple and efficient approach for placing limit sell orders on the Hyperliquid testnet using the REST API in Node.js. By utilizing the @nktkas/hyperliquid SDK and ethers.js for wallet management, you can easily integrate order placement into your algorithmic strategies or trading bots.
However, it's important to keep in mind a few key considerations:
With this setup, you'll be able to execute trades efficiently, taking full advantage of Hyperliquid's low-latency and high-performance decentralized exchange.
By integrating Hyperliquid API, we enabled our clients to benefit from low-latency trading and efficient order management. This has helped them streamline their trading operations, optimize their strategies, and ultimately save time. We continue to recommend Hyperliquid API for clients seeking high-performance decentralized trading solutions.
If you are planning to launch your own DEX like Hyperliquid, connect with our skilled blockchain developers to get started.