Hyperliquid JavaScript SDK example: rest-trailing-stop.ts

Hyperliquid REST trailing stop JavaScript example. Read the source code, review its SDK calls, and browse related exchange integration examples.

We use environment variables in our examples for API keys. It is your responsibility to manage and secure your keys appropriately.

examples/Hyperliquid/Rest/rest-trailing-stop.ts

What this example covers

  • Hyperliquid REST API JavaScript example.
  • Uses the Siebly Hyperliquid SDK package @siebly/hyperliquid-api instead of hand-written HTTP request plumbing.
  • Source path: Hyperliquid/Rest/rest-trailing-stop.ts.
  • Imports SDK symbols including RestClient.
  • Calls SDK methods such as resolveAsset(), submitTrailingStop().

How to use this example

  • Start here for the specific request or stream pattern, then check the matching SDK guide for install, credentials, and operational notes.
  • Open the repository source when you need the latest committed version: GitHub source file.
import { RestClient } from '@siebly/hyperliquid-api'; // or// import { RestClient } from '@siebly/hyperliquid-api'; /** * Place a reduce-only trailing stop for an existing ETH long on testnet. * Trailing stops are available for perpetuals only. * * Unix: * HYPERLIQUID_PRIVATE_KEY='<testnet-private-key>' npx tsx examples/Rest/rest-trailing-stop.ts */const privateKey = process.env.HYPERLIQUID_PRIVATE_KEY;if (!privateKey) {  throw new Error('Missing HYPERLIQUID_PRIVATE_KEY.');} const client = new RestClient({  privateKey,  testnet: true,}); async function main() {  try {    const asset = await client.resolveAsset('ETH');    const result = await client.submitTrailingStop({      asset: asset.assetId,      isBuy: false,      sz: '0.01',      reduceOnly: true,      retracement: { pct: '1.234%' },      // For a fixed price distance, use retracement: { px: '100' }.      activationPx: null,    });    console.log('trailing stop:', JSON.stringify(result, null, 2));  } catch (e) {    console.error('request failed: ', e);  }} main(); 

Subscribe on Substack

Complete the Substack form below to join our newsletter. Substack handles all subscriber data directly.