Skip to content

Instantly share code, notes, and snippets.

@joshuarossi
Created February 1, 2017 20:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuarossi/456a16bd17577a9e7681b6d43880b920 to your computer and use it in GitHub Desktop.
Save joshuarossi/456a16bd17577a9e7681b6d43880b920 to your computer and use it in GitHub Desktop.
const ws = require('ws')
const crypto = require('crypto')
const bfx = new ws('wss://api.bitfinex.com/ws/2')
// Replace with your own api key and secret
let api_key = ''
let api_secret = ''
// Ensure your nonce is always increasing
let nonce = Date.now()
// Payload to be sent to authenticate
let payload = {
apiKey: api_key,
event: 'auth',
authPayload: 'AUTH' + nonce,
authNonce: +nonce
}
payload.authSig = crypto.createHmac('sha384', api_secret)
.update(payload.authPayload)
.digest('hex')
// On open event, send authentication message
bfx.on('open', () => {
bfx.send(JSON.stringify(payload))
})
// Here is the example order from the docs (all WS messages must be strings, hence JSON.stringify)
let order = JSON.stringify([
0,
'on',
null,
{
gid: 1,
cid: 12345,
type: "LIMIT",
symbol: "tBTCUSD",
amount: "1.0",
price: "500",
hidden: 0
}
])
// When we get the message telling us we are authenticated, send our order
bfx.on('message', (message, buffer) => {
msg = JSON.parse(message)
if (msg.event === 'auth' && msg.status === 'OK') {
bfx.send(order)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment