npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

okx-api-new

v0.0.6

Published

Node.js connector for OKX REST APIs and WebSockets, with TypeScript & integration tests.

Downloads

17

Readme

Node.js & Typescript OKX (OKEX) API & WebSocket SDK

原仓库:https://github.com/tiagosiebler/okx-api

[okx okex SDK for nodejs rest & websockets api][1]

Node.js连接器,用于OKX APIs和WebSockets:

  1. 与所有OKX APIs的完全集成。
  2. 支持TypeScript(具有大多数API请求和响应的类型声明)。
  3. 超过100个端到端测试进行真实的API调用和WebSocket连接,在它们到达npm之前验证任何更改。
  4. 强大的WebSocket集成
  • 可配置连接心跳(自动检测失败的连接)。
  • 自动重新连接,然后重新订阅工作流程。
  • 自动身份验证和心跳处理。
  1. 浏览器支持(通过webpack捆绑包,请参见下面的“浏览器使用”)。

下载

npm install okx-api-new

文档说明

大多数方法接受JS对象。这些可以使用okx的API文档中指定的参数进行填充,或者检查rest-client类方法中的类型定义。

Contributions & Thanks

Support my efforts to make algo trading accessible to all - register with my referral links:

For more ways to give thanks & support my efforts, visit Contributions & Thanks!

项目结构

该项目使用TypeScript,资源存储在三个关键结构中。

  • src - the whole connector written in typescript
  • lib - the javascript version of the project (compiled from typescript). This should not be edited directly, as it will be overwritten with each release. This is also the version published to npm.
  • dist - the packed bundle of the project for use in browser environments (manual, using webpack).
  • examples - some implementation examples & demonstrations. Contributions are welcome!

使用方法

基本使用实例

  1. 根据需求在OKX API Documentation找到对应的接口 -> A
  2. 在代码中引入之后,点击'okx-api-new' 进入包,找到rest-client.js 文件,搜索 A ,复制函数名,
  3. 在代码中使用

// 实例一
import { RestClient, OrderRequest } from 'okx-api-new';
const API_KEY = "生成的key"
const API_SECRET = "生成的密钥"
const API_PASS = "密码"

const client = new RestClient({
  apiKey: API_KEY,
  apiSecret: API_SECRET,
  apiPass: API_PASS,
});

async function main() {
  const args = {
    instId: "TRX-USDT-SWAP",
  }
  // 获取资金费率
  const info = await client.getFundingRateHistory(args)
  console.log(JSON.stringify(info));
}

main()
// 实例二
import { RestClient, OrderRequest, WebsocketClient } from 'okx-api-new';
const API_KEY = "生成的key"
const API_SECRET = "生成的密钥"
const API_PASS = "密码"

const client = new RestClient({
  apiKey: API_KEY,
  apiSecret: API_SECRET,
  apiPass: API_PASS,
});
/*
 * 如果需要测试的相关配置,看WebsocketClient可选项
 * 1. 收到打开ws, 会先执行new WebsocketClient() 打印一段信息
 * 2. 第二步 wsClient.on('open',
 * 3. 第三步 wsClient.on('response',
 * 4. 第四步 wsClient.on('update'
 * 
 *
 */

// 私有频道
// const wsClient = new WebsocketClient(
//   {
//     accounts: [
//       {
//         apiKey: API_KEY,
//         apiSecret: API_SECRET,
//         apiPass: API_PASS,
//       },
//     ],
//   },
// );

// 共有频道
const wsClient = new WebsocketClient({});


wsClient.on('update', (data) => {
  console.log(new Date(), " 接受到的消息: ", JSON.stringify(data));
});
wsClient.on('open', (data) => {
  console.log('ws connection opened open:', data.wsKey);
});

wsClient.on('response', (data) => {
  console.log('ws response received: ', JSON.stringify(data, null, 2));
});
// 尝试自动重连接
wsClient.on('reconnect', ({ wsKey }) => {
  console.log('ws automatically reconnecting.... ', wsKey);
});
// 自动重连成功
wsClient.on('reconnected', (data) => {
  console.log('ws has reconnected ', data?.wsKey);
});
// 报错
wsClient.on('error', (data) => {
  console.error('ws exception: ', data);
});


// ====>>> 订阅 资金费率
wsClient.subscribe({
  channel: "funding-rate",
  instId: "BTC-USDT-SWAP"
});

npm 发布流程

REST Client

Requests & Responses

HTTP 200 and "code" in the response body === "0"

如果返回非正常,请看APIResponse.

Websocket Client

此连接器包括用于 OKX 公共和私有 websockets 的高性能 node.js 和 typescript websocket 客户端。

  • If your IDE doesn't have IntelliSense, check the websocket-client.ts for a list of methods, params & return types.
  • When subscribing to channels, only the "args" should be passed as an object or array when calling the websocket client subcribe() function: API docs.
  • TypeScript recommended (but it is not required) for a richer experience: typescript-subscribe
  • The ws client will automatically open connections as needed when subscribing to a channel.
  • If the connection is lost for any reason, the ws client will detect this (via the connection heartbeats). It will then:
    • Automatically teardown the dead connection.
    • Automatically respawn a fresh connection.
    • Automatically reauthenticate, if using private channels.
    • Automatically resubscribe to previously subscribed topics.
    • Resume producing events as before, without extra handling needed in your logic.
  • The ws client will automatically authenticate if accounts are provided and a private channel is subscribed to.
  • Up to 100 accounts are supported on the private connection, as per the API docs. Authentication is automatic if accounts are provided.
  • For examples in using the websocket client, check the examples in the repo:
    • Private channels (account data): examples/ws-private.ts
    • Public chanels (market data): examples/ws-public.ts
    • These examples are written in TypeScript, so can be executed with ts-node for easy testing: ts-node examples/ws-private.ts
    • Or convert them to javascript:
      • Change the import { ... } from 'okx-api' to const { ... } = require('okx-api');
      • Rename the file to ws-private.js
      • And execute with node: node examples/ws-private.js

Public Events

See examples/ws-public.ts for a full example:

typescript-events-public

Private Events

See examples/ws-private.ts for a full example:

typescript-events

Browser/Frontend Usage

Import

This is the "modern" way, allowing the package to be directly imported into frontend projects with full typescript support.

  1. Install these dependencies
    npm install crypto-browserify stream-browserify
  2. Add this to your tsconfig.json
    {
      "compilerOptions": {
        "paths": {
          "crypto": [
            "./node_modules/crypto-browserify"
          ],
          "stream": [
            "./node_modules/stream-browserify"
          ]
    }
  3. Declare this in the global context of your application (ex: in polyfills for angular)
    (window as any).global = window;

Webpack

This is the "old" way of using this package on webpages. This will build a minified js bundle that can be pulled in using a script tag on a website.

Build a bundle using webpack:

  • npm install
  • npm build
  • npm pack

The bundle can be found in dist/. Altough usage should be largely consistent, smaller differences will exist. Documentation is still TODO.


Star History

Star History Chart