← All articles

Hyperliquid API wallet stopped working? Expiry, pruning and rotation in Node.js

Hyperliquid doesn’t hand out API keys. Instead, your main wallet approves an API wallet (also called an agent wallet): a separate private key that can sign trading actions for your account but can’t withdraw. It’s a good design, but API wallets can stop working without your code changing, and the error you get back doesn’t say why. This guide covers the three reasons that happens and how to check and rotate API wallets from Node.js with the Hyperliquid JavaScript SDK (@siebly/hyperliquid-api on npm).

The symptom

A bot that traded fine yesterday now gets this on every order:

const { RestClient } = require('@siebly/hyperliquid-api');

const client = new RestClient({
  privateKey: process.env.HYPERLIQUID_API_WALLET_KEY,
  accountAddress: process.env.HYPERLIQUID_MASTER_ADDRESS,
});

client
  .submitLimitOrder({ symbol: 'BTC', side: 'buy', price: '10000', size: '0.001' })
  .catch((e) => console.error(e.name, e.message));
// HyperliquidApiError User or API Wallet 0x4612...41ea does not exist.

The address in the message is the signer, i.e. your API wallet. Hyperliquid is telling you it no longer recognises that key as approved for any account.

Why an API wallet disappears

Hyperliquid’s nonces and API wallets docs list three cases where an API wallet is pruned:

  1. It was replaced. An account has one unnamed API wallet and up to three named ones. Approving a new unnamed wallet deregisters the old unnamed one, and approving a wallet with a name that’s already in use replaces the wallet under that name. This catches people who create a “new key” in the app for a second bot and silently break the first.
  2. It expired. API wallets can be given an expiry, up to 180 days ahead. When that date passes, the wallet stops working.
  3. The account that approved it no longer has funds. Empty the master account and its API wallets can be pruned with it.

There’s a fourth cause that produces the same message and has nothing to do with pruning: the wallet was approved on one network and you’re calling the other. A testnet approval doesn’t exist on mainnet. If you pass testnet: true, the API wallet must have been approved on testnet.

Check what’s approved right now

Two read-only calls tell you where you stand. getUserRole() says whether an address is a known user, an agent (and for whom), a vault, or missing. getExtraAgents() lists the named API wallets on a master account with their expiry:

const { RestClient } = require('@siebly/hyperliquid-api');

const info = new RestClient(); // no key needed for these reads

async function checkApiWallet(apiWalletAddress, masterAddress) {
  const role = await info.getUserRole({ user: apiWalletAddress });
  console.log('API wallet role:', role);
  // { role: 'agent', data: { user: '0x...master' } } when it's still approved
  // { role: 'missing' } when Hyperliquid doesn't know it

  const agents = await info.getExtraAgents({ user: masterAddress });
  for (const a of agents) {
    const expires = a.validUntil ? new Date(a.validUntil).toISOString() : 'no expiry';
    console.log(a.name, a.address, expires);
  }
}

checkApiWallet(process.env.HYPERLIQUID_API_WALLET_ADDRESS, process.env.HYPERLIQUID_MASTER_ADDRESS);

If the role comes back missing, the wallet has gone and no amount of retrying will help. If it comes back as agent but for a different master address than you expected, your accountAddress setting is wrong. Add testnet: true to the client when you’re checking a testnet setup.

Rotate to a fresh API wallet

Hyperliquid strongly suggests generating a new API wallet each time rather than re-approving an old address, because a deregistered wallet’s nonce state may be pruned. Approving has to be signed by the main wallet, so run this somewhere you’re comfortable holding that key, not inside the trading bot:

const { RestClient } = require('@siebly/hyperliquid-api');
const { Wallet } = require('ethers');

async function rotateApiWallet() {
  const main = new RestClient({ privateKey: process.env.HYPERLIQUID_MAIN_WALLET_KEY });

  const apiWallet = Wallet.createRandom();
  await main.approveApiWallet({
    agentAddress: apiWallet.address,
    agentName: 'bot-1', // reusing a name replaces the wallet under it
  });

  console.log('New API wallet:', apiWallet.address);
  // Store apiWallet.privateKey in your secret manager, then restart the bot with it
}

rotateApiWallet().catch((e) => console.error(e.name, e.message));

The SDK throws a HyperliquidApiError when Hyperliquid rejects an action, so a failed approval won’t pass silently. To set an expiry, add a valid_until suffix to the name as described in Hyperliquid’s exchange endpoint docs (180 days is the maximum).

A few habits that prevent this

  • Use a named API wallet per bot or process (bot-1, bot-2), so approving one never replaces another. Hyperliquid also recommends one API wallet per subaccount, because nonces are tracked per signer.
  • If you set an expiry, put the date in your monitoring and rotate a week early rather than finding out from failed orders.
  • Pass accountAddress (the master or subaccount address) to the client. Account queries need the real account address, not the API wallet, otherwise you get empty results. With the SDK, an authenticated client without accountAddress resolves the master address with one userRole lookup and caches it, but setting it explicitly saves the request and makes mistakes obvious.
  • Keep mainnet and testnet keys in separate environment variables so a testnet key never ends up in a mainnet process.

Further reading

The Hyperliquid JavaScript tutorial walks through wallet and API wallet setup, account state, orders and WebSockets end to end, and the Hyperliquid SDK docs cover every REST and WebSocket method. The SDK is open source and part of the Siebly family of exchange SDKs, with over 4.5 million npm downloads across 10+ SDKs, and more in the works.

Disclosure: this guide is published by Siebly, the maintainers of @siebly/hyperliquid-api.