Continued from page 1.
As an example, you'll make a request for stock prices to the Intrinio API. You can get your Intrinio API password and username here. Note that with Intrinio, the username and password that you supply to the API are not the same as the username and password that you used to login to Intrinio.com. They can be found under the Access Keys heading of the account area on the Intrinio website.
To initialize the variables for username and password, enter the following lines into your RStudio script or the R console:
username <- "Paste_API_Username_Here" password <- "Paste_API_Password_Here"
Next, initialize variables for the API call you'd like to make. Once you've pasted this example all together, you'll be able to retrieve stock prices for Apple:
base <- "https://api.intrinio.com/" endpoint <- "prices" stock <- "AAPL" call1 <- paste(base,endpoint,"?","ticker","=", stock, sep="")
Figure 3. You will need to enter your own username and password. Print call1 to see the full API call.
Now that you have your username and password, as well as the API URL that specifies what data you'd like to see, you're ready to pass those objects to the GET function of httr:
get_prices <- GET(call1, authenticate(username,password, type = "basic")) Deserializing The API's Response
When an API responds to a request, the act of formatting the data for transmission in the response is called "serialization." When the response is received on the other end, the application that made the original request must deserialize the payload. In this example, the response to the sample API call is a list. The list has many different items with most of it being administrative information from the API, not the data you want. Make sure you understand this information because you'll need some of it later. But to get the data you want, you'll want to use another httr function to start the process of deserialization:
get_prices_text <- content(get_prices, "text")
The, enter the following to display the contents of the newly loaded variable:
Figure 4. A status of 200 means the API call was successful. The content function with a "text" parameter
This converts the raw data from your API call into JSON format. Then you can parse the JSON using the jsonlite package (which you installed earlier):
get_prices_json <- fromJSON(get_prices_text, flatten = TRUE)
Finally, you can then convert the parsed JSON to a data frame for analysis:
get_prices_df <- as.data.frame(get_prices_json)
And then to display it like a spreadsheet:
Figure 5. Converting the JSON to a data frame reveals the data in a great format for analyses in R.
How to Page Through a Data Set with R
For APIs without paging limits, once you have the response to a GET request parsed in R, you're done. Many APIs, however, put a limit on the number of results you can get with a single API call. In the example above, the paging limit is 100, so you only pulled in the first 100 days of historical stock prices. Each API sets its own limits and each API has a different "pager" that lets you create loops to get the rest of the pages.