How to make API Calls From Coingecko on R

1. Load Necessary Packages

Cem Yilmaz
2 min readJan 30, 2021

--

To make an API call to coingecko, we need only two packages, “httr” and “rlist”. The get() function exists in the package “httr” which is used to get data from the URL of the data source. As we’re working with JSON file types, we need a function which transform the data to R like data structures. This function is taken from “rlist” package.

library(httr) ##Where GET() function resides
library(rlist) ##to transform data from list to df

2. Visit Coingecko and Grab URL

The link to coingecko API page is here. Simply, find a data point that interests you. In the below screenshot, you can find the “coins” section. This provides you with a list of all supported coin ids and their symbol. Then, click on execute which will produce the URL link. The URL that is required is under heading “Request URL”. Grab this one and go to your R session.

Example API call from site

3. Extract Data and Turn into Dataframe

Now lets use the “GET” function which we loaded from “httr” and place the URL inside the argument. Next, we need to unpack the content and turn it into a list. And finally, by using list.stack function from “rlist” we can make it look like something a little more familiar in R, the dataframe!


get_request <- GET(“https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&category=decentralized_finance_defi&order=market_cap_desc&per_page=250&page=1&sparkline=false")
#Convert it to list
list <- content(get_request)
coins <- list.stack(list) ##turn into dataframe

And that's it. Have fun.

--

--