Skip to content

JavaScript helper functions for CoinGecko API v3 exchange rates

License

Notifications You must be signed in to change notification settings

jasbanza/coingecko-exchange-rates-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 

Repository files navigation

coingecko-exchange-rates-js

Pure JavaScript helper functions for CoinGecko API v3 exchange rates. Makes use of Big.js dependency to ensure accurate conversion.

API endpoint: https://api.coingecko.com/api/v3/exchange_rates

You will notice the CoinGecko API endpoint above returns fiat and major crypto tickers with values quoted against a BTC base

Setup

Add the following script tags to your webpage, and edit the paths according to your directory structure:

<script type="text/javascript" src="./coingecko-exchange-rates-js/lib/big.min.js"></script>
<script type="text/javascript" src="./coingecko-exchange-rates-js/index.js"></script>

This will instantiate the RATES variable in global scope.

Example usage

Getting exchange rates from CoinGecko and doing lookups.

If you plan on only doing a single lookup, use Method 1. If your intentions are to do a lot of lookups, use method 2. This is to ensure the API is only called once, and you won't accidently run into CoinGecko rate limiting.

NB: All functions are asynchronous

  • Method 1 - calling from API each time:
let usd_zar = await RATES.exchange({
  "from": "usd",
  "to": "zar",
  "amount": 1
});

console.log(usd_zar);

// output will be:
{
    "from": {
        "amount": 1,
        "ticker": "usd",
        "name": "US Dollar",
        "unit": "$",
        "type": "fiat"
    },
    "to": {
        "amount": "15.82",
        "ticker": "zar",
        "name": "South African Rand",
        "unit": "R",
        "type": "fiat"
    }
}
  • Method 2 - get rates once, and use multiple times:
var rates = await RATES.get(); // get rates once

console.log(await RATES.exchange({
  "from": "usd",
  "to": "zar",
  "amount": 1,
  "rates": rates    //reuse of rates
}));
console.log(await RATES.exchange({
  "from": "usd",
  "to": "jpy",
  "amount": 1,
  "rates": rates    //reuse of rates
}));
console.log(await RATES.exchange({
  "from": "usd",
  "to": "rup",
  "amount": 1,
  "rates": rates    //reuse of rates
}));

Polling the exchange rates for specific types

You can also query RATES.list(options) with an options object.

let options = {
      "types": ["fiat", "crypto", "commodity", "all"],
      "rates": null
    };

/* `options.types` is an array which can contain any combination of the following:
["fiat", "crypto", "commodity", "all"]

 Omitting `options.types` will result in all types being returned.

 `options.rates` is a previously returned `rates` object.
 Ommitting this will result in the CoinGecko API endpoint being queried.*/


console.log(await RATES.list(options));
// returns object of exchange rates

Releases

No releases published

Packages

No packages published