Authentication and account balances
Bitget credentials consist of an API key, API secret and API passphrase. The passphrase belongs to the key; it is not your account password. Give account-read examples read permission only. Enable trading only for applications that need order commands. Withdrawal permission is unnecessary here.
Keep credentials on your server. Never embed them in browser, React Native or Expo bundles, commit them, or expose them through public environment-variable prefixes. Use an IP allowlist where supported and synchronize the server clock.
Configure credentials
Store these in a private .env file excluded from version control:
BITGET_API_KEY=your_api_key
BITGET_API_SECRET=your_api_secret
BITGET_API_PASSPHRASE=your_api_passphraseSave this helper as authenticated-client.ts:
import { RestClientV2, RestClientV3 } from 'bitget-api';
export function requiredEnv(name: string): string {
const value = process.env[name];
if (!value) {
throw new Error(`Set ${name} in your server environment.`);
}
return value;
}
export const credentials = {
apiKey: requiredEnv('BITGET_API_KEY'),
apiSecret: requiredEnv('BITGET_API_SECRET'),
apiPass: requiredEnv('BITGET_API_PASSPHRASE'),
};
export const classic = new RestClientV2(credentials);
export const unified = new RestClientV3(credentials);The SDK signs requests automatically. The helper creates both clients without sending requests; use V3 for UTA or V2 for Classic. Both default to production. For simulation, configure demo trading on every client.
Read starting account state
Save as account-state.ts beside the helper:
import { classic, unified } from './authenticated-client.js';
async function main() {
const account = process.argv[2];
if (account === 'classic') {
const balances = await classic.getSpotAccountAssets();
const orders = await classic.getSpotOpenOrders({
symbol: 'BTCUSDT',
});
console.log('Classic Spot balances:', balances.data);
console.log('Classic open orders (one page):', orders.data);
} else if (account === 'uta') {
const balances = await unified.getBalances();
const orders = await unified.getUnfilledOrders({
category: 'SPOT',
symbol: 'BTCUSDT',
});
console.log('UTA balances:', balances.data);
console.log('UTA open orders (one page):', orders.data);
} else {
throw new Error('Choose classic or uta to match your account.');
}
}
main().catch(() => {
console.error('Account read failed. Check the API error code and message on your server without logging credentials.');
process.exitCode = 1;
});Compile and run the examples
npm install bitget-api
npm install --save-dev typescript @types/node
npx tsc --target ES2022 --module NodeNext --strict --skipLibCheck --outDir dist authenticated-client.ts account-state.ts
node --env-file=.env dist/account-state.js utaFor an existing Classic account, use classic instead of uta. Run with Node.js 24 from the project directory containing .env. Relative .js imports refer to the compiled output. These commands compile the .ts files directly. If you use the TypeScript starter, add them to its include list, which uses .mts files by default.
For the other private examples, replace account-state.ts in the compile command with private-stream.ts, classic-private-stream.ts, first-order.ts or ws-api.ts, retaining authenticated-client.ts. Compilation itself sends no requests.
getSpotAccountAssets() reads Classic Spot assets; getBalances() reads UTA balances. The example prints one page of open orders. Use pagination to load all orders in your application. A successful request may return empty balances or no open orders.
Load account state at startup, then use the private account stream to keep it current. Use REST again for explicit lookups or recovery after a gap. Keep account data out of public logs.