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

@cex-io/cexio-spot-trading

v2.0.1

Published

The official Node.js client for CEX.IO Spot Trading API

Downloads

21

Readme

CEX.IO Spot Trading

The official Node.js client for CEX.IO Spot Trading API (https://trade.cex.io/docs)

Features

  • Easy to use, requires only key-secret pair to setup
  • Handle all transport work, just call required action
  • Popular protocols supported, REST and WebSocket onboard

Installation

npm install @cex-io/cexio-spot-trading

Rest client

const { RestClient } = require('@cex-io/cexio-spot-trading')
const defaultClient = new RestClient()
const authenticatedClient = new RestClient(apiKey, apiSecret, options)

Arguments for RestClient are optional. For private actions you need to generate apiKey and apiSecret pair from UI terminal.

  • apiKey string - Api key for specific account.
  • apiSecret string - Api secret for specific account.
  • options object - Additional settings for client.

Available client options described below, they all are optional:

  • apiLimit integer - Rate limit value for apiKey. Default is 300. Client will check requests count and prevent from spam the server. You can ask to increase this limit.
  • timeout integer - Request timeout in milliseconds. Default is 30000.
  • rejectUnauthorized boolean - This option useful when you test demo env. Default is true.
  • host string - Can be changed to test your bot on demo environment. Default is 'https://trade.cex.io/api/spot/'
  • apiUrlPublic string - Use a concrete url for public API calls. This option overrides host value. Default is 'https://trade.cex.io/api/spot/rest-public/'
  • apiUrl string - Use a concrete url for private API calls. This option overrides host value. Default is 'https://trade.cex.io/api/spot/rest/'

Public actions

To make a public request use async callPublic(action, params) method. This method return Promise which resolves with server response. If some error was occurred then method rejects with status code and error description.

For more details check api reference.

const { RestClient } = require('@cex-io/cexio-spot-trading')

const client = new RestClient()

try {
  const res = await client.callPublic('get_ticker')
  console.log(res)
} catch (err) {
  console.log(err)
}
{ error: 'Bad Request', statusCode: 400 }
{ error: 'Unexpected error', statusCode: 500 }

Private actions

To make private api calls use async callPrivate(action, params). It's similar to public method but requires apiKey and apiSecret arguments to client initialization. Each private request is signed with HMAC sha256 so if key is incorrect or signature is wrong client will return rejected promise with error like this { error: 'Authorization Failed', statusCode: 401 }

const { RestClient } = require('@cex-io/cexio-spot-trading')

const key = '_account_api_key_'
const secret = '_account_api_secret_'
const action = 'get_my_orders'
const params = {
  pair: 'BTC-USD'
}

const client = new RestClient(key, secret)

try {
  const res = await client.callPrivate(action, params)
  console.log(res)
} catch (err) {
  console.error(err)
}

Success response example:

{ ok: 'ok', data: { ... } }

WebSocket client

const { WebsocketClient } = require('@cex-io/cexio-spot-trading')
const ws = new WebsocketClient(apiKey, apiSecret, options)

To init the WebsocketClient you must pass apiKey and apiSecret arguments. You can generate them in UI terminal.

  • apiKey string - Api key for specific account.
  • apiSecret string - Api secret for specific account.
  • options object - Additional settings for client.

Available client options described below, they all are optional:

  • wsReplyTimeout integer - Request timeout in milliseconds. Default is 30000.
  • rejectUnauthorized boolean - This option useful when you test demo env. Default is true.
  • host string - Can be changed to test your bot on demo environment. Default is 'wss://trade.cex.io/api/spot/'
  • apiUrlPublic string - Use a concrete url for public WS calls. This option overrides host value. Default is 'wss://trade.cex.io/api/spot/ws-public/'
  • apiUrl string - Use a concrete url for private WS calls. This option overrides host value. Default is 'wss://trade.cex.io/api/spot/ws/'

Call Private actions

To send request to the server you need to connect and auth first. Everything is under the hood and all you need is call async ws.connect() method. After that you can invoke async ws.callPrivate(action, params) method which returns Promise with server response. If some error was occurred then method rejects with status code and error description.

  const { WebsocketClient } = require('@cex-io/cexio-spot-trading')
  const ws = new WebsocketClient(apiKey, apiSecret, options)

  await ws.connect() // connect and auth on the server

  const res = await ws.callPrivate(action, params)
  console.log('result:', res)

  ws.disconnect() // close connection

Subscribe to updates

The WebsocketClient allows you to receive updates. The following types of updates are available: account_update, executionReport, order_book_increment, tradeUpdate, etc. You can get more details about them in documentation.

const { WebsocketClient } = require('@cex-io/cexio-spot-trading')
const ws = new WebsocketClient(apiKey, apiSecret)

try {
  await ws.connect()

  ws.subscribe('executionReport', msg => {
    console.log('executionReport:', msg)
  })

  ws.subscribe('account_update', msg => {
    console.log('account_update:', msg)
  })
} catch (err) {
  console.error(err)
}