Authentication and account balances
Gate.io private requests require an API key and secret. Enable read permission for account data and trading permission only when your application needs order commands. These examples do not need withdrawal permission.
Keep credentials server-side. 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 keep the server's clock synchronized.
Configure credentials
Store these in a private .env file excluded from version control:
GATEIO_API_KEY=your_api_key
GATEIO_API_SECRET=your_api_secret
GATEIO_USE_TESTNET=falseSave this helper as authenticated-client.ts:
import { RestClient } from 'gateio-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('GATEIO_API_KEY'),
apiSecret: requiredEnv('GATEIO_API_SECRET'),
};
// Set GATEIO_USE_TESTNET=true and use TestNet keys to switch all private examples.
export const clientOptions = {
...credentials,
useTestnet: process.env.GATEIO_USE_TESTNET === 'true',
};
export const client = new RestClient(clientOptions, { timeout: 10000 });The SDK signs private REST requests and WebSocket subscriptions automatically. The helper defaults to live trading. To use TestNet, set GATEIO_USE_TESTNET=true and replace both credentials with TestNet credentials. All private examples import this shared configuration.
Read starting account state
Save as account-state.ts beside the helper:
import { client } from './authenticated-client.js';
async function main() {
console.log('Spot balances:', await client.getSpotAccounts());
console.log(
'Open BTC_USDT orders:',
await client.getSpotOrders({
currency_pair: 'BTC_USDT',
status: 'open',
account: 'spot',
page: 1,
limit: 100,
}),
);
}
main().catch(() => {
console.error(
'Account request failed. Check credentials, permissions and environment.',
);
process.exitCode = 1;
});Compile and run the examples
In your project directory, install the SDK and compiler dependencies, then compile these files:
npm install gateio-api
npm install --save-dev typescript @types/node
npx tsc --ignoreConfig --target ES2022 --module NodeNext --strict --skipLibCheck --outDir dist authenticated-client.ts account-state.ts
node --env-file=.env dist/account-state.jsRun with Node.js 24 from the project directory containing .env. Relative .js imports refer to compiled files. The command compiles the listed files independently of any existing tsconfig.json. To use your project configuration instead, add these .ts files to its include list and run npx tsc without file arguments. The TypeScript starter includes only .mts files by default.
For other private examples, replace account-state.ts in the compile command with private-stream.ts, first-order.ts or ws-api.ts, retaining authenticated-client.ts. Run the emitted .js file as shown in its guide.
getSpotAccounts() returns currency balances with available and locked amounts. The example reads one page of open BTC_USDT Spot orders; paginate to load all relevant orders. An empty result can be valid when the account has no balances or open orders.
Load initial account state through REST, then use private updates to keep it current. Keep balances and raw authenticated errors out of shared logs.