Hyperliquid is a high-performance blockchain designed specifically for decentralized derivatives trading. It offers incredibly fast transaction processing, low fees, and a fully onchain open financial system.

This guide will walk you through setting up trading on Hyperliquid using Privy’s EVM wallets, focusing on how to securely authenticate and interact with the Hyperliquid API without ever exposing private keys. The Privy SDKs are designed to be fully compatible with the Hyperliquid API, allowing you to build secure trading applications with ease.

Resources

Integrating with Hyperliquid

Setting up Hyperliquid with Privy in NodeJS

We’ll be using the Hyperliquid SDK with Privy’s ethers.js integration to interact with the Hyperliquid API.

1. Installation

First, install the necessary dependencies:

npm install @nktkas/hyperliquid @privy-io/server-auth ethers@latest

2. Initialize Privy Server SDK

Set up Privy’s server SDK to interact with your managed wallets:

import { PrivyClient } from '@privy-io/server-auth';

// Initialize Privy client with your API key
const privyClient = new PrivyClient('insert-your-app-id', 'insert-your-app-secret', {
  walletApi: {
    authorizationPrivateKey: 'insert-your-authorization-private-key' // Optional
  }
});

3. Create Ethers Compatible Signer

Use Privy’s createEthersSigner utility to create an ethers-compatible signer:

import { ethers } from 'ethers';
import { createEthersSigner } from '@privy-io/server-auth/ethers';

// Initialize your ethers provider
const provider = new ethers.JsonRpcProvider('https://base.llamarpc.com');

// Get your wallet from Privy
const walletId = 'insert-wallet-id';
const wallet = await privyClient.walletApi.getWallet({id: walletId});
const address = wallet.address;

// Create an ethers signer
const signer = createEthersSigner({
    walletId,
    address,
    provider,
    privyClient,
});

4. Initialize Hyperliquid SDK

Use the ethers signer to initialize the Hyperliquid client:

import * as hl from '@nktkas/hyperliquid';

// Create Hyperliquid transport and client
const transport = new hl.HttpTransport();
const client = new hl.WalletClient({
  transport,
  wallet: signer
});

5. Making Trades

Now you can use the client to place orders and interact with the Hyperliquid API:

// Get available assets
const meta = await client.meta();

// Find the asset index for BTC
const btcIndex = meta.universe.findIndex(asset => asset.name === "BTC");

// Place a market order
const orderResponse = await client.order({
  orders: [{
    a: btcIndex,      // Asset index
    b: true,          // Buy order
    s: "0.01",        // Size
    r: false,         // Not reduce-only
    t: { market: {} } // Market order
  }],
  grouping: "na"      // No grouping
});

console.log("Order placed:", orderResponse);

// Check open positions
const userState = await client.clearinghouseState({ user: address });
console.log("Account state:", userState);

This approach keeps your wallet’s private keys secure by delegating all signing operations to Privy’s secure API, while providing full compatibility with Hyperliquid’s trading features.

Conclusion

By combining Privy’s secure wallet infrastructure with Hyperliquid’s high-performance trading platform, you can build powerful trading applications without compromising on security. The custom signer approach allows you to leverage Privy’s key management while still benefiting from all the features of Hyperliquid’s API.

For more advanced features like subscription to real-time order book data or implementing complex trading strategies, refer to the Hyperliquid documentation.

Your application can now securely trade on Hyperliquid using Privy’s managed wallets!