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-v5-api

v2.4.1

Published

This is a non-official [OKX V5 API](https://www.okx.com/docs-v5/) SDK for javascript.

Downloads

10

Readme

okx-v5-api

This is a non-official OKX V5 API SDK for javascript.

install

npm install okx-v5-api


Hello world

main.js

import { OkxV5Api } from 'okx-v5-api'

const run = async () => {
    const okxV5Api = new OkxV5Api({
        apiBaseUrl: 'https://www.okx.com',
        /* profileConfig: { <-- only needed if you will call private APIs
            apiKey: 'XXX',
            secretKey: 'YYY',
            passPhrase: 'ZZZ',
        }, */
    })

    const apiResult = await okxV5Api.call({
        method: 'GET',
        path: '/api/v5/market/exchange-rate',
    })

    console.log(apiResult)
    
    const apiResult2 = await okxV5Api.call({
        method: 'GET',
        path: '/api/v5/asset/currencies?ccy=BTC,ETH,USDT,USDC',
    })
    
    console.log(apiResult2)
}

run()

output

ApiResult {
  code: '0',
  msg: '',
  data: [ { usdCny: '7.244' } ],
  error: null
}

...

GET method API sample

const apiResult = (
    await okxV5Api.call({
        method: 'GET',
        path: '/api/v5/xxx/yyy?ccy=BTC'
    })
).getOrThrow()

POST method API sample

const apiResult = (
    await okxV5Api.call({
        method: 'POST',
        path: '/api/v5/xxx/yyy',
        data: {
          param: 123
        }
    })
).getOrThrow()

By default we return an ApiResult object to represent the raw response body.

Get Data or Throw Error

ApiResult object's getOrThrow method can:

  • if API success, return the data
  • if API error, throw the error

Demo success case

const apiResult = (
    await okxV5Api.call({
        method: 'GET',
        path: '/api/v5/market/exchange-rate',
    })
).getOrThrow()

console.log(apiResult)

output

[ { usdCny: '7.244' } ]

Demo error case

const apiResult = (
    await okxV5Api.call({
        method: 'POST',
        path: '/api/v5/account/set-position-mode',
        data: {
            posMode: 'net_mode',
        },
    })
).getOrThrow()

output

...\ApiResult.js:15
            this.error = new ApiError_1.ApiError(code, message);
                         ^

ApiError: 50114: Invalid Authority
    at new ApiResult (.....)
    at OkxV5Api.call (.....)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async run (.....\main.js:19:25) {
  code: '50114',
  msg: 'Invalid Authority'
}

Authentication

If you need to call private APIs, you need authentication by passing the profileConfig property when you create new OkxV5Api instance.

const okxV5Api = new OkxV5Api({
    apiBaseUrl: 'https://www.okx.com',
    profileConfig: {
        apiKey: 'XXX',
        secretKey: 'YYY',
        passPhrase: 'ZZZ',
    },
})

Demo-trading mode

If you need to call private APIs, you need authentication by passing the profileConfig property when you create new OkxV5Api instance.

const okxV5Api = new OkxV5Api({
    apiBaseUrl: 'https://www.okx.com',
    profileConfig: {
        apiKey: 'XXX',
        secretKey: 'YYY',
        passPhrase: 'ZZZ',
        simulated: true // <--- pass this param
    },
})

API

class OkxV5Api (main class)

Basically you only need to create an instance of OkxV5Api once, and reuse it to call different APIs by the call methods.

The call methods return a ApiResult object


class ApiResult (Thin wrapper to result)

It is thin wrapper of the V5 API's raw response result. It has the following attributes:

  • code (string)
  • msg (string)
  • data (any)

In addition:

  • success (boolean)
  • error (of type ApiError or null)

It also has a method of getOrThrow, which return the data if success, or throw error otherwise.


class ApiError (Error wrapper)

just an Error wrapping V5-API's code and message.

export class ApiError extends Error {
    code: string
    msg: string

    constructor(code: string, msg: string) {
        super(`${code}: ${msg}`)
        this.code = code
        this.msg = msg
        this.name = 'ApiError'
    }
}