OKX REST JavaScript SDK example: demo-trading.ts

OKX REST demo trading JavaScript example. Read the source code, review its SDK calls, and browse related exchange integration examples.

This example uses okx-api, the JavaScript and TypeScript SDK for OKX. Install it with npm install okx-api.

We use environment variables in our examples for API keys. It is your responsibility to manage and secure your keys appropriately.

examples/OKX/Rest/demo-trading.ts

What this example covers

  • OKX REST API JavaScript example.
  • Uses the Siebly OKX SDK package okx-api instead of hand-written HTTP request plumbing.
  • Source path: OKX/Rest/demo-trading.ts.
  • Example category: REST.
  • Imports SDK symbols including RestClient, WebsocketClient.

How to use this example

  • Start here for the specific request or stream pattern, then check the matching SDK guide for install, credentials, and operational notes.
  • Open the repository source when you need the latest committed version: GitHub source file.
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);  }})(); 

Subscribe on Substack

Complete the Substack form below to join our newsletter. Substack handles all subscriber data directly.