← All articles

OKX API 51000 “Parameter posSide error” in Node.js: match your position mode

OKX error 51000 means “Parameter {name} error”, and the version that catches most people placing perpetual or futures orders is “Parameter posSide error”. It isn’t a signing or permissions problem. It means the posSide you sent (or didn’t send) doesn’t match your account’s position mode.

This post shows how to read the position mode and build orders that match it, using okx-api (3.2.9 at the time of writing).

Why it happens

OKX accounts run SWAP and FUTURES positions in one of two modes:

  • net_mode (one-way): one net position per instrument. Buys and sells move the same position. posSide is optional and, if sent, must be net.
  • long_short_mode (hedge): separate long and short positions per instrument. posSide is required and must be long or short.

So the two common causes are sending posSide: 'long' on a net-mode account, and leaving posSide out on a long/short-mode account. Code copied from a tutorial written for the other mode fails straight away. For SPOT and MARGIN orders, leave posSide out completely.

What the error looks like in okx-api

For order requests OKX returns a top-level error plus a per-order code. okx-api throws the response body, so the useful part is in data[0]:

import { RestClient } from 'okx-api';

const client = new RestClient({
  apiKey: process.env.OKX_API_KEY,
  apiSecret: process.env.OKX_API_SECRET,
  apiPass: process.env.OKX_API_PASSPHRASE,
});

try {
  await client.submitOrder({
    instId: 'BTC-USDT-SWAP',
    tdMode: 'cross',
    side: 'buy',
    posSide: 'long', // wrong if the account is in net_mode
    ordType: 'limit',
    px: '10000',
    sz: '1',
  });
} catch (e) {
  // Per-order result first, then the top-level code (auth errors such as 50111 only have this).
  const code = e?.data?.[0]?.sCode ?? e?.code;
  const msg = e?.data?.[0]?.sMsg ?? e?.msg;
  console.error(code, msg); // e.g. 51000 Parameter posSide error
}

Read the position mode once, then build orders to match

getAccountConfiguration() returns the account settings, including posMode. Read it at startup and keep it, rather than guessing:

const [config] = await client.getAccountConfiguration();
const posMode = config.posMode; // 'net_mode' or 'long_short_mode'

// direction: 'long' or 'short'. action: 'open' or 'close'.
function buildSwapOrder({ instId, direction, action, sz, px }) {
  const side =
    (direction === 'long') === (action === 'open') ? 'buy' : 'sell';

  const order = {
    instId,
    tdMode: 'cross',
    side,
    ordType: px ? 'limit' : 'market',
    sz,
    ...(px ? { px } : {}),
  };

  if (posMode === 'long_short_mode') {
    // Hedge mode: always say which position the order belongs to.
    order.posSide = direction;
  } else if (action === 'close') {
    // Net mode: no posSide. Mark closing orders as reduce-only
    // so they can't flip the position.
    order.reduceOnly = true;
  }
  return order;
}

await client.submitOrder(
  buildSwapOrder({ instId: 'BTC-USDT-SWAP', direction: 'long', action: 'open', sz: '1', px: '10000' }),
);

In long/short mode, closing a long is a sell with posSide: 'long', and closing a short is a buy with posSide: 'short'. That’s the other place hedge-mode code usually goes wrong.

Or change the mode to suit your code

If your code is written for one mode, you can switch the account with setPositionMode(). OKX only allows this when there are no open positions and no pending orders on the account, so do it before trading, not mid-session:

await client.setPositionMode({ posMode: 'net_mode' }); // or 'long_short_mode'

Other 51000 messages

51000 is a general “bad parameter” code, and the message names the field. If it isn’t posSide, check these against the instrument from getInstruments({ instType: 'SWAP', instId }):

  • sz: SWAP and FUTURES sizes are in contracts, not coins. BTC-USDT-SWAP has ctVal 0.01 BTC and lotSz 0.01, so sz: '1' is 0.01 BTC.
  • px: must be a multiple of tickSz (0.1 for BTC-USDT-SWAP).
  • tdMode: SWAP needs cross or isolated. cash is for spot only.

Test it on demo trading first

OKX demo trading has its own API keys and its own position-mode setting. Create demo keys and pass demoTrading: true to the client, which adds the x-simulated-trading header for you:

const demo = new RestClient({
  apiKey: process.env.OKX_DEMO_API_KEY,
  apiSecret: process.env.OKX_DEMO_API_SECRET,
  apiPass: process.env.OKX_DEMO_API_PASSPHRASE,
  demoTrading: true,
});

Checklist

  • Read posMode with getAccountConfiguration() at startup.
  • long_short_mode: always send posSide as long or short.
  • net_mode: leave posSide out (or send net), and use reduceOnly for closing orders.
  • Never send posSide on SPOT or MARGIN orders.
  • Read data[0].sCode and sMsg to see which parameter OKX rejected.

For the auth side of OKX errors, see OKX 50101 to 50113 in Node.js. The OKX API JavaScript tutorial covers REST, WebSockets and the WebSocket API in more detail.