Retrieves the current exchange rate for a crypto- of fiat currency in Bitcoin

exchange_rate(currency = NULL, max_attempts = 3)

Arguments

currency

(character or NULL): a vector with abbreviated names of the currencies of interest. An up-to-date list of supported currencies (both fiat and cryptocurrencies) can be retrieved with the supported_currencies() function. If an unsupported currency is requested, the call will fail with the respective error message. If currency = NULL (default), the function will return exchange rates for all supported currencies.

max_attempts

(double, positive): specifies the maximum number of attempts to call the CoinGecko API (e.g., if the first call fails for some reason). Additional attempts are implemented with an exponential backoff. Defaults to 3.

Value

If the API call succeeds, the function returns a tibble with the following columns:

  • timestamp (POSIXct): date and time of the API request;

  • currency (character): abbreviated name of the currency;

  • name (character): common name of the currency;

  • price_in_btc (double): price in Bitcoin;

  • type (character): type of the currency ("fiat" or "crypto").

If no data can be retrieved (e.g., because of going over the API rate limit or mis-specifying the query parameters), the function returns nothing (NULL).

Details

This function is based on the public CoinGecko API, which has a rate limit of 10-30 calls per minute. Please keep this limit in mind when developing your applications.

Examples

if (FALSE) {
# get exchange rates for all supported currencies
r1 <- exchange_rate()
print(r1)

# get exchange rates for a set of currencies:
r2 <- exchange_rate(currency = c("usd", "eur", "gbp"))
print(r2)
}