Skip to content

Instantly share code, notes, and snippets.

@sennett-lau
Created June 19, 2025 07:26
Show Gist options
  • Select an option

  • Save sennett-lau/6b050cf4f4e465553d71cf188e3c9b41 to your computer and use it in GitHub Desktop.

Select an option

Save sennett-lau/6b050cf4f4e465553d71cf188e3c9b41 to your computer and use it in GitHub Desktop.
@nktkas/hyperliquid Usage sample
/**
* Creating Info API and Exchange API client
* Use `isTestnet: true` option for Testnet
*/
import { HttpTransport, InfoClient, ExchangeClient } from "@nktkas/hyperliquid";
import { privateKeyToAccount } from "viem/accounts";
const MASTER_PRIVATE_KEY = ''
const main = async () => {
// include `{ isTestnet: true }` for testnet
const transport = new HttpTransport({ isTestnet: true })
// Public API, no wallet signing is required
const infoClient = new InfoClient({
transport,
})
// Exchange API require a wallet for signing, `{ isTestnet: true }` for testnet
const wallet = privateKeyToAccount(`0x${MASTER_PRIVATE_KEY}`);
const exchClient = new ExchangeClient({
transport,
wallet,
isTestnet: true,
})
}
main().then(() => {
console.log('done')
}).catch((error) => {
console.error(error)
})
/**
* Using the master account wallet to authorize another address as a API Wallet
*/
import { HttpTransport, ExchangeClient } from "@nktkas/hyperliquid";
import { privateKeyToAccount } from "viem/accounts";
const MASTER_PRIVATE_KEY = ''
const API_WALLET_1_ADDRESS = ''
const main = async () => {
const wallet = privateKeyToAccount(`0x${MASTER_PRIVATE_KEY}`);
const transport = new HttpTransport({ isTestnet: true })
const exchClient = new ExchangeClient({
transport,
wallet,
isTestnet: true,
})
const agentAddress = API_WALLET_1_ADDRESS
const agentName = 'Agent 1' // important for API wallet revoke
await exchClient.approveAgent({
agentAddress,
agentName,
})
}
main().then(() => {
console.log('done')
}).catch((error) => {
console.error(error)
})
/**
* Using Info API to retrieve user portfolio
*/
import { HttpTransport, InfoClient } from "@nktkas/hyperliquid";
const TARGET_WALLET_ADDRESS = ''
const main = async () => {
const transport = new HttpTransport({ isTestnet: true })
const infoClient = new InfoClient({
transport,
})
const res = await infoClient.portfolio({
user: TARGET_WALLET_ADDRESS,
})
console.log(JSON.stringify(res, null, 2))
}
main().then(() => {
console.log('done')
}).catch((error) => {
console.error(error)
})
/**
* Using the master account wallet to revoke another address's API Wallet access
*/
import { HttpTransport, ExchangeClient } from "@nktkas/hyperliquid";
import { privateKeyToAccount } from "viem/accounts";
const MASTER_PRIVATE_KEY = ''
const NULL_ADDRESS = '0x0000000000000000000000000000000000000000'
const main = async () => {
const wallet = privateKeyToAccount(`0x${MASTER_PRIVATE_KEY}`);
const transport = new HttpTransport({ isTestnet: true })
const exchClient = new ExchangeClient({
transport,
wallet,
isTestnet: true,
})
const agentAddress = NULL_ADDRESS
const agentName = 'Agent 1' // authorized agent name
await exchClient.approveAgent({
agentAddress,
agentName,
})
}
main().then(() => {
console.log('done')
}).catch((error) => {
console.error(error)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment