Site icon R-bloggers

API Calls in R

[This article was first published on R in Trevor French on Medium, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

🤘 Here’s a quick guide on making API calls in R featuring Helium data.

Outline

Install Packages
Require Packages
Make Request
Parse & Explore Data
Adding Parameters to Requests
Adding Headers to Requests

Install Packages

Require Packages

Make Request

Pass a URL into the ‘GET’ function and store the response in a variable called ‘res’.

Response [https://api.helium.io/v1/stats]
  Date: 2022-08-04 01:25
  Status: 200
  Content-Type: application/json; charset=utf-8
  Size: 922 B

Parse & Explore Data

Use the ‘fromJSON’ function from the ‘jsonlite’ package to parse the response data and then print out the names in the resulting data set.

[1] "data"

Go one level deeper into the data set and print out the names again.

[1] "token_supply"     "election_times"   "counts"           "challenge_counts" "block_times"

Alternatively, you can loop through the names as follows:

[1] "token_supply"
[1] "election_times"
[1] "counts"
[1] "challenge_counts"
[1] "block_times"

Get the ‘token_supply’ field from the data.

[1] 124675821

Adding Parameters to Requests

Add ‘min_time’ and ‘max_time’ as parameters on a different endpoint and print the resulting ‘fee’ data.

[1] 10112755000

Adding Headers to Requests

Execute the same query as above except this time specify headers. This will likely be necessary when working with an API which requires an API Key.

[1] 10112755000
R Programming

API Calls in R was originally published in Trevor French on Medium, where people are continuing the conversation by highlighting and responding to this story.

To leave a comment for the author, please follow the link and comment on their blog: R in Trevor French on Medium.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.