Skip to content

Instantly share code, notes, and snippets.

@gsee
Last active September 18, 2019 20:30
Show Gist options
  • Save gsee/b20b3b9893cd74e462a8 to your computer and use it in GitHub Desktop.
Save gsee/b20b3b9893cd74e462a8 to your computer and use it in GitHub Desktop.
authenticate and place an order on Coinbase Exchange in R
library(RCurl) # for base64Decode(), base64Encode(), getURLContent(), and getCurlHandle()
library(digest) # for hmac()
library(RJSONIO) # for toJSON() and fromJSON()
api.key <- "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
secret <- "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
passphrase <- "your passphrase"
api.url <- "https://api.gdax.com"
req.url <- "/orders"
method <- "POST"
timestamp <- format(as.numeric(Sys.time()), digits=13)
key <- base64Decode(secret, mode="raw")
order <- list(price="1.0", size="0.01", side="buy", product_id="BTC-USD")
body <- toJSON(order, collapse="")
what <- paste0(timestamp, toupper(method), req.url, body)
sign <- base64Encode(hmac(key, what, algo="sha256", raw=TRUE))
# set headers
httpheader <- list('CB-ACCESS-KEY'=api.key,
'CB-ACCESS-SIGN'=sign,
'CB-ACCESS-TIMESTAMP'=timestamp,
'CB-ACCESS-PASSPHRASE'=passphrase,
'Content-Type'='application/json')
# place an order
(order <- fromJSON(getURLContent(url=paste0(api.url, req.url),
curl=getCurlHandle(useragent="R"),
httpheader=httpheader,
postfields=body)))
@tsulgrove
Copy link

tsulgrove commented Dec 27, 2017

@DheerajAgarwal I am having a 400 error as well, comes up as an invalid signature. Could you post the code that successfully connected? Thanks

@DheerajAgarwal
Copy link

@tsulgrove Here is the link to my repo which is released under MIT. Please use at your own risk. I am currently testing it for CRAN release and hopefully CRAN will accept it without much issues. https://github.com/DheerajAgarwal/rgdax

@DheerajAgarwal
Copy link

rgdax is now available on cran. library(rgdax) should give you all the common functions needed.

@prateekji2016
Copy link

@DheerajAgarwal thanks for making the effort to put together this code. But the bad request error does not seem to go away. Can you please share the exact code you got working, without burying the message in details about rgdax?

@DheerajAgarwal
Copy link

hey @prateekji2016, my complete code with examples is available on CRAN as well as my repo. https://github.com/DheerajAgarwal/rgdax for some time. I believe posting code is only going to make it more confusing as the actual code continues to evolve. Hope you will find what you are looking for.

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