Interfacing with APIs using R: the basics

June 12, 2017

(This article was first published on Revolutions, and kindly contributed to R-bloggers)

While R (and its package ecosystem) provides a wealth of functions for querying and analyzing data, in our cloud-enabled world there's now a plethora of online services with APIs you can use to augment R's capabilities. Many of these APIs use a RESTful interface, which means you will typically send/receive data encoded in the JSON format using HTTP commands.

Fortunately, as Steph Locke explains in her most recent R Quick tip, the process is pretty simple using R:

  1. Obtain an authentication key for using the service 
  2. Find the URL of the API service you wish to use
  3. Convert your input data to JSON format using toJSON in the jsonlite package
  4. Send your data to the API service using the POST function in the httr package. Include your API key using the add_headers function
  5. Extract your results from the API response using the fromJSON function

Steph provides an example of detecting text language using the Microsoft Cognitive Services Text Analytics API, but that basic recipe should work for most APIs. (API keys for other Microsoft Cognitive Services APIs for vision, speech, language, knowledge and search are also available for free use with your Github account.) There many APIs available for use around the web, and this list of public APIs maintained by Abhishek Banthia is also a useful resource.

Locke Data: R Quick tip: Microsoft Cognitive Services’ Text Analytics API



To leave a comment for the author, please follow the link and comment on their blog: Revolutions. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)