|
| 1 | +import { RestClient, WebsocketClient } from '../../src/index.js'; |
| 2 | + |
| 3 | +// or |
| 4 | +// import { RestClient } from 'okx-api'; |
| 5 | + |
| 6 | +const client = new RestClient({ |
| 7 | + /** |
| 8 | + * To use "app.okx.com", set market to "US" |
| 9 | + * |
| 10 | + * Note: your available functionality may differ, refer to the corresponding API docs for more details: |
| 11 | + * https://app.okx.com/docs-v5/en/#overview-production-trading-services |
| 12 | + */ |
| 13 | + market: 'US', |
| 14 | +}); |
| 15 | + |
| 16 | +const wsClient = new WebsocketClient({ |
| 17 | + market: 'US', |
| 18 | +}); |
| 19 | + |
| 20 | +/** |
| 21 | + * This is a simple script wrapped in a immediately invoked function expression, designed to make public API calls without credentials |
| 22 | + */ |
| 23 | +(async () => { |
| 24 | + try { |
| 25 | + const results = await client.getInstruments({ instType: 'SPOT' }); |
| 26 | + |
| 27 | + console.log( |
| 28 | + 'result: ', |
| 29 | + results.filter((row) => row.baseCcy === 'SUI'), |
| 30 | + ); |
| 31 | + } catch (e) { |
| 32 | + console.error('request failed: ', e); |
| 33 | + } |
| 34 | + |
| 35 | + // Raw data will arrive on the 'update' event |
| 36 | + wsClient.on('update', (data) => { |
| 37 | + // console.log( |
| 38 | + // new Date(), |
| 39 | + // 'ws update (raw data received)', |
| 40 | + // JSON.stringify(data, null, 2), |
| 41 | + // ); |
| 42 | + // console.log('ws update (raw data received)', JSON.stringify(data, null, 2)); |
| 43 | + console.log( |
| 44 | + new Date(), |
| 45 | + 'ws update (raw data received)', |
| 46 | + JSON.stringify(data), |
| 47 | + ); |
| 48 | + }); |
| 49 | + wsClient.on('open', (data) => { |
| 50 | + console.log('ws connection opened open:', data.wsKey); |
| 51 | + }); |
| 52 | + wsClient.on('reconnected', (data) => { |
| 53 | + console.log('ws has reconnected ', data?.wsKey); |
| 54 | + }); |
| 55 | + wsClient.on('exception', (data) => { |
| 56 | + console.error('ws exception: ', data); |
| 57 | + }); |
| 58 | + wsClient.subscribe([ |
| 59 | + { |
| 60 | + channel: 'instruments', |
| 61 | + instType: 'SPOT', |
| 62 | + }, |
| 63 | + { |
| 64 | + channel: 'tickers', |
| 65 | + instId: 'ETH-BTC', |
| 66 | + }, |
| 67 | + ]); |
| 68 | +})(); |
0 commit comments