Translate POST call to httr

Hi,
I am trying to connect to the private API of Kraken and I have problems to translate the code to do it with POST from httr.

I will show with a dummy example, get your Balance, from their Postman collections. Of course, this keys are not valid, but will show the problem.

If you execute this example in Postman and/or use their curl counterpart, what you get is EAPI:Invalid nonce message what means that the request is ok:

curl --location --request POST 'https://api.kraken.com/0/private/Balance' \
--header 'API-Key: Nosz8dniQf/O2Nb9eX86ZMRGnjnmlG0M4VOReJE7wP8CPq7AmD8SycH1' \
--header 'API-Sign: QrzFae3O+6vXmC7d3OaQmpekQeLTEsC1YBd9DwA/O+yawid7vS8RvmrTLT4EqEZ+L7LGlN8Dl0O6PqHNm5TaQw==' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'nonce=1611821386727000'

{"error":["EAPI:Invalid nonce"]}

I have translated this call to POST with httr as it is below and the answer that I get is always "EAPI:Invalid key" error and not the previous one. This means that the call is not right.

library(httr)

r <- POST(url = 'https://api.kraken.com/0/private/Balance', 
          config = list(add_headers(.headers = c('API-Key'= 'Nosz8dniQf/O2Nb9eX86ZMRGnjnmlG0M4VOReJE7wP8CPq7AmD8SycH1',
                                                 'API-Sign' = 'QrzFae3O+6vXmC7d3OaQmpekQeLTEsC1YBd9DwA/O+yawid7vS8RvmrTLT4EqEZ+L7LGlN8Dl0O6PqHNm5TaQw=='))),
          body = 'nonce=1611821386727000',
          encode = "form",
          verbose(info = TRUE))

content(r)
#> $error
#> $error[[1]]
#> [1] "EAPI:Invalid key"

Created on 2021-01-28 by the reprex package (v0.3.0)

Session info
devtools::session_info()
#> - Session info ---------------------------------------------------------------
#>  setting  value                       
#>  version  R version 4.0.3 (2020-10-10)
#>  os       Windows 10 x64              
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  Spanish_Spain.1252          
#>  ctype    Spanish_Spain.1252          
#>  tz       Europe/Paris                
#>  date     2021-01-28                  
#> 
#> - Packages -------------------------------------------------------------------
#>  package     * version date       lib source        
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.3)
#>  callr         3.5.1   2020-10-13 [1] CRAN (R 4.0.3)
#>  cli           2.2.0   2020-11-20 [1] CRAN (R 4.0.3)
#>  crayon        1.3.4   2017-09-16 [1] CRAN (R 4.0.3)
#>  curl          4.3     2019-12-02 [1] CRAN (R 4.0.3)
#>  desc          1.2.0   2018-05-01 [1] CRAN (R 4.0.3)
#>  devtools      2.3.2   2020-09-18 [1] CRAN (R 4.0.3)
#>  digest        0.6.27  2020-10-24 [1] CRAN (R 4.0.3)
#>  ellipsis      0.3.1   2020-05-15 [1] CRAN (R 4.0.3)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 4.0.3)
#>  fansi         0.4.2   2021-01-15 [1] CRAN (R 4.0.3)
#>  fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.3)
#>  glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.3)
#>  highr         0.8     2019-03-20 [1] CRAN (R 4.0.3)
#>  htmltools     0.5.1   2021-01-12 [1] CRAN (R 4.0.3)
#>  httr        * 1.4.2   2020-07-20 [1] CRAN (R 4.0.3)
#>  jsonlite      1.7.2   2020-12-09 [1] CRAN (R 4.0.3)
#>  knitr         1.30    2020-09-22 [1] CRAN (R 4.0.3)
#>  lifecycle     0.2.0   2020-03-06 [1] CRAN (R 4.0.3)
#>  magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.0.3)
#>  memoise       1.1.0   2017-04-21 [1] CRAN (R 4.0.3)
#>  pkgbuild      1.2.0   2020-12-15 [1] CRAN (R 4.0.3)
#>  pkgload       1.1.0   2020-05-29 [1] CRAN (R 4.0.3)
#>  prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.0.3)
#>  processx      3.4.5   2020-11-30 [1] CRAN (R 4.0.3)
#>  ps            1.5.0   2020-12-05 [1] CRAN (R 4.0.3)
#>  purrr         0.3.4   2020-04-17 [1] CRAN (R 4.0.3)
#>  R6            2.5.0   2020-10-28 [1] CRAN (R 4.0.3)
#>  remotes       2.2.0   2020-07-21 [1] CRAN (R 4.0.3)
#>  rlang         0.4.10  2020-12-30 [1] CRAN (R 4.0.3)
#>  rmarkdown     2.6     2020-12-14 [1] CRAN (R 4.0.3)
#>  rprojroot     2.0.2   2020-11-15 [1] CRAN (R 4.0.3)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.3)
#>  stringi       1.5.3   2020-09-09 [1] CRAN (R 4.0.3)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.0.3)
#>  testthat      3.0.1   2020-12-17 [1] CRAN (R 4.0.3)
#>  usethis       2.0.0   2020-12-10 [1] CRAN (R 4.0.3)
#>  withr         2.4.0   2021-01-16 [1] CRAN (R 4.0.3)
#>  xfun          0.20    2021-01-06 [1] CRAN (R 4.0.3)
#>  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.3)
#> 
#> [1] C:/Users/avidal/Documents/R/win-library/4.0
#> [2] C:/Program Files/R/R-4.0.3/library

I was trying also with real private keys and the problem is always the same: with Postman I got the real answer while with httr I got EAPI:Invalid key

Is there any other parameter that I miss in the POST call with respect to the curl call?

Thanks

Hi,

Your mistake lies in adding the headers. Here is how you should do it

library(httr)

r <- POST(url = 'https://api.kraken.com/0/private/Balance', 
          add_headers(
            'API-Key'= 'Nosz8dniQf/O2Nb9eX86ZMRGnjnmlG0M4VOReJE7wP8CPq7AmD8SycH1',
            'API-Sign' = 'QrzFae3O+6vXmC7d3OaQmpekQeLTEsC1YBd9DwA/O+yawid7vS8RvmrTLT4EqEZ+L7LGlN8Dl0O6PqHNm5TaQw=='),
          body = "nonce=1611821386727000",
          encode = "form",
          verbose(info = TRUE))

content(r)
#> $error
#> $error[[1]]
#> [1] "EAPI:Invalid nonce"

Created on 2021-01-28 by the reprex package (v0.3.0)

Hope this helps,
PJ

1 Like

To be honest, I also spent way too long on it trying to figure it out haha, it's not an intuitive function I find ... Glad it works now though!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Many thanks @pieterjanvc,
It works perfectly. It was one of the configuration combinations that I didn't already try.