question

thehumanteller avatar image
2 Likes"
thehumanteller asked ·

Credentials to access API in R

I'm trying to access the API using this tutorial, however, it mentions I need credentials before I can pull the data. I couldn't locate any credential information for API on the website. Can anyone help out? Thanks

api
10 |600 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

· Write an Answer
mkelly avatar image
1 Like"
mkelly answered ·

Your API credentials can be found on your Intrinio account page under the API Keys tab. There you will find a free-use sandbox API key that will give you access to test data and a subscription-based production API key that will allow access to any information you subscribed to. The API key is uniquely account-based and is used in any type of APL call. This might help you since it includes most of the major updates we have had since that article was written

#package install
install.packages("httr")
install.packages("jsonlite")
require("httr")
require("jsonlite")

#vairable setting
api_Key <- "YOUR_API_KEY"
base <- "https://api-v2.intrinio.com"
section <- "securities"# str| Sets the API that will be accessed. 
identifier <- "AAPL" # str | A Security identifier (Ticker, FIGI, ISIN, CUSIP, Intrinio ID)
endpoint <- "prices"# str | sets the endpoint that the API will return.
start_date <- "2018-01-01" # date | Return prices on or after the date (optional)
end_date <- "2019-01-01" # date | Return prices on or before the date (optional)
frequency <- "" # str | Return stock prices in the given frequency (optional)
page_size <- 100 # int | The number of results to return (optional) (default to 100)
next_page <- "" # str | Gets the next page of data from a previous API call (optional)
max_pages<-1000 # int | Sets the max number of pages that will be returned to prevent uncontrolled looping
createFrame <- TRUE # boo | Creates the set dataframe to write to in the end.

#looping API call
for (i in 2:max_pages) {
  call_1 <-paste(base, "/", section, "/", identifier, "/", endpoint, "?","start_date=", start_date,"&", "end_date=",
                 end_date,"&","frequency=",frequency,"&", "page_size=", page_size,"&", "next_page","=", next_page, "&", "api_key", "=", api_Key, sep="")
  get_prices <- GET(call_1)
  get_prices_text <-content(get_prices, "text")
  get_prices_json <-fromJSON(get_prices_text, flatten = TRUE)
  next_page <-get_prices_json$next_page
  
  if(is.null(next_page)){
    call_1
    break
  }
  get_prices_df <-as.data.frame(get_prices_json)
  
  if(createFrame){
    get_full_prices_df <- as.data.frame(get_prices_json)
    createFrame <- FALSE
  }
  get_full_prices_df <-rbind(get_full_prices_df, get_prices_df)
}

#display results
View(get_full_prices_df)

· Share
10 |600 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.