Skip to content

Instantly share code, notes, and snippets.

@jordanbaucke
Created June 19, 2013 06:15
Show Gist options
  • Save jordanbaucke/5812034 to your computer and use it in GitHub Desktop.
Save jordanbaucke/5812034 to your computer and use it in GitHub Desktop.
NodeJs example code for Bitfinex API v1 Authenticated call
var https = require('https');
var crypto = require('crypto');
var url = 'https://bitfinex.com/api/v1/balances';
var subPath = '/balances';
var api_key = '';
var api_secret = '';
var path = '/api/v1' + subPath;
console.log(path);
var payloadObject = {
request : path,
nonce : "" + Date.now(),
options : {}
};
var payloadJson = JSON.stringify(payloadObject);
console.log(JSON.stringify(payloadObject));
var payload = new Buffer(payloadJson).toString('base64');
console.log(payload);
var signature = crypto.createHmac('sha384', api_secret).update(payload).digest('hex');
var headers = {
"X-BFX-APIKEY" : api_key,
"X-BFX-PAYLOAD" : payload,
"X-BFX-SIGNATURE" : signature
};
console.log(headers);
var options = {
hostname : 'bitfinex.com',
port : 443,
path : '/api/v1/balances',
method : 'GET',
headers: headers
};
var req = https.request(options, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.end();
req.on('error', function(e) {
console.error(e);
});
@anwar0302
Copy link

hi JB,

I was trying to POST balances from VB.net using below code but getting "404-Not Found" constantly. Can you please assist me finding what I am missing.

Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Web
Imports System.Web.Script.Serialization
Imports System.Security.Cryptography

Public Class Form1
Public Shared api_url As String = "https://bitfinex.com/api"
Public Shared api_key As String = ""
Public Shared api_secret As String = ""
'Public Shared api_path As String = "/v1/symbols"
Public Shared api_path As String = "/v1/balances"

Public Class coptions
    Public hostname As String = "https://api.bitfinex.com"
    Public port As String = "443"
    Public path As String
    Public method As String = "POST"
End Class

Public Class PayLoad
    Public request As String = api_path
    Public nonce As String = Convert.ToInt64((DateTime.Now - New DateTime(1970, 1, 1)).TotalMilliseconds) ' time.Now
    Public options As New coptions
End Class

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Public Function check_balance()
    'generate payload
    Dim payLoadobject As New PayLoad()
    Dim MySerializer As JavaScriptSerializer = New JavaScriptSerializer()
    Dim payloadJson As String = MySerializer.Serialize(payLoadobject)
    Dim payload As String = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(payloadJson))

    'generate signature
    Dim encoding As New System.Text.ASCIIEncoding()
    Dim getmessageBytes As Byte() = encoding.GetBytes(payload)
    Dim secretByte As Byte() = encoding.GetBytes(api_secret)
    Dim gethmacsha384 As New HMACSHA384(secretByte)
    Dim hashmessage As Byte() = gethmacsha384.ComputeHash(getmessageBytes)
    Dim finalString As New StringBuilder()
    For i As Integer = 0 To hashmessage.Length - 1
        finalString.Append(hashmessage(i).ToString("X2"))
    Next
    Dim SIGNATURE As String = finalString.ToString()

    Dim request As HttpWebRequest
    Dim nurl As String = api_url + api_path
    Dim address As Uri = New Uri(nurl)

    request = DirectCast(WebRequest.Create(address), HttpWebRequest)
    request.Accept = True
    request.Method = "POST"
    request.Headers("X-BFX-APIKEY") = api_key
    request.Headers("X-BFX-PAYLOAD") = payload
    request.Headers("X-BFX-SIGNATURE") = SIGNATURE
    request.AllowAutoRedirect = True
    request.AllowWriteStreamBuffering = True
    request.KeepAlive = True

    Dim response As HttpWebResponse = Nothing
    Dim reader As StreamReader = Nothing
    Dim result As String = Nothing
    Try
        response = DirectCast(request.GetResponse(), HttpWebResponse)
        reader = New StreamReader(response.GetResponseStream())
        result = reader.ReadToEnd()
    Catch ex As Exception
        Return ex.Message.ToString()
    Finally
        If Not response Is Nothing Then response.Close()
    End Try

    Return result

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    tbox.Text = check_balance()
End Sub

End Class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment