<pre><code>string Query()
        {
            string url = "https://api.bitfinex.com/v1/balances";
            string paramDict = "{\"request\":\"\\/v1\\/balances\",\"nonce\":\"" + GetNonce() * 100000 + "\"}";
            string payload = EncodeTo64(paramDict);
            string message = payload;
            string skey = secret;
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            byte[] keyByte = encoding.GetBytes(skey);
            HMACSHA384 hmacsha384 = new HMACSHA384(keyByte);
            byte[] messageBytes = encoding.GetBytes(message);
            byte[] hashmessage = hmacsha384.ComputeHash(messageBytes);
            string signature = this.ByteToString(hashmessage);
            var headers = new Dictionary&lt;string, string&gt;
            {
                {"X-BFX-APIKEY",key},
                {"X-BFX-PAYLOAD",payload},
                {"X-BFX-SIGNATURE",signature}
            };
            var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
            if (request == null)
                throw new Exception("Non HTTP WebRequest");

            var data = encoding.GetBytes(paramDict);
            request.Method = "POST";
            request.Timeout = 30000;
            request.ContentLength = data.Length;
            foreach (var a in headers)
            {
                request.Headers.Add(a.Key, a.Value);
            }

            var write = request.GetRequestStream();
            write.Write(data, 0, data.Length);

            var respone = request.GetResponse() as HttpWebResponse;

            return null;
        }
</code></pre>

<p>this is new code to fix error with HMACSHA384</p>

<pre><code> string ByteToString(byte[] buff)
        {
            string sbinary = "";

            for (int i = 0; i &lt; buff.Length; i++)
            {
                sbinary += buff[i].ToString("X2"); // hex format
            }
            return (sbinary);
        }
</code></pre>

<p>and some more fixes.</p>

<pre><code>long GetNonce()
        {
            return nonce++;
        }

        static public string EncodeTo64(string toEncode)
        {
            byte[] toEncodeAsBytes
                  = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
            string returnValue
                  = System.Convert.ToBase64String(toEncodeAsBytes);
            return returnValue;
        }
</code></pre>

<p>oh and last fix to prove the error</p>

<pre><code>    string signature = this.ByteToString(hashmessage).ToLower();
</code></pre>

CC BY-SA 3.0
2015-01-28T23:51:01.873Z
2015-01-28T23:51:01.873Z
Yura Sultanov
<p>You are getting an <a href="https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error" rel="nofollow">HTTP 400 error</a> which means the server is (for some reason) refusing to process your request. It could be due to bad string formatting, incorrect or unsuported <a href="https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Request_fields" rel="nofollow">headers</a> format or <a href="https://en.wikipedia.org/wiki/Internet_media_type" rel="nofollow">MIME type</a>. You might want to take a deeper look at your headers and the way the request in general is being parsed.</p>

<p>Looking at your code, I can only help but notice the "@" at the beginning of your url string declaration. I do not know if this is intented or whether the language you're coding with requires you to use it (I might just be rising a flag where it isn't), I however, and out of pure personal interest (but might help with the issue) would like to know, are you coding C#? .NET? other?</p>

CC BY-SA 3.0
2015-01-28T23:44:03.960Z
I Use c#, i try many type of headers, 
1-st error was with forming hmac
string payload = EncodeTo64(paramDict);
            string message = payload;
            string skey = secret;
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            byte[] keyByte = encoding.GetBytes(skey);
            HMACSHA384 hmacsha384 = new HMACSHA384(keyByte);
            byte[] messageBytes = encoding.GetBytes(message);
            byte[] hashmessage = hmacsha384.ComputeHash(messageBytes);
            string signature = this.ByteToString(hashmessage);
Yura Sultanov
CC BY-SA 3.0
2015-01-28T23:57:30.617Z
I see you posted a self-answer, did you happen to fix the whole issue and tackle other bugs? Are you getting a 200 OK now? Can't help much with C#, thought I'd throw some pointers at the least though.
Dhuum
CC BY-SA 3.0
2015-01-28T23:40:31.987Z
2015-01-28T23:40:31.987Z
Dhuum
<pre><code>            string url = @"https://api.bitfinex.com/v1/balances";
            string paramDict = "{\"request\": \"/v1/balances\",\"nonce\": \"" + GetNonce() * 10000 + "\"}";
            string payload = Convert.ToBase64String(Encoding.ASCII.GetBytes(paramDict));
            string signature = BitConverter.ToString(hmac.ComputeHash(Encoding.ASCII.GetBytes(payload))).Replace("-","").ToLower();

            var headers = new Dictionary&lt;string, string&gt;
            {
                {"X-BFX-APIKEY",key},
                {"X-BFX-PAYLOAD",payload},
                {"X-BFX-SIGNATURE",signature}
            };


            var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
            if (request == null)
                throw new Exception("Non HTTP WebRequest");

            var data = System.Text.Encoding.ASCII.GetBytes(paramDict);
            request.Method = "POST";
            request.Timeout = 30000;
            request.ContentLength = data.Length;
            foreach (var a in headers)
            {
                request.Headers.Add(a.Key, a.Value);
            }

            var write = request.GetRequestStream();
            write.Write(data, 0, data.Length);

            var respone = request.GetResponse() as HttpWebResponse;
</code></pre>

<p>This is my code. Maybe you can see where I made an error. As for me, API is normal made.</p>

<p>When I run this code it returns an HTTP 400 error code.</p>

<api><c#><bitfinex>
Bitfinex 400 error
Yura Sultanov
# Bitfinex 400 error

(Asked by: username_0 on 2015-01-28)

string url = @"https://api.bitfinex.com/v1/balances";
            string paramDict = "{\"request\": \"/v1/balances\",\"nonce\": \"" + GetNonce() * 10000 + "\"}";
            string payload = Convert.ToBase64String(Encoding.ASCII.GetBytes(paramDict));
            string signature = BitConverter.ToString(hmac.ComputeHash(Encoding.ASCII.GetBytes(payload))).Replace("-","").ToLower();

            var headers = new Dictionary
            {
                {"X-BFX-APIKEY",key},
                {"X-BFX-PAYLOAD",payload},
                {"X-BFX-SIGNATURE",signature}
            };


            var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
            if (request == null)
                throw new Exception("Non HTTP WebRequest");

            var data = System.Text.Encoding.ASCII.GetBytes(paramDict);
            request.Method = "POST";
            request.Timeout = 30000;
            request.ContentLength = data.Length;
            foreach (var a in headers)
            {
                request.Headers.Add(a.Key, a.Value);
            }

            var write = request.GetRequestStream();
            write.Write(data, 0, data.Length);

            var respone = request.GetResponse() as HttpWebResponse;

This is my code. Maybe you can see where I made an error. As for me, API is normal made.
When I run this code it returns an HTTP 400 error code.

**Tags:** api, c#, bitfinex


---

## 2 answers

### Answer by username_0 on 2015-01-28. Score: 1

string Query()
        {
            string url = "https://api.bitfinex.com/v1/balances";
            string paramDict = "{\"request\":\"\\/v1\\/balances\",\"nonce\":\"" + GetNonce() * 100000 + "\"}";
            string payload = EncodeTo64(paramDict);
            string message = payload;
            string skey = secret;
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            byte[] keyByte = encoding.GetBytes(skey);
            HMACSHA384 hmacsha384 = new HMACSHA384(keyByte);
            byte[] messageBytes = encoding.GetBytes(message);
            byte[] hashmessage = hmacsha384.ComputeHash(messageBytes);
            string signature = this.ByteToString(hashmessage);
            var headers = new Dictionary
            {
                {"X-BFX-APIKEY",key},
                {"X-BFX-PAYLOAD",payload},
                {"X-BFX-SIGNATURE",signature}
            };
            var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
            if (request == null)
                throw new Exception("Non HTTP WebRequest");

            var data = encoding.GetBytes(paramDict);
            request.Method = "POST";
            request.Timeout = 30000;
            request.ContentLength = data.Length;
            foreach (var a in headers)
            {
                request.Headers.Add(a.Key, a.Value);
            }

            var write = request.GetRequestStream();
            write.Write(data, 0, data.Length);

            var respone = request.GetResponse() as HttpWebResponse;

            return null;
        }

this is new code to fix error with HMACSHA384
 string ByteToString(byte[] buff)
        {
            string sbinary = "";

            for (int i = 0; i < buff.Length; i++)
            {
                sbinary += buff[i].ToString("X2"); // hex format
            }
            return (sbinary);
        }

and some more fixes.
long GetNonce()
        {
            return nonce++;
        }

        static public string EncodeTo64(string toEncode)
        {
            byte[] toEncodeAsBytes
                  = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
            string returnValue
                  = System.Convert.ToBase64String(toEncodeAsBytes);
            return returnValue;
        }

oh and last fix to prove the error
    string signature = this.ByteToString(hashmessage).ToLower();


---

### Answer by username_1 on 2015-01-28. Score: 0

You are getting an [HTTP 400 error](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error) which means the server is (for some reason) refusing to process your request. It could be due to bad string formatting, incorrect or unsuported [headers](https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Request_fields) format or [MIME type](https://en.wikipedia.org/wiki/Internet_media_type). You might want to take a deeper look at your headers and the way the request in general is being parsed.

Looking at your code, I can only help but notice the "@" at the beginning of your url string declaration. I do not know if this is intented or whether the language you're coding with requires you to use it (I might just be rising a flag where it isn't), I however, and out of pure personal interest (but might help with the issue) would like to know, are you coding C#? .NET? other?

> **Comments:**
> **username_0**: I Use c#, i try many type of headers,  1-st error was with forming hmac string payload = EncodeTo64(paramDict);             string message = payload;             string skey = secret;             System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();             byte[] keyByte = encoding.GetBytes(skey);             HMACSHA384 hmacsha384 = new HMACSHA384(keyByte);             byte[] messageBytes = encoding.GetBytes(message);             byte[] hashmessage = hmacsha384.ComputeHash(messageBytes);             string signature = this.ByteToString(hashmessage);  
> **username_1**: I see you posted a self-answer, did you happen to fix the whole issue and tackle other bugs? Are you getting a 200 OK now? Can't help much with C#, thought I'd throw some pointers at the least though.  

---

