<p>Finally discovered the fix: hexhash has to be lower case:</p>

<pre><code>string hexHash = BitConverter.ToString(hash).Replace("-", "").ToLower();
</code></pre>

CC BY-SA 3.0
2014-09-11T14:55:11.243Z
Such a weird API... Thanks for both your question and (self) response
RichardW1001
CC BY-SA 3.0
2016-02-20T16:21:02.750Z
I am having the same issue in python, but the hexdigest is already in lower case...
fccoelho
CC BY-SA 3.0
2017-12-14T20:41:02.020Z
did you ever get the order/new to work ? I am still getting bad request despite running properly on other calls such as balances etc.
Farhan Ahmed
CC BY-SA 3.0
2014-06-08T08:35:05.733Z
2014-06-08T08:35:05.733Z
steve cook
<p>Trying to get authentication with BitFinex to work. Docs here: <a href="https://www.bitfinex.com/pages/api" rel="nofollow">https://www.bitfinex.com/pages/api</a></p>

<blockquote>
  <p>Say the client wants to make a request to POST</p>
  
  <p><a href="https://api.bitfinex.com/v1/order/new" rel="nofollow">https://api.bitfinex.com/v1/order/new</a> </p>
  
  <p>With a payload of <code>{ "request":"/v1/order/new", "nonce": "1234", "option1": ... }</code> </p>
  
  <p>The nonce provided must be strictly increasing.</p>
  
  <p>To authenticate a request, use the following:</p>

<pre><code>payload = parameters-dictionary -&gt; JSON encode -&gt; base64 
signature = HMAC-SHA384(payload, api-secret) as hexadecimal 
send (api-key, payload, signature) 
</code></pre>
  
  <p>These are encoded as HTTP headers named:
  X-BFX-APIKEY X-BFX-PAYLOAD X-BFX-SIGNATURE</p>
</blockquote>

<p>Here is the c# code, which follows the above example, but generates a HTTP 400 Bad Request. Any ideas how to fix?</p>

<pre><code>        long nonce = DateTime.Now.ToUnixTimestampMS(); //returns a strictly increasing timestamp based number e.g. 1402207693893

        string path = "https://api.bitfinex.com/v1/balances";
        string paramDict = "{\"request\": \"/v1/balances\",\"nonce\": \"" + nonce + "\"}"; //ie. {"request": "/v1/balances","nonce": "1402207693893"}
        string payload = Convert.ToBase64String(Encoding.UTF8.GetBytes(paramDict));

        //API Sign
        HMACSHA384 hmac = new HMACSHA384(Encoding.UTF8.GetBytes(APISECRET));           //My API SECRET
        byte[] hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(payload));
        string hexHash = BitConverter.ToString(hash).Replace("-", "");

        NameValueCollection headers = new NameValueCollection();
        headers.Add("X-BFX-APIKEY", APIKEY);                                            //My API KEY
        headers.Add("X-BFX-PAYLOAD", payload);
        headers.Add("X-BFX-SIGNATURE", hexHash);

        //POST data
        try
        {
            //create post request
            HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(path);
            request.KeepAlive = true;
            request.Method = "POST";

            //add headers
            request.Headers.Add(headers);

            //write out payload
            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(paramDict);
            request.ContentLength = byteArray.Length;
            using (var writer = request.GetRequestStream()){writer.Write(byteArray, 0, byteArray.Length);}

            //read reply
            using (var response = request.GetResponse() as System.Net.HttpWebResponse)
            {
                using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    //get reply (JSON)
                    string responseContent = reader.ReadToEnd();
                }
            }
        }
        catch (Exception e)
        {
            //always throws an exception here
            Debug.WriteLine(e.Message);
        }
</code></pre>

<api><authentication><bitfinex>
BitFinex API call returns 400 bad request
steve cook
# BitFinex API call returns 400 bad request

(Asked by: username_0 on 2014-06-08)

Trying to get authentication with BitFinex to work. Docs here: <https://www.bitfinex.com/pages/api>

> Say the client wants to make a request to POST
>
> <https://api.bitfinex.com/v1/order/new>
>
> With a payload of `{ "request":"/v1/order/new", "nonce": "1234", "option1": ... }`
>
> The nonce provided must be strictly increasing.
>
> To authenticate a request, use the following:
>
> ```
> payload = parameters-dictionary -> JSON encode -> base64 
> signature = HMAC-SHA384(payload, api-secret) as hexadecimal 
> send (api-key, payload, signature) 
>
> ```
>
> These are encoded as HTTP headers named:
> X-BFX-APIKEY X-BFX-PAYLOAD X-BFX-SIGNATURE

Here is the c# code, which follows the above example, but generates a HTTP 400 Bad Request. Any ideas how to fix?

```
        long nonce = DateTime.Now.ToUnixTimestampMS(); //returns a strictly increasing timestamp based number e.g. 1402207693893

        string path = "https://api.bitfinex.com/v1/balances";
        string paramDict = "{\"request\": \"/v1/balances\",\"nonce\": \"" + nonce + "\"}"; //ie. {"request": "/v1/balances","nonce": "1402207693893"}
        string payload = Convert.ToBase64String(Encoding.UTF8.GetBytes(paramDict));

        //API Sign
        HMACSHA384 hmac = new HMACSHA384(Encoding.UTF8.GetBytes(APISECRET));           //My API SECRET
        byte[] hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(payload));
        string hexHash = BitConverter.ToString(hash).Replace("-", "");

        NameValueCollection headers = new NameValueCollection();
        headers.Add("X-BFX-APIKEY", APIKEY);                                            //My API KEY
        headers.Add("X-BFX-PAYLOAD", payload);
        headers.Add("X-BFX-SIGNATURE", hexHash);

        //POST data
        try
        {
            //create post request
            HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(path);
            request.KeepAlive = true;
            request.Method = "POST";

            //add headers
            request.Headers.Add(headers);

            //write out payload
            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(paramDict);
            request.ContentLength = byteArray.Length;
            using (var writer = request.GetRequestStream()){writer.Write(byteArray, 0, byteArray.Length);}

            //read reply
            using (var response = request.GetResponse() as System.Net.HttpWebResponse)
            {
                using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    //get reply (JSON)
                    string responseContent = reader.ReadToEnd();
                }
            }
        }
        catch (Exception e)
        {
            //always throws an exception here
            Debug.WriteLine(e.Message);
        }

```

**Tags:** api, authentication, bitfinex


---

## 1 answers

### Answer by username_0 on 2014-06-08. Score: 6 | Accepted answer

Finally discovered the fix: hexhash has to be lower case:

```
string hexHash = BitConverter.ToString(hash).Replace("-", "").ToLower();

```

> **Comments:**
> **username_1**: Such a weird API... Thanks for both your question and (self) response  
> **username_2**: I am having the same issue in python, but the hexdigest is already in lower case...  
> **username_3**: did you ever get the order/new to work ? I am still getting bad request despite running properly on other calls such as balances etc.  

---

