Since this article was written, Intrinio has made some changes that renders this information slightly out of date. API authentication and response paging have had upgrades and an R SDK is in the development process. The current process of retrieving data is similar to what is written in the article and follows the same steps with slight changes made to the upgraded sections mentioned before. For complete clarity the article will be reiterated with the appropriate changes. Once again it is suggested that you follow along line by line in RStudio.

In version 2, use an API key to grant access to data instead of the username/password system. This is the standard API access method and is more convenient for user access since all required information can be included in a single URL while maintaining the safety of version 1. 

The upgrade made to the API changed paging from a numeric system to a more effective string system that has increased performance with only a slight change to the user’s interaction with the API. Within the returned object is a string, that when placed back into the API call returns the next page of information, making the backend processing of different page sizes faster and more reliable. 

The R SDK in development at Intrinio will have well documented endpoints similar to the other language SDKs we support. For more information on our supported SDK libraries, visit our documentation page and check out examples. 

Here is an outline of what is required:

  1. Install the "httr" and "jsonlite" packages.
  2. Format data into a URL compatible with the API.
  3. Make a "GET" request to the API.
  4. Format the raw data into JSON formatting.
  5. Set the retrieved data into a data frame.
  6. Combine all of the above steps into a script that can use a loop to retrieve multiple pages.

Current process

Begin by using install and require on the httr and jsonlite packages.

install.packages(“httr”)
require(“httr”)

install.packages(“jsonlite”)
require(“jsonlite”)


The use of an API key eliminates the need for an API username and API password. The API key is made part of the URL leaving the header for other parameters which simplifies the user experience. For an endpoint such as Stock Prices, you will need to retrieve your API key found under the API Keys section of the Intrinio account page. 

Initialize a single variable to store the API key.

api_key <- “YOUR_API_KEY”


Once your API key is saved, prepare the sections of the API call you would like to make as follows.

base <- "https://api-v2.intrinio.com"
section <- "securities"
identifier <- "AAPL"
endpoint <- “prices”


Information about Intrinio’s other web API parameters can be found on the endpoint documentation page. These additional parameters can be added directly to the URL between the endpoint and the API key. Parameters are delineated with "&" and include page size and next page, which will be covered toward the end of this tutorial.

After all required variables are set, place them in the URL. 

call1 <-paste(base, "/", section, "/", stock, "/", endpoint, "?”, “api_key”, ”=", api_key, sep = "")


Authentication happens within the URL itself, so the GET command is made simple. 

get_prices <- GET(call1)


You can tell if the URL receives a healthy response if you get a return value of 200. You can also print the response to ensure that the correct information has been returned. 

get_prices


The data in its raw form is not conducive to rationalization, but it can be formatted and placed into a data-frame using the JSON package that was installed at the beginning.

get_prices_text <- content(get_prices, "text")

get_prices_json <- fromJSON(get_prices_text, flatten = TRUE)

get_prices_df <- as.data.frame(get_prices_json)


R studio is able to display a JSON formatted data table directly in the IDE. 

view(get_prices_df)

 
As mentioned before, the string paging system is faster and more reliable than the numeric paging system used in version 1. We have examples of the next_page string in use in our other supported languages are shown here. Retrieval of the paging string is performed with the “$” operator and saved for later use.

next_page <- get_prices_json$next_page


Finally, you loop through the same process and break on either a set number of returned calls, or a null pointer for a next_page response. If you want to display all the data in a data frame you will need to create two, one for displaying the full data set and one for transferring each page of data into the data frame. Both can be created within the loop compactly using an if statement. 

Below is a compact script that can perform each step mentioned in this article.

#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)

Note that the data frame at the end will not be entirely complete due to the added securities information that the API returns not fitting into the data frame. However, this data can still be accessed within the get_prices_json variable.

Conclusion 

Intrinio currently has an R SDK in progress which will join our five other supported languages in easing access to our endpoint library. Information about our currently supported languages can be found on the documentation page, or you can chat with our knowledgeable support staff. If you have anything to add or want to engage with the community, visit the community forum.

Did this answer your question?