Simple Dart wrapper for the CoinGecko API

popularity likes

Table of contents

Installation

Add the dependency to your Dart / Flutter project:

dependencies:
  coingecko_api: ^1.1.0

Usage

  1. Create an instance of the API wrapper:
     CoinGeckoApi api = CoinGeckoApi();
    
  2. Use any methods provided by wrapper. The methods are divided into sections, just as the CoinGecko does.
     final marketChart = await api.coins.getCoinMarketChart(
       id: 'bitcoin',
       vsCurrency: 'usd',
       days: 7,
     );
    
  3. Work with the result as you see fit:
     if (!marketChart.isError) {
       marketChart.data.forEach((item) {
         print('Price: ${item.price}, Market Cap: ${item.marketCap}');
       });
     }
    

Documentation

All methods are asynchronous and use Future, so you need to use await to get the result.

Query results

The result of each method is a special object of type CoinGeckoResult. This is a kind of wrapper that allows you to first find out how successful the request was using its isError property. If isError is false, then we can use the data property of this object to get the processed data itself. Otherwise, we can view the error code and error message using properties errorCode and errorMessage.

Sections

All methods are divided into sections for easier understanding of the purpose of each method. To call any method, we first need to specify a section.

General scheme:

final result = await api./*section name*/./*method name*/(/*parameters*/);

All sections fully correspond to those in the CoinGecko API documentation itself: https://www.coingecko.com/api/documentation

Let's take a closer look at each of the sections and a set of methods in them. Each section will contain a table indicating the method, a short description of the method and the corresponding query string from the official CoinGecko website.

ping

Method nameDescriptionQuery string
pingCheck API server status./ping

simple

Method nameDescriptionQuery string
listPricesGet the current price of any cryptocurrencies in any other supported currencies that you need./simple/price
listTokenPricesGet current price of tokens (using contract addresses) for a given platform in any other currency that you need./simple/token_price/{id}
listSupportedVsCurrenciesGet list of supported vs currencies./simple/supported_vs_currencies

coins

Method nameDescriptionQuery string
listCoinsList all supported coins id, name and symbol./coins/list
listCoinMarketsList all supported coins price, market cap, volume, and market related data./coins/markets
getCoinDataGet current data (name, price, market, ... including exchange tickers) for a coin./coins/{id}
listCoinTickersGet coin tickers (paginated to 100 items)./coins/{id}/tickers
getCoinHistoryGet historical data (name, price, market, stats) at a given date for a coin./coins/{id}/history
getCoinMarketChartGet historical market data include price, market cap, and 24h volume (granularity auto)./coins/{id}/market_chart
getCoinMarketChartRangedGet historical market data include price, market cap, and 24h volume within a range of timestamp (granularity auto)./coins/{id}/market_chart/range
listCoinStatusUpdatesGet status updates for a given coin./coins/{id}/status_updates
getCoinOHLCGet coin's OHLC./coins/{id}/ohlc

contract

Method nameDescriptionQuery string
getContractTokenDataGet coin info from contract address./coins/{id}/contract/{contract_address}
getContractMarketChartGet historical market data include price, market cap, and 24h volume (granularity auto) from a contract address./coins/{id}/contract/{contract_address}/market_chart
getContractMarketChartRangedGet historical market data include price, market cap, and 24h volume within a range of timestamp (granularity auto) from a contract address./coins/{id}/contract/{contract_address}/market_chart/range

asset_platforms

Method nameDescriptionQuery string
listAssetPlatformsList all asset platforms (Blockchain networks)./asset_platforms

categories

Method nameDescriptionQuery string
listCategoriesShortList all categories./coins/categories/list
listCategoriesList all categories with market data./coins/categories

exchanges

Method nameDescriptionQuery string
listExchangesList all exchanges./exchanges
listExchangesShortList all supported exchanges: id and name./exchanges/list
getExchangeDataGet exchange volume in BTC and top 100 tickers only./exchanges/{id}
getExchangeTickersGet exchange tickers (paginated, 100 tickers per page)./exchanges/{id}/tickers
getExchangeVolumeChartDataGet volume_chart data for a given exchange./exchanges/{id}/volume_chart

indexes

Method nameDescriptionQuery string
listMarketIndexesList all market indexes./indexes
getMarketIndexGet market index by market id and index id./indexes/{market_id}/{id}
listMarketIndexesShortList market indexes id and name./indexes/list

derivatives

Method nameDescriptionQuery string
listDerivativesList all derivative tickers./derivatives
listDerivativeExchangesList all derivative exchanges./derivatives/exchanges
getDerivativeExchangeShow derivative exchange data./derivatives/exchanges/{id}
listDerivativeExchangesShortList all derivative exchanges name and identifier./derivatives/exchanges/list

exchange_rates

Method nameDescriptionQuery string
getBtcExchangeRatesGet BTC-to-Currency exchange rates./exchange_rates

search

Method nameDescriptionQuery string
searchForSearch for coins, categories and markets listed on CoinGecko ordered by largest Market Cap first./search

trending

Method nameDescriptionQuery string
getSearchTrendingGet trending search coins (Top-7) on CoinGecko in the last 24 hours./search/trending

global

Method nameDescriptionQuery string
getGlobalDataGet cryptocurrency global data./global
getGlobalDefiDataGet cryptocurrency global decentralized finance(defi) data./global/decentralized_finance_defi

companies

Method nameDescriptionQuery string
getCompaniesDataGet public companies data./companies/public_treasury/{coin_id}

Full Example

final result = await api.coins.getCoinOHLC(
    id: 'bitcoin',
    vsCurrency: 'usd',
    days: 7,
);
if (!result.isError) {
    print('getCoinOHLC method returned result');
    result.data.forEach(
        (item) => print(
            '${item.timestamp}: open = ${item.open}, high = ${item.high}, low = ${item.low}, close = ${item.close}',
        ),
    );
} else {
    print('getCoinOHLC method returned error ${result.errorCode}: ${result.errorMessage}');
}

Tests

Each method is covered by a separate test using native dart testing platform dart test.

Roadmap

See the open issues for a list of proposed features and known issues.

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the project
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Yegor Pelykh

E-mail: yegor.dev@gmail.com

Project Link: github.com/yegor-pelykh/coingecko_api

Libraries

asset_platform
asset_platforms_section
categories_section
category
category_short
coin
coin_community_data
coin_developer_data
coin_developer_data_code_changes
coin_history
coin_market_data
coin_public_interests_stats
coin_repos_url
coin_short
coingecko_api
Provides simple access to the CoinGecko API (Version 3)
coingecko_rate_limit_exception
coingecko_result
coins_section
companies_data
companies_section
company
contract_section
convert
derivative
derivative_exchange
derivative_exchange_extended
derivative_exchange_short
derivative_ticker
derivatives_section
enumerations
exchange
exchange_extended
exchange_market
exchange_rate
exchange_rates_section
exchange_short
exchange_volume_data
exchanges_section
global_coin_data
global_defi_data
global_section
helpers
image_info
indexes_section
localized_string
market
market_chart_data
market_data
market_index
market_index_identified
market_index_short
market_sparkline
ohlc_info
ping_section
price_info
search_results
search_section
simple_section
status_update
status_update_project
ticker
time_unit