A modern, asynchronous, and easy-to-use [Okx API](https://www.okx.com/docs-v5) wrapper for Node.js/bun/Web Browser/Cloudflare workers.
npm install okex-apiThis lib is developed by bun, and written in typescript.
Thanks to okx-api, it defines almost all types of apis, and it's good to use. But it uses axios for restful api, ws for websocket api(when useing node), and node:crypto for signature, so it cannot be used in Cloudfalre Workers, that's why i rewrite.
bash
$3
npm install okex-api$3
bun install okex-api
`Documentation
For restful API, see OKX API Documentation, and find it in HttpApi, you will known how to use.
`js
import { HttpApi } from 'okex-api';
// ...const api = await HttpApi.create(process.env.API_KEY!, process.env.API_SECRET!, process.env.PASSPHRASE!); // Because web crypto api is async, so we have to create in async function.
console.log(await api.getBalance({ ccy: 'BTC'}));
console.log(await api.getAccountInstruments({ instType: 'SWAP' }));
`For websocket API, see OKX API Documentation to find channels you want to subscribe.
`js
import { WsPublic, WsPrivate } from 'okex-api';
// ...const wsPublic = new WsPublic(); // Public channel.
wsPublic.addEventListener('open', () => {
console.log('Connected');
ws.subscribe({ channel: 'tickers', instId: 'ETH-USD-SWAP' });
});
wsPublic.addEventListener('subscribe', (event) => {
console.log(event.data);
});
wsPublic.addEventListener('tickers', (event) => {
console.log(event.data);
});
wsPublic.connect();
const wsPrivate = await WsPrivate.create(process.env.API_KEY!, process.env.API_SECRET!, process.env.PASSPHRASE!); // Will auto login & auto connect.
wsPrivate.addEventListener('open', () => {
console.log('Connected');
wsPrivate.subscribe({ channel: 'positions', instType: 'SWAP', instFamily: 'ETH-USD' });
});
wsPrivate.addEventListener('login', (event) => {
console.log(event.data);
});
wsPrivate.addEventListener('positions', (event) => {
console.log(event.data);
});
wsPrivate.connect();
// Websocket api to plalce order, cancel order, etc.
const order = await wsPrivate.order({ instId: 'ETH-USD-SWAP', tdMode: 'cross', side: 'sell', posSide: 'short', ordType: 'market', sz: '1'});
const ordId = order.data[0].ordId;
await wsPrivate.cancelOrder({ instId: 'ETH-USD-SWAP', ordId });
``