1.0.0-beta.3 • Published 4 months ago

okex-api v1.0.0-beta.3

Weekly downloads
3
License
MIT
Repository
github
Last release
4 months ago

okex-api

A modern, asynchronous, and easy-to-use Okx API wrapper for Node.js/bun/Web Browser/Cloudflare workers.

This 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.

Features

  • Depends nothing.
    • Use fetch api for restful api.
    • Use globalThis.WebScoket for webScoket api(So requires node version >= 22).
    • Use web crypto api for signature.
  • Typscript support.
  • Implements almost all Okx v5 apis.
  • Websocket api with autoreconnect & promisified order op api.
  • Browser support.
  • Cloudflare workers support.

Installation

### Using npm
npm install okex-api

### Using bun
bun install okex-api

Documentation

For restful API, see OKX API Documentation, and find it in HttpApi, you will known how to use.

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.

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 });

Abount API credentials

Create API credentials at okx, OKX my-api

1.0.0-alpha.5

4 months ago

1.0.0-alpha.4

4 months ago

1.0.0-alpha.3

4 months ago

1.0.0-alpha.2

4 months ago

1.0.0-alpha.1

4 months ago

1.0.0-beta.2

4 months ago

1.0.0-beta.3

4 months ago

1.0.0-beta.1

4 months ago

0.14.0

2 years ago

0.13.1

2 years ago

0.13.2

2 years ago

0.12.0

3 years ago

0.11.5

3 years ago

0.11.4

4 years ago

0.11.3

4 years ago

0.11.0

4 years ago

0.11.1

4 years ago

0.11.2

4 years ago

0.10.5

4 years ago

0.10.3

4 years ago

0.10.4

4 years ago

0.10.2

4 years ago

0.10.1

4 years ago

0.10.0

4 years ago

0.9.0

4 years ago

0.8.1

4 years ago

0.8.0

4 years ago

0.7.3

5 years ago

0.7.2

5 years ago

0.7.1

5 years ago

0.7.0

5 years ago

0.6.2

5 years ago

0.6.1

5 years ago

0.6.0

5 years ago

0.5.3

5 years ago

0.5.0

6 years ago

0.5.2

6 years ago

0.5.1

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago