OKX REST demo trading JavaScript example for the Siebly OKX SDK, covering exchange REST API and WebSocket integration, setup, and production SDK docs.
We use environment variables in our examples for API keys. It is your responsibility to manage and secure your keys appropriately.
okx-api instead of hand-written HTTP request plumbing.OKX/Rest/demo-trading.ts.RestClient, WebsocketClient.import { RestClient, WebsocketClient } from 'okx-api'; // read from environmental variables, your demo trading keysconst API_KEY = process.env.API_KEY_COM;const API_SECRET = process.env.API_SECRET_COM;const API_PASS = process.env.API_PASSPHRASE_COM; if (!API_KEY || !API_SECRET || !API_PASS) { throw new Error( 'Missing api credentials. Use environmental variables or hard code in the script', );} const demoRestClient = new RestClient({ // okx api credentials apiKey: API_KEY, apiSecret: API_SECRET, apiPass: API_PASS, demoTrading: true,}); const demoWsClient = new WebsocketClient({ demoTrading: true, accounts: [ { apiKey: API_KEY, apiSecret: API_SECRET, apiPass: API_PASS, }, ],}); // Raw data will arrive on the 'update' eventdemoWsClient.on('update', (data) => { // console.log('ws update (raw data received)', JSON.stringify(data, null, 2)); console.log('ws update (raw data received)', JSON.stringify(data));}); demoWsClient.on('open', (data) => { console.log('connection opened open:', data.wsKey);}); // Replies (e.g. authenticating or subscribing to channels) will arrive on the 'response' eventdemoWsClient.on('response', (data) => { // console.log('ws response: ', JSON.stringify(data, null, 2)); console.log('ws response: ', JSON.stringify(data));}); demoWsClient.on('reconnect', ({ wsKey }) => { console.log('ws automatically reconnecting.... ', wsKey);});demoWsClient.on('reconnected', (data) => { console.log('ws has reconnected ', data?.wsKey);});demoWsClient.on('exception', (data) => { console.error('ws exception: ', data);}); // Subscribe to demo account events:demoWsClient.subscribe([ { channel: 'account', }, { channel: 'positions', instType: 'ANY', },]); (async () => { try { const balResponse1 = await demoRestClient.getBalance({ ccy: 'USDT', }); console.log('balResponse1: ', JSON.stringify(balResponse1, null, 2)); /** Simple examples for private REST API calls with bybit's V5 REST APIs */ const response = await demoRestClient.getPositions({ instType: 'SPOT', }); console.log('response:', response); // Trade USDT linear perps const buyOrderResult = await demoRestClient.submitOrder({ instId: 'BTC-USDT', ordType: 'market', side: 'buy', sz: '0.002', tdMode: 'cash', tgtCcy: 'base_ccy', }); console.log('buyOrderResult:', buyOrderResult); const sellOrderResult = await demoRestClient.submitOrder({ instId: 'BTC-USDT', ordType: 'market', side: 'sell', sz: '0.002', tdMode: 'cash', tgtCcy: 'base_ccy', }); console.log('sellOrderResult:', sellOrderResult); const balResponse2 = await demoRestClient.getBalance({ ccy: 'USDT', }); console.log('balResponse2: ', JSON.stringify(balResponse2, null, 2)); } catch (e) { console.error('request failed: ', e); }})(); We use essential cookies and optional analytics. Read the Privacy Policy.
Essential cookies stay on. Toggle analytics if you want to share anonymous usage insights. You can revisit this anytime via Cookie Settings in the footer.