Connecting to Rest API in R with a key

This is a simple question, but I still can't figure it out. I want to connect to REST API using my API key. I've looked at the documentation for httr

, jsonlite

and others, but still can't figure out how to set the API key.

Is this the endpoint - https://api.tiingo.com/tiingo/daily//prices?startDate=2012-1-1&endDate=2016-1-1?

I have tried using a function GET

on this url and in my call specify my API key as key

. I have also tried api_key = key

. I always get 401 error.

thank

+3


source to share


2 answers


The API expects a header Authorization

with Token yOuRAsSiGnEdT0k3n

. You have to store the token in something like an environment variable so it doesn't get stuck in scripts. I used TIINGO_TOKEN

and put it in ~/.Renviron

.

You can make a helper function to make calls less mutant:

library(httr)
library(jsonlite)
library(tidyverse)
library(hrbrthemes)

get_prices <- function(ticker, start_date, end_date, token=Sys.getenv("TIINGO_TOKEN")) {

  GET(
    url = sprintf("https://api.tiingo.com/tiingo/daily/%s/prices", ticker),
    query = list(
      startDate = start_date,
      endDate = end_date
    ),
    content_type_json(),
    add_headers(`Authorization` = sprintf("Token %s", token))
  ) -> res

  stop_for_status(res)

  content(res, as="text", encoding="UTF-8") %>%
    fromJSON(flatten=TRUE) %>%
    as_tibble() %>%
    readr::type_convert()

}

      

Now you can just pass parameters:

xdf <- get_prices("googl", "2012-1-1", "2016-1-1")

glimpse(xdf)
## Observations: 1,006
## Variables: 13
## $ date        <dttm> 2012-01-03, 2012-01-04, 2012-01-05, 2012-01-06, 2...
## $ close       <dbl> 665.41, 668.28, 659.01, 650.02, 622.46, 623.14, 62...
## $ high        <dbl> 668.15, 670.25, 663.97, 660.00, 647.00, 633.80, 62...
## $ low         <dbl> 652.3700, 660.6200, 656.2300, 649.7900, 621.2300, ...
## $ open        <dbl> 652.94, 665.03, 662.13, 659.15, 646.50, 629.75, 62...
## $ volume      <int> 7345600, 5722200, 6559200, 5380400, 11633500, 8782...
## $ adjClose    <dbl> 333.7352, 335.1747, 330.5253, 326.0164, 312.1937, ...
## $ adjHigh     <dbl> 335.1095, 336.1627, 333.0130, 331.0218, 324.5017, ...
## $ adjLow      <dbl> 327.1950, 331.3328, 329.1310, 325.9010, 311.5768, ...
## $ adjOpen     <dbl> 327.4809, 333.5446, 332.0901, 330.5955, 324.2509, ...
## $ adjVolume   <int> 3676476, 2863963, 3282882, 2692892, 5822572, 43955...
## $ divCash     <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ splitFactor <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,...

      



And, it "just works":

ggplot(xdf, aes(date, close)) +
  geom_segment(aes(xend=date, yend=0), size=0.25) +
  scale_y_comma() +
  theme_ipsum_rc(grid="Y")

      

enter image description here

You can follow this idiom for other API endpoints. When this is done, consider making a package out of it so the community can use what you got.

You can take a few extra steps and actually execute functions that take dates or numeric parameters to actually take these types of R objects and check them in the input too.

+6


source


Authorization needs to be added as request header when calling REST api. If you have a username and password, the authorization header will be "username: password". If it is one apiKey or accessToken, it could be something like "Bearer" Replace with your Api Key.



Request header name must be "Authorization"

0


source







All Articles