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

bitfinex-api-node-john

v2.0.0

Published

Node reference library for Bitfinex API

Downloads

4

Readme

Bitfinex Trading API for Node.JS. Bitcoin, Ether and Litecoin trading

Build Status

A Node.JS reference implementation of the Bitfinex API

  • Official implementation
  • REST v2 API
  • WebSockets v2 API

Documentation at https://docs.bitfinex.com/v2/reference

Installation

  npm i bitfinex-api-node

See doc/ for REST2 and WS2 API methods.

Usage

Version 2.0.0 of bitfinex-api-node supports the v2 REST and WebSocket APIs. The clients for v1 of those APIs are maintained for backwards compatibility, but deprecated.

As network calls are slow, data is sent as arrays. In order to reconstruct key / value pairs, set opts.transform to true when creating an interface.

The BFX constructor returns a client manager, which can be used to create clients for v1 & v2 of the REST and WebSocket APIs via .rest() and .ws(). The options for the clients can be defined here, or passed in later

const BFX = require('bitfinex-api-node')

const bfx = new BFX({
  apiKey: '...',
  apiSecret: '...',

  ws: {
    autoReconnect: true,
    seqAudit: true,
    packetWDDelay: 10 * 1000
  }
})

The clients are cached per version/options pair, and default to version 2:

let ws2 = bfx.ws() //
ws2 = bfx.ws(2)    // same client
const ws1 = bfx.ws(1)

const rest2 = bfx.rest(2, {
  // options
})

The websocket client is recommended for receiving realtime data & notifications on completed actions.

For more examples, check the examples/ folder.

NOTE: v1 REST and WS clients

Both v1 client classes & server APIs have been deprecated, and will be removed. In the meantime, some methods available via RESTv1 have been exposed on RESTv2 to prevent future migration issues. Although the underlying implementation of these methods is likely to change once they are fully ported to v2, the signatures should remain the same.

WS2 Example: Sending an order & tracking status

const ws = bfx.ws()

ws.on('error', (err) => console.log(err))
ws.on('open', ws.auth.bind(ws))

ws.once('auth', () => {
  const o = new Order({
    cid: Date.now(),
    symbol: 'tETHUSD',
    amount: 0.1,
    type: Order.type.MARKET
  }, ws)

  // Enable automatic updates
  o.registerListeners()

  o.on('update', () => {
    console.log(`order updated: ${o.serialize()}`)
  })

  o.on('close', () => {
    console.log(`order closed: ${o.status}`)
    ws.close()
  })

  o.submit().then(() => {
    console.log(`submitted order ${o.id}`)
  }).catch((err) => {
    console.error(err)
    ws.close()
  })
})

ws.open()

WS2 Example: Cancel all open orders

const ws = bfx.ws()

ws.on('error', (err) => console.log(err))
ws.on('open', ws.auth.bind(ws))

ws.onOrderSnapshot({}, (orders) => {
  if (orders.length === 0) {
    console.log('no open orders')
    return
  }

  console.log(`recv ${orders.length} open orders`)

  ws.cancelOrders(orders).then(() => {
    console.log('cancelled orders')
  })
})

ws.open()

WS2 Example: Subscribe to trades by pair

const ws = bfx.ws()

ws.on('error', (err) => console.log(err))
ws.on('open', () => {
  ws.onTrade({ pair: 'BTCUSD' }, (trade) => {
    if (Array.isArray(trade[0])) {
      console.log(`recv snapshot of ${trade.length} trades`)
    } else {
      console.log(`trade: ${JSON.stringify(trade)}`)
    }
  })
})

ws.open()

Version 2.0.0 Breaking changes:

constructor takes only an options object now, including the API keys.

Old:

new BFX(API_KEY, API_SECRET, { version: 2 })

since 2.0.0:

new BFX({ apiKey: '', apiSecret: '' })

trade and orderbook snapshots are emitted as nested lists

To make dealing with snapshots better predictable, snapshots are emitted as an array.

normalized orderbooks for R0

Lists of raw orderbooks (R0) are ordered in the same order as P0, P1, P2, P3

Testing

npm test

FAQ

My websocket won't connect!

Did you call open()? :)

nonce too small

I make multiple parallel request and I receive an error that the nonce is too small. What does it mean?

Nonces are used to guard against replay attacks. When multiple HTTP requests arrive at the API with the wrong nonce, e.g. because of an async timing issue, the API will reject the request.

If you need to go parallel, you have to use multiple API keys right now.

How do te and tu messages differ?

A te packet is sent first to the client immediately after a trade has been matched & executed, followed by a tu message once it has completed processing. During times of high load, the tu message may be noticably delayed, and as such only the te message should be used for a realtime feed.

Contributors