Installations
npm install okx-v5-api
Developer Guide
Typescript
Yes
Module System
ESM
Node Version
18.12.0
NPM Version
8.19.2
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (93.27%)
JavaScript (6.73%)
Developer
airicyu
Download Statistics
Total Downloads
1,636
Last Day
2
Last Week
8
Last Month
41
Last Year
391
GitHub Statistics
12 Commits
2 Forks
1 Watching
1 Branches
1 Contributors
Bundle Size
99.15 kB
Minified
37.69 kB
Minified + Gzipped
Package Meta Information
Latest Version
2.4.1
Package Id
okx-v5-api@2.4.1
Unpacked Size
43.02 kB
Size
12.30 kB
File Count
31
NPM Version
8.19.2
Node Version
18.12.0
Publised On
21 Sept 2023
Total Downloads
Cumulative downloads
Total Downloads
1,636
Last day
-66.7%
2
Compared to previous day
Last week
-65.2%
8
Compared to previous week
Last month
215.4%
41
Compared to previous month
Last year
-67.8%
391
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
okx-v5-api
This is a non-official OKX V5 API SDK for javascript.
install
npm install okx-v5-api
Hello world
main.js
1import { OkxV5Api } from 'okx-v5-api' 2 3const run = async () => { 4 const okxV5Api = new OkxV5Api({ 5 apiBaseUrl: 'https://www.okx.com', 6 /* profileConfig: { <-- only needed if you will call private APIs 7 apiKey: 'XXX', 8 secretKey: 'YYY', 9 passPhrase: 'ZZZ', 10 }, */ 11 }) 12 13 const apiResult = await okxV5Api.call({ 14 method: 'GET', 15 path: '/api/v5/market/exchange-rate', 16 }) 17 18 console.log(apiResult) 19 20 const apiResult2 = await okxV5Api.call({ 21 method: 'GET', 22 path: '/api/v5/asset/currencies?ccy=BTC,ETH,USDT,USDC', 23 }) 24 25 console.log(apiResult2) 26} 27 28run()
output
ApiResult {
code: '0',
msg: '',
data: [ { usdCny: '7.244' } ],
error: null
}
...
GET method API sample
1const apiResult = ( 2 await okxV5Api.call({ 3 method: 'GET', 4 path: '/api/v5/xxx/yyy?ccy=BTC' 5 }) 6).getOrThrow()
POST method API sample
1const apiResult = ( 2 await okxV5Api.call({ 3 method: 'POST', 4 path: '/api/v5/xxx/yyy', 5 data: { 6 param: 123 7 } 8 }) 9).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
1const apiResult = ( 2 await okxV5Api.call({ 3 method: 'GET', 4 path: '/api/v5/market/exchange-rate', 5 }) 6).getOrThrow() 7 8console.log(apiResult)
output
[ { usdCny: '7.244' } ]
Demo error case
1const apiResult = ( 2 await okxV5Api.call({ 3 method: 'POST', 4 path: '/api/v5/account/set-position-mode', 5 data: { 6 posMode: 'net_mode', 7 }, 8 }) 9).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.
1const okxV5Api = new OkxV5Api({ 2 apiBaseUrl: 'https://www.okx.com', 3 profileConfig: { 4 apiKey: 'XXX', 5 secretKey: 'YYY', 6 passPhrase: 'ZZZ', 7 }, 8})
Demo-trading mode
If you need to call private APIs, you need authentication by passing the profileConfig
property when you create new OkxV5Api
instance.
1const okxV5Api = new OkxV5Api({ 2 apiBaseUrl: 'https://www.okx.com', 3 profileConfig: { 4 apiKey: 'XXX', 5 secretKey: 'YYY', 6 passPhrase: 'ZZZ', 7 simulated: true // <--- pass this param 8 }, 9})
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.
1export class ApiError extends Error { 2 code: string 3 msg: string 4 5 constructor(code: string, msg: string) { 6 super(`${code}: ${msg}`) 7 this.code = code 8 this.msg = msg 9 this.name = 'ApiError' 10 } 11}
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No security vulnerabilities found.