Search code examples
rapihttrtradier

Issue with API call to Tradier using R with httr package


I'm trying to make an API call to tradier API, but get 401 error. Here's the link to their API: https://developer.tradier.com/documentation/markets/get-quotes

And it's my simple code:

library(httr)
tradier <- "https://api.tradier.com/v1/markets/history?symbol=AAPL"
getdata<-GET(url=tradier, add_headers(Authorization="Bearer XXXXXXXXXXXXXXXXX"))
getdata

Solution

  • The sandbox API endpoint works for me. I guess for the brokerage API endpoint you have to upgrade your account.

    library(magrittr)
    library(httr)
    
    token <- keyring::key_get("tradier_token")
    url <- "https://sandbox.tradier.com/v1/markets/quotes?symbols=AAPL,MSFT"
    resp <- GET(url = url, add_headers(Authorization = paste("Bearer", token)))
    content(resp)[[1]] %>%
      data.table::rbindlist()
    #>               V1             V2
    #>  1:         AAPL           MSFT
    #>  2:    Apple Inc Microsoft Corp
    #>  3:            Q              Q
    #>  4:        stock          stock
    #>  5:       217.66         114.26
    #>  6:        -2.38            0.7
    #>  7:        -1.08           0.61
    #>  8:     96246748       71229698
    #>  9:     26803955       24017088
    #> 10:     33973603       21492554
    #> 11:  1.53756e+12    1.53756e+12
    #> 12:       220.78            114
    #> 13:       221.36         115.29
    #> 14:       217.29         113.51
    #> 15:       217.66         114.26
    #> 16:       220.03         113.57
    #> 17:       229.67          113.8
    #> 18:       149.16          72.92
    #> 19:       217.75         113.81
    #> 20:            1              9
    #> 21:            K              P
    #> 22: 1.537574e+12   1.537574e+12
    #> 23:       218.02         113.95
    #> 24:            2              3
    #> 25:            P              P
    #> 26: 1.537574e+12   1.537574e+12
    #> 27:         AAPL           MSFT
    #>               V1             V2
    

    Created on 2018-09-23 by the reprex package (v0.2.1)