3

I'm trying to use the httr R package to place orders on BitMex through their API.

I found some guidance over here, and after specifying both my API key and secret in respectively the objects K and S, I've tried the following

verb <- 'POST'
expires <- floor(as.numeric(Sys.time() + 10000))
path <- '/api/v1/order'
data <- '{"symbol":"XBTUSD","price":4500,"orderQty":10}'

body <- paste0(verb, path, expires, data)
signature <- hmac(S, body, algo = 'sha256')

body_l <- list(verb = verb, expires = expires, path = path, data = data)

And then both:

msg <- POST('https://www.bitmex.com/api/v1/order', encode = 'json', body = body_l, add_headers('api-key' = K, 'api-signature' = signature, 'api-expires' = expires))

and:

msg <- POST('https://www.bitmex.com/api/v1/order', body = body, add_headers('api-key' = K, 'api-signature' = signature, 'api-expires' = expires))

Give me the same error message when checked:

rawToChar(msg$content)
[1] "{\"error\":{\"message\":\"Signature not valid.\",\"name\":\"HTTPError\"}}"

I've tried to set it up according to how BitMex explains to use their API, but I appear to be missing something. They list out a couple of issues that might underly my invalid signature issue, but they don't seem to help me out. When following their example I get the exact same hashes, so that seems to be in order.

1 Answers1

0

bit late to the party here but hopefully this helps!

Your POST call just needs some minor changes:

  • add content_type_json()

  • include .headers = c('the headers') in add_headers(). See example below:

library(httr)
library(digest)

S <- "your api secret"
K <- "your api key"

verb <- 'POST'
expires <- floor(as.numeric(Sys.time() + 10))
path <- '/api/v1/order'
data <- '{"symbol":"XBTUSD","price":4500,"orderQty":10}'

body <- paste0(verb, path, expires, data)
signature <- hmac(S, body, algo = 'sha256')

msg <- POST('https://www.bitmex.com/api/v1/order', 
            encode = 'json', 
            body = data, 
            content_type_json(),
            add_headers(.headers = c('api-key' = K, 
                        'api-signature' = signature, 
                        'api-expires' = expires)))

content(msg, "text")

I have a package on CRAN - bitmexr - that provides a wrapper around the majority of BitMEX's API endpoints that you might be interested in. Still quite a "young" package so I would welcome any feedback!

hfshr
  • 118
  • 1
  • 7