API Calls in R

You, R
Back

🤘 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

Install Packages

install.packages(c('httr', 'jsonlite'))

Require Packages

library('httr')
library('jsonlite')

Make Request

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

res = GET("https://api.helium.io/v1/stats")
print(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.

data = fromJSON(rawToChar(res$content))
names(data)
[1] "data"

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

data = data$data
names(data)
[1] "token_supply"     "election_times"   "counts"           "challenge_counts" "block_times"

Alternatively, you can loop through the names as follows:

for (name in names(data)){print(name)}
[1] "token_supply"
[1] "election_times"
[1] "counts"
[1] "challenge_counts"
[1] "block_times"

Get the ‘token_supply’ field from the data.

token_supply = data$token_supply
print(token_supply)
[1] 124675821

Adding Parameters to Requests

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

res = GET("https://api.helium.io/v1/dc_burns/sum",
          query = list(min_time = "2020-07-27T00:00:00Z"
                       , max_time = "2021-07-27T00:00:00Z"))

data = fromJSON(rawToChar(res$content))
fee = data$data$fee
print(fee)
[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.

res = GET("https://api.helium.io/v1/dc_burns/sum",
          query = list(min_time = "2020-07-27T00:00:00Z"
                       , max_time = "2021-07-27T00:00:00Z"),
          add_headers(`Accept`='application/json'
                      , `Connection`='keep-live'))

data = fromJSON(rawToChar(res$content))
fee = data$data$fee
print(fee)
[1] 10112755000

R Programming

© Trevor French.RSS