Create your own Crypto Apps/Bots with Coingecko's Free API - includes Twitter bot and Android app examples

By Holdman | Crypto Automations | 14 Mar 2021


As I was analysing APIs to get the coin prices and statistics, I found that Coingecko provides this service FREE without any keys or logins necessary.

https://www.coingecko.com/en/api

This can allows us to really automate stuff in crypto like creating portfolio watchers, alerts, automatic tweets, price checkers, discord bots, etc.

Examples of stuff you can do with the API:

Android App: WOW - A dogecoin price tracker
bba437e595a2bbe0218966f40c0576e7f6049c51b1b489424738c50d210e3df0.png


See it live

https://play.google.com/store/apps/details?id=such.wow.cogecointracker


Relevant code using API (Java/Android)

refreshCoin('dogecoin', 'eur');
  
public void refreshCoin(String coin_id, String coin_currency) {
        ...
        Ion.with(this)
                .load("https://api.coingecko.com/api/v3/coins/"+ coin_id)
                .setHeader("Content-Type","application/json")
                .asJsonObject()
                .setCallback(new FutureCallback<JsonObject>() {
                    @Override
                    public void onCompleted(Exception e, JsonObject result) {
                        if(result!=null && result.getAsJsonObject("market_data")!=null) {
                            JsonObject marketData = result.getAsJsonObject("market_data");
                            JsonObject currentPrice = marketData.getAsJsonObject("current_price");

                            JsonObject ath = marketData.getAsJsonObject("ath");
                            String price_change_percentage_24h = marketData.get("price_change_percentage_24h").toString();
                            String price_change_percentage_7d = marketData.get("price_change_percentage_7d").toString();
                            String price_change_percentage_30d = marketData.get("price_change_percentage_30d").toString();
                            String price_change_percentage_1y = marketData.get("price_change_percentage_1y").toString();
                            String CurrentPriceInCurrency = Float.parseFloat(currentPrice.get(coin_currency).toString())
                            String athWithInCurrency = Float.parseFloat(ath.get(coin_currency).toString());
          ...
}


API URL used

https://api.coingecko.com/api/v3/coins/dogecoin 

 

Twitter Bot: All time High alerts (every 15min)

ecd52d197c761dda785c01e9c3dda8f0dc632618e19ca80d494db9c9433e3687.png

See it live

https://twitter.com/GuyInnaChair 

Relevant code using API (PHP/TwitterAPI)

<?php 
require_once("vendor/autoload.php"); 

//download here: https://github.com/dg/twitter-php
use DG\Twitter\Twitter;

//Display errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

//You can get this info here: https://developer.twitter.com/en/portal/dashboard
$consumerKey = 'XXX';
$consumerSecret = 'XXX';
$accessToken = 'XXX';
$accessTokenSecret = 'XXX';

$twitter = new Twitter($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);

$coinsToAnalyze = array('bitcoin', 'dogecoin', 'ethereum', 'chainlink', 'polkadot', 'cardano', 'stellar', 'ripple', 'binancecoin', 'litecoin', 'filecoin', 'vechain','algorand','the-graph');

foreach($coinsToAnalyze as $coin) {
    $coinData = analyzeDataFromAPI(getDataFromCoin($coin));
    //Should be the same as cronjob (every 15 min)
    if(wasAllTimeHighLast('-15 minutes', $coinData['ath_date'])) {
        $msg = "🙃 New all time high on ".'$'.strtoupper($coinData['symbol'])." $".$coinData['ath'];
        tweet($twitter, $msg);
    }
    sleep (1); //let API sleep a bit
}

function wasAllTimeHighLast($dateIntervalString, $athOfTheCoinDate) {
    $date_last_x_min = strtotime($dateIntervalString);
    $ath_coin = strtotime($athOfTheCoinDate);
    return $ath_coin>$date_last_x_min;
}

function analyzeDataFromAPI($apiObj) {
    return array('symbol' => $apiObj->symbol,
                 'current_price' => $apiObj->market_data->current_price->usd,
                 'ath' => $apiObj->market_data->ath->usd,
                 'ath_change_percent' => $apiObj->market_data->ath_change_percentage->usd,
                 'ath_date' => $apiObj->market_data->ath_date->usd,
                 'price_change_percentage_24h' => $apiObj->market_data->price_change_percentage_24h,
                 'price_change_percentage_7d' => $apiObj->market_data->price_change_percentage_7d,
                 'price_change_percentage_14d' => $apiObj->market_data->price_change_percentage_14d,
                 'price_change_percentage_30d' => $apiObj->market_data->price_change_percentage_30d,
                 'price_change_percentage_60d' => $apiObj->market_data->price_change_percentage_60d,
                 'price_change_percentage_200d' => $apiObj->market_data->price_change_percentage_200d,
                 'price_change_percentage_1y' => $apiObj->market_data->price_change_percentage_1y,
                );
}

function tweet($twitter, $msg) {
    try {
        $twitter->send($msg);
    } catch (DG\Twitter\Exception $e) {
        echo "Error: ", $e->getMessage();
    }
}

function getDataFromCoin($coinname) {
    $url = 'https://api.coingecko.com/api/v3/coins/' . $coinname;
    return json_decode(curl($url));
}

function curl($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);     
    return $output;
}


API URL used

https://api.coingecko.com/api/v3/coins/dogecoin 
https://api.coingecko.com/api/v3/coins/bitcoin 
https://api.coingecko.com/api/v3/coins/ethereum 
https://api.coingecko.com/api/v3/coins/chainlink 
https://api.coingecko.com/api/v3/coins/polkadot 
https://api.coingecko.com/api/v3/coins/binancecoin 
https://api.coingecko.com/api/v3/coins/cardano 
https://api.coingecko.com/api/v3/coins/stellar 
https://api.coingecko.com/api/v3/coins/ripple 

Future projects

  • Discord Bot with price alerts and on demand requests
  • Telegram Bot
  • Image Generation

How do you rate this article?

4


Holdman
Holdman

I'm a fullstack developer with a strong passion for automation and crypto (especially $BAN)


Crypto Automations
Crypto Automations

Join me in my adventure on creating automations, bots and miners for crypto currencies

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.