
/*
 * Copyright 2014-2026 GPLv3, Open Crypto Tracker by Mike Kilday: Mike@DragonFrugal.com (leave this copyright / attribution intact in ALL forks / copies!)
 */
 

This app has a built-in (internal) REST API available, so other external apps can connect to it and receive market data, including market conversion (converting the market values to their equivalent value in country fiat currencies and secondary cryptocurrency market pairs).


To see a list of the supported assets in the API, use the endpoint:

/api/asset_list


To see a list of the supported exchanges in the API, use the endpoint:

/api/exchange_list


To see a list of the supported markets for a particular exchange in the API, use the endpoint:

/api/market_list/[exchange name]


To see a list of the supported conversion currencies (market values converted to these currency values) in the API, use the endpoint:

/api/conversion_list


To get raw market values AND also get a market conversion to a supported conversion currency (see ALL requested market values also converted to values in this currency) in the API, use the endpoint:

/api/market_conversion/[conversion currency]/[exchange1-asset1-pair1],[exchange2-asset2-pair2],[exchange3-asset3-pair3]


To skip conversions and just receive raw market values in the API, you can use the endpoint:

/api/market_conversion/market_only/[exchange1-asset1-pair1],[exchange2-asset2-pair2],[exchange3-asset3-pair3]


For security, the API requires a key / token to access it. This key must be named "api_key", and must be sent with the "POST" data method.


IMPORTANT REST API NOTES: 

The 'Linux Desktop Edition' of this app has a slightly different endpoint format for the internal REST API (due to unavoidable feature restrictions). Login to your Admin Config area, and see the 'Internal API / Webhook' section, for the endpoint and example code required to use the internal REST API.


Below are examples (NOT INCLUDING your web address and auto-generated login authentication tokens), of connecting an external app with CURL command line or PHP, and Javascript...


THE API ADMIN SECTION IN THE APP GIVES YOU CUSTOMIZED CODE EXAMPLES (AUTO-INCLUDING ANY HTACCESS USER / PASS CODING REQUIRED, AND YOUR  WEB ADDRESS / LOGIN CREDENTIALS), SO IT'S READY TO RUN "OUT-OF-THE-BOX".


///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////


# CURL command line example
# Add --insecure to the command, if your app's SSL certificate
# is SELF-SIGNED (not CA issued), #OR THE COMMAND WON'T WORK#
# WINDOWS USERS: REMOVE THE "Invoke-WebRequest" CURL ALIAS FIRST: Remove-item alias:curl

curl -d "api_key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" -X POST https://YOUR-WEB-ADDRESS.COM/api/market_conversion/eur/kraken-btc-usd,coinbase-eth-usd,binance_us-sol-usdc


///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////


// Javascript example
// API request (API results sent to console log)

var api_request = new XMLHttpRequest();

api_request.open("POST", "https://YOUR-WEB-ADDRESS.COM/api/market_conversion/eur/kraken-btc-usd,coinbase-eth-usd,binance_us-sol-usdc", true);

var params = "api_key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

api_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

	api_request.onload = function () {
	console.log("api_request = " + api_request.responseText);
	};

api_request.send(params);


///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////


// CURL PHP example (requires CURL PHP module)

// PHP version
if (!defined('PHP_VERSION_ID')) {
$version = explode('.', PHP_VERSION);
define('PHP_VERSION_ID', ($version[0] * 10000 + $version[1] * 100 + $version[2]));
}

// Curl version
if ( function_exists('curl_version') ) {
$curl_setup = curl_version();
define('CURL_VERSION_ID', str_replace(".", "", $curl_setup["version"]) );
}
else {
echo 'CURL module for PHP required.';
exit;
}


// Initiate CURL
$ch = curl_init('https://YOUR-WEB-ADDRESS.COM/api/market_conversion/eur/kraken-btc-usd,coinbase-eth-usd,binance_us-sol-usdc');

$params = array('api_key' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params) ); // Encode post data with http_build_query()

// Timeout in seconds (so we don't hang if API is not responsive)
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);

// Strict SSL connections, or not? (see notes next to settings)
// If your app's SSL certificate is SELF-SIGNED (not CA issued), 
// #CHANGE ALL OF THESE# to false / 0, #OR THE API REQUEST WILL FAIL#
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, true); // SET TO false to skip verifying the peer's SSL certificate
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 2); // SET TO 0 to skip verifying the certificate's name against the host 
if ( PHP_VERSION_ID >= 70700 && CURL_VERSION_ID >= 7410 ) {
curl_setopt ($ch, CURLOPT_SSL_VERIFYSTATUS, true); // SET TO false to skip verifying the SSL certificate's status
}


// $json_api_data contains the API response, in JSON format
$json_api_data = curl_exec($ch);
curl_close($ch);

// JSON data converted to a PHP array
$api_data_array = json_decode($json_api_data, true);

// Print out the array data on screen for developing / etc
var_dump($api_data_array);


///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////




