For Bitget spot error 43012 (“Insufficient balance”), check how much of the coin you are spending is still available after open orders and fees. On futures, 40754 (“balance not enough”) points to the available margin. For 40762, compare the order size with the maximum Bitget lets you open at your current margin and leverage. Size the order within the symbol limits in both cases.
The Node.js examples use bitget-api 3.2.9.
Bitget codes for insufficient funds and position size
| code | Bitget message | Usual cause |
|---|---|---|
43012 | Insufficient balance | A spot order costs more than the free balance of the coin you pay with (quote coin for buys, base coin for sells). |
40754 | balance not enough | Not enough available margin in the futures account for the order or transfer. |
40762 | The order size is greater than the max open size | The futures size is above what your margin and leverage allow you to open. The message text can differ between API versions, so match on the code, not the text. |
45110 | less than the minimum amount {0} USDT | The order value is under the symbol minimum, the opposite problem. |
45111 | less than the minimum order quantity | The size is under the minimum quantity. |
The usual triggers are funds sitting in the wrong account (spot money not transferred to futures), open orders holding part of the balance as frozen, the fee pushing a full-balance buy over the edge, and leverage set lower on the exchange than the bot assumes.
Install and set up the client
npm install bitget-api
Create the API key with read and trade permissions only, never withdrawal, and bind it to your server’s IP address. Keep the key, secret and passphrase in environment variables. See the API key security checklist for the full list.
import { RestClientV2 } from 'bitget-api';
const client = new RestClientV2({
apiKey: process.env.BITGET_API_KEY!,
apiSecret: process.env.BITGET_API_SECRET!,
apiPass: process.env.BITGET_API_PASS!,
});
// Round a decimal string down to a number of decimal places, without floats.
function floorToPlaces(value: string, places: number): string {
const [whole, frac = ''] = value.split('.');
if (places <= 0) return whole;
const cut = frac.slice(0, places).replace(/0+$/, '');
return cut ? `${whole}.${cut}` : whole;
}
floorToPlaces rounds down as a string, so 0.0012345 with 4 places becomes 0.0012 without floating-point surprises. Rounding down matters: rounding up is exactly how a “spend the whole balance” order turns into 43012.
Spot: check the free balance before you buy
getSpotSymbolInfo() returns the symbol rules and getSpotAccountAssets() returns the balance per coin. Only available is spendable; frozen is held by open orders and locked is not usable for trading.
export async function safeSpotBuy(symbol: string, price: string, wantSize: string) {
const [info] = (await client.getSpotSymbolInfo({ symbol })).data;
const [quote] = (await client.getSpotAccountAssets({ coin: info.quoteCoin })).data;
const size = floorToPlaces(wantSize, Number(info.quantityPrecision));
const cost = Number(size) * Number(price);
const fee = cost * Number(info.takerFeeRate);
const available = Number(quote?.available ?? '0');
if (cost < Number(info.minTradeUSDT)) {
throw new Error(`Order value ${cost} is below minTradeUSDT ${info.minTradeUSDT}`);
}
if (cost + fee > available) {
throw new Error(`Need about ${cost + fee} ${info.quoteCoin}, only ${available} available`);
}
return client.spotSubmitOrder({
symbol,
side: 'buy',
orderType: 'limit',
force: 'gtc',
price,
size,
clientOid: `buy-${Date.now()}`,
});
}
For BTCUSDT, getSpotSymbolInfo() returned quantityPrecision: "6", pricePrecision: "2", minTradeUSDT: "1" and takerFeeRate: "0.002". A limit order that ends up taking liquidity pays the taker fee, so the check leaves room for it. For a sell, check the base coin (info.baseCoin) instead of the quote coin.
Futures: ask Bitget how much you can open
On USDT-M futures the limit is margin, not the coin balance. getFuturesAccountAsset() gives available, crossedMaxAvailable and isolatedMaxAvailable, and getFuturesOpenCount() turns a margin amount, price and leverage into the size Bitget will let you open. Compare your size with that number before you call futuresSubmitOrder().
export async function safeFuturesOpen(symbol: string, price: string, wantSize: string, leverage: string) {
const productType = 'USDT-FUTURES' as const;
const marginCoin = 'USDT';
const [config] = (await client.getFuturesContractConfig({ symbol, productType })).data;
const account = (await client.getFuturesAccountAsset({ symbol, productType, marginCoin })).data;
const openable = (
await client.getFuturesOpenCount({
symbol,
productType,
marginCoin,
openAmount: account.crossedMaxAvailable,
openPrice: price,
leverage,
})
).data.size;
const size = floorToPlaces(wantSize, Number(config.volumePlace));
if (Number(size) < Number(config.minTradeNum)) {
throw new Error(`Size ${size} is below minTradeNum ${config.minTradeNum}`);
}
if (Number(size) > Number(openable)) {
throw new Error(`Size ${size} is above the openable size ${openable}`);
}
return client.futuresSubmitOrder({
symbol,
productType,
marginMode: 'crossed',
marginCoin,
size,
price,
side: 'buy',
tradeSide: 'open',
orderType: 'limit',
force: 'gtc',
clientOid: `open-${Date.now()}`,
});
}
For BTCUSDT, getFuturesContractConfig() returned minTradeNum: "0.0001", volumePlace: "4", sizeMultiplier: "0.0001" and minTradeUSDT: "5". Sizes are in the base coin (BTC here) and must be a multiple of sizeMultiplier. Pass the same leverage that is set on the exchange; if you set it with setFuturesLeverage(), use that value here too. For isolated margin, use isolatedMaxAvailable and marginMode: 'isolated'.
Unified Trading Account: one call with RestClientV3
If the account has been upgraded to Bitget’s Unified Trading Account, use RestClientV3. getMaxOpenAvailable() returns available, maxBuyOpen and maxSellOpen for a symbol, category and price in a single request, and getBalances() returns the account assets.
import { RestClientV3 } from 'bitget-api';
const uta = new RestClientV3({
apiKey: process.env.BITGET_API_KEY!,
apiSecret: process.env.BITGET_API_SECRET!,
apiPass: process.env.BITGET_API_PASS!,
});
export async function utaMaxOpen(symbol: string, price: string) {
const { data } = await uta.getMaxOpenAvailable({
category: 'USDT-FUTURES',
symbol,
orderType: 'limit',
side: 'buy',
price,
});
console.log('available', data.available, 'maxBuyOpen', data.maxBuyOpen);
return data;
}
Place the order with submitNewOrder() once the size is at or below maxBuyOpen (or maxSellOpen for shorts).
Reading the error in your catch block
On an exchange error, bitget-api exposes the HTTP status in code and Bitget’s response in body. Branch on body.code, which is a string.
{
"code": 400,
"message": "Bad Request",
"body": { "code": "40037", "msg": "Apikey does not exist", "requestTime": 1795626517303, "data": null }
}
try {
await safeSpotBuy('BTCUSDT', '50000', '0.001');
} catch (e: unknown) {
const body = (e as { body?: { code?: string; msg?: string } })?.body;
if (body?.code === '43012' || body?.code === '40754' || body?.code === '40762') {
console.error('Balance or size problem:', body.code, body.msg);
} else {
throw e;
}
}
Don’t retry these errors in a loop: the balance won’t change on its own. Log the code and the numbers you used, then re-size or stop. The SDK replaces the secret and passphrase in the thrown error’s requestOptions, but the API key is still there, so log body rather than the whole object.
