How to Access Any RESTful API Using the R Language

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.

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="")
 You will need to enter your own username and password. Print call1 to see the full API call.

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:

ces_text
Figure 4. A status of 200 means the API call was successful. The content function with a ''text'' parameter converts the raw data to JSON.

Figure 4. A status of 200 means the API call was successful. The content function with a "text" parameter converts the raw data to JSON.

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:

View(get_prices_df)
Figure 5. Converting the JSON to a data frame reveals the data in a great format for analyses in R.

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.

Andrew Carpenter I'm an author, I/O Psychologist, and budding data analyst. When I'm not living in a fantasy world, I'm at Intrinio trying to redefine HR Analytics. Intrinio's mission is to make financial data affordable and accessible so investors can save money and make time to build something meaningful. You can view examples of books I've written here: https://www.amazon.com/Roll-Honey-Andrew-James-Carpenter/dp/1537174711?_encoding=UTF8&psc=1 https://www.amazon.com/Blood-Born-Saga-Book-ebook/dp/B00GM8WYIK/ref=sr_1_1?ie=UTF8&qid=1495128022&sr=8-1&keywords=the+blood+born+andrew+carpenter
 

Comments (3)

srinivas-kasiboyina

Sir, Thank you very much. I have gained a lot on APIs in R from your Blog  Post.

Thanks a lot.

Srinivas K

Andrew Carpenter

Srinivas,

Glad you liked it. I spent a lot of time learning how to do it the first time but now that I know how to make 1 API call, every other API is pretty easy. 

It makes me happy that other R users are learning from this. When I was learning, articles and posts were supper helpful, so its nice to give back.

Andrew

Saravana kumar

Hi,

I have a web scrapper code in R which crawls data from API and feed those data to MS SQL SERVER for further analysis.

Each and every time if I run the code it retrieves all the data from API and feed it to database but I want to pull only newly added data in API and feed those into database to avoid API transaction overflow.

Is there any way to do so inside the R code or any add-in available?

Yours answers would be much appreciated.

Cheers, Saravana,Email: sarajki333@gmail.com