1.0.0 • Published 3 years ago

huobi-api-js v1.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
3 years ago

huobi-api-js

npm version Build Codecov branch

dependencies Status devDependencies Status

This is a non-official HuoBi Rest API nodejs module which support for signature version 2.

(這是非官方的火幣REST API nodejs module,支援簽名版本2。)


Install

$ npm i huobi-api-js

Usage

  • calling huobi REST API without handling signature protocol yourself
import { HbApi } from 'huobi-js'

...

const hbApi = new HbApi({
    apiConfig: config.api,
    profileConfig: config.huobi,
})

...

const account = await hbApi.restApi({ path: `/v1/acco3unt/accounts`, method: 'GET' })

API

New service class

Create a new service class which is reusable to call multiple REST APIs

const hbApi = new HbApi({
    apiBaseUrl: config.apiBaseUrl,
    profileConfig: config.huobi,
})

constructor parameters:

{
    apiBaseUrl: string
    profileConfig: {
        accessKey: string
        secretKey: string
    }
    logger?: Logger
    httpClient?: HttpClient
}
ParameterTypeDescriptionMandatory
apiBaseUrlstringAPI base URLY
profileConfig.accessKeystringaccess keyY
profileConfig.secretKeystringsecretKeyY
loggerobjectyour logger implementationN
httpClientfunctionyour httpClient implementationN

Call huobi Rest API

Call REST API which internally handled the signature protocol and return result as Promise

hbApi.restApi({ path: `/v1/acco3unt/accounts`, method: 'GET' })

Method: restAPI

Parameters:

ParameterTypeDescriptionMandatory
pathstringREST API path, e.g: "/v1/acco3unt/accounts"Y
methodstringeither "GET" or "POST"Y
queryRecord<string, unknown>query parametersN
bodyRecord<string, unknown>post body dataN
timeoutnumberHTTP request timeout in ms, default value is 5000msN

Return type: Promise of data


Example

import { HbApi } from 'huobi-js'

const options = {
    apiBaseUrl: 'https://api.huobipro.com',
    profileConfig: {
        accessKey: '<REPLACE_WITH_YOUR_ACCESS_KEY>',
        secretKey: '<REPLACE_WITH_YOUR_SECRET_KEY>',
    },
}

async function run() {
    const hbApi = new HbApi(options)

    const CURRENCY_USDC = 'usdc'

    /*****
     * root user info
     */
    const account = await hbApi.restApi({ path: `/v1/acco3unt/accounts`, method: 'GET' })
    console.log('account:', { account })

    const subUsers = await hbApi.restApi({ path: `/v2/sub-user/user-list`, method: 'GET' })
    console.log('subUsers:', { subUsers })

    /**
     * Get sub account aggregate balance
     */
    const subAccountAggregateBalance = await hbApi.restApi({
        path: `/v1/subuser/aggregate-balance`,
        method: 'GET',
    })
    console.log('subAccountAggregateBalance:', { subAccountAggregateBalance })

    const withdrawQuota = await hbApi.restApi({
        path: `/v2/account/withdraw/quota`,
        method: 'GET',
        query: {
            currency: CURRENCY_USDC,
        },
    })
    console.log(`withdrawQuota:`, {
        withdrawQuota,
    })

    console.log('exit')
}

run()