---
title: "Hyperliquid REST Leverage JavaScript SDK Example | Siebly"
description: "Hyperliquid REST leverage JavaScript example for the Siebly Hyperliquid SDK, covering exchange REST API and WebSocket integration, setup, and production SDK docs."
canonical: "https://siebly.io/examples/Hyperliquid/Rest/rest-leverage"
robots: "index,follow"
---

# Hyperliquid REST Leverage JavaScript SDK Example

Hyperliquid REST leverage JavaScript example for the Siebly Hyperliquid SDK, covering exchange REST API and WebSocket integration, setup, and production SDK docs.

**Path:** [Home](/) / [Examples](/examples) / [Hyperliquid](/examples/Hyperliquid) / [Rest](/examples/Hyperliquid/Rest) / [rest-leverage.ts](/examples/Hyperliquid/Rest/rest-leverage)

[View source on GitHub](https://github.com/sieblyio/hyperliquid-api/blob/main/examples/Rest/rest-leverage.ts)

[Open the Hyperliquid JavaScript SDK guide](/sdk/hyperliquid/javascript)

## rest-leverage.ts

```typescript
import { RestClient } from '@siebly/hyperliquid-api';

// or
// import { RestClient } from '@siebly/hyperliquid-api';

/**
 * Read ETH leverage, then set cross and isolated leverage.
 *
 * Unix:
 * HYPERLIQUID_PRIVATE_KEY='0xabc' npx tsx examples/Rest/rest-leverage.ts
 */
const privateKey = process.env.HYPERLIQUID_PRIVATE_KEY;
if (!privateKey) {
  throw new Error(
    "Missing HYPERLIQUID_PRIVATE_KEY. Unix example: HYPERLIQUID_PRIVATE_KEY='0xabc' npx tsx examples/Rest/rest-leverage.ts",
  );
}

const client = new RestClient({
  privateKey,
  // testnet: true,
});

async function main() {
  try {
    const summary = await client.getPerpAccountSummary();
    for (const entry of summary.assetPositions) {
      if (entry.position.coin === 'ETH') {
        console.log('ETH leverage:', entry.position.leverage);
      }
    }

    console.log(
      'cross 21x:',
      await client.updateLeverage({
        asset: 'ETH',
        isCross: true,
        leverage: 21,
      }),
    );
    console.log(
      'isolated 21x:',
      await client.updateLeverage({
        asset: 'ETH',
        isCross: false,
        leverage: 21,
      }),
    );
    try {
      console.log(
        'isolated margin top-up:',
        await client.updateIsolatedMargin({
          asset: 'ETH',
          isBuy: true,
          ntli: 1,
        }),
      );
    } catch (e) {
      console.log(
        'isolated margin top-up skipped (needs an ETH position):',
        e instanceof Error ? e.message : e,
      );
    }
  } catch (e) {
    console.error('request failed: ', e);
  }
}

main();
```
