Search code examples
rapisha256hmacbinance

How to: New order Binance API via RStudio


I am trying to create a new order via the Binance API using RStudio.

I found the Binance Official API Docs and figured out that I should use

POST /api/v3/order (HMAC SHA256).

The following script doesn't work out for me:

url='https://api.binance.com/api/v3/account'

GET(url, 
    add_headers("X-MBX-APIKEY"= *[my API key]*),
    query=list("symbol"="ETHBTC", 
               "side"="BUY", 
               "type"="MARKET", 
               "quantity"=1, 
               recvWindow=5000, 
               "timestamp"=1499827319559, 
               "signature"=**???**), 
    verbose())

Does anyone know what I'm doing wrong and how I can create an order via the Binance API using RSTUDIO and how I can create my signature?


Solution

  • library(httr)
    
    timestamp <-
      as.character(jsonlite::fromJSON(content(
        GET("https://api.binance.com/api/v1/time"), "text"
      ))$serverTime + 999)
    
    query <-
      list(
        "symbol" = "VENBTC",
        "side" = "BUY",
        "type" = "MARKET",
        "quantity" = 1,
        "recvWindow" = 5000,
        "timestamp" = timestamp
      )
    
    signature <-
      digest::hmac(
        key = "*[my secret key]*",
        object = paste(names(query), query, sep = "=", collapse = "&"),
        algo = "sha256"
      )
    
    POST(
      url,
      add_headers("X-MBX-APIKEY" = "*[my API key]*"),
      query = c(query, signature = signature),
      verbose()
    )