API for GDAX, A service provided by coinbase. (An Unofficial API), (*1)
Quick Use Sections:
* Installation
* Usage Examples
* Api Summary, (*2)
The API methods in the Api class start at "getAccounts". Each method has
basic usage information along with a reference link to the GDAX Api
documentaiton on the internet., (*3)
The ApiInterface class has a cleaner represenation of methods and
parameters without comments., (*4)
License
MIT - MIT License
File: LICENSE, (*5)
Installation
Copy the config.php.example file to config.php and place it in your document
root. Add credentials for private API calls., (*6)
Public API calls will work without API credentials. Get credentials from the gdax site: Login and goto the Api section., (*7)
Composer
composer require mrteye/gdax
Usage Examples
The following three examples show how to use the mrteye\GDAX api. From basic to
Advanced usage examples: public acces, private access, and extending the API
into your own class., (*8)
Working examples are provided with index.php, located in the test folder., (*9)
Example #1 Basic. Public access, no authentication required.
<?php
use mrteye\Gdax\Api as Api;
use mrteye\Gdax\Auth as Auth;
// Get the GDAX API and start making calls.
$gdax = new Api('https://api-public.sandbox.gdax.com');
$products = false;
try {
// Example usage of public calls.
$products = $gdax->getProducts();
$productId = 'BTC-USD';
$productOrderBook = $gdax->getProductOrderBook($productId, $param = [
'level' => 1
]);
$productTrades = $gdax->getProductTrades($productId, $param = [
'before' => 1,
'limit' => 100
]);
} catch (\Exception $e) {
echo $e->getMessage();
echo '
'. print_r($gdax->getError(), true) .'
';
}
if ($products) {
echo 'Products:
'. print_r($products, true) .'
';
} else {
echo 'Something went wrong.';
}
Example #2 Basic. Private access, authentication is required.
// An example config file is provided.
include 'config.php';
use mrteye\Gdax\Api as Api;
use mrteye\Gdax\Auth as Auth;
// Authenticate per GDAX documentation; the time url is optional.
$auth = new Auth(
$config->key,
$config->secret,
$config->pass,
$config->time_url
);
// Get the API and start making calls.
$gdax = new Api($config->api_url, $auth);
$accounts = false;
try {
// Usage examples with some private calls.
$accounts = $gdax->getAccounts();
$account = $gdax->getAccount($accounts[0]->id);
$order = $gdax->createOrder([
// Common Order Parameters
'type' => 'limit',
'side' => 'buy',
'product_id' => 'BTC-USD',
// Limit Order Parameters
'price' => ".01",
'size' => ".01"
]);
$orders = $gdax->getOrders($param = [
'status' => 'open',
'product_id' => '',
'before' => 0,
'after' => 1000,
'limit' => 100
]);
$uuids = $gdax->cancelOrder($orders[0]->id);
$uuids = $gdax->cancelAllOrders($param = [
'product_id' => 'BTC-USD'
]);
} catch (\Exception $e) {
echo '
gdax-private: '. print_r($gdax->getError(), true). '
';
}
if ($accounts) {
echo 'Accounts:
'. print_r($accounts, true) .'
';
}
Example #3 Advanced. Extend the Api class.
<?php
use mrteye\Gdax\Api as Api;
use mrteye\Gdax\Auth as Auth;
use mrteye\Gdax\AppCurl;
class MyBot extends Api {
function __construct($private = false, $config) {
// Create an authentication object if necessary.
$auth = false;
if ($private) {
// TODO: Reminder; define values for key, secret, pass and gdax_time_api.
// These values should be stored in an external file or other source.
// - OR - you could simply hard code them here.
$auth = new Auth(
$config->key,
$config->secret,
$config->pass,
$config->time_url
);
}
// Load the Gdax API.
parent::__construct($config->api_url, $auth);
// Set a different timeout for curl.
$this->curl = new AppCurl(2000);
}
// ~ Add custom methods application methods...
}
// Example usage of the AppGdaxApi class
$gdax = new MyBot(true, $config);
$accounts = false;
// Detail debugging is on by default.
//$gdax->setDebug(true);
try {
// Get all accounts and products.
$accounts = $gdax->getAccounts();
$products = $gdax->getProducts();
} catch (\Exception $e) {
echo $e->getMessage();
// Get debug info.
$errors = $gdax->getError();
}
if ($accounts) {
echo 'Accounts:
'. print_r($accounts, true) .'
';
}
API Summary
All $param properties are associative arrays with either API parameters
or pagination parameters or both. The parameters for each method are documented
in the Api class file, the ApiInterface file, and on the internet at the provided url., (*10)
public function getAccounts()
public function getAccount($accountId)
Get account activity. https://docs.gdax.com/#get-account-history
This API is paginated.
public function getAccountHistory($accountId, $param)
This API is paginated.
public function getAccountHolds($accountId, $param)
public function createOrder($param)
public function cancelOrder($orderId)
public function cancelAllOrders($param)
This API is paginated.
public function getOrders($param)
public function getOrder($orderId)
This API is paginated.
public function getFills($param)
This API is paginated.
public function getFundings($param)
public function repay($param)
public function marginTransfer($param)
public function getPosition()
public function closePosition($param)
public function deposit($param)
public function depositCoinbase($param)
public function withdraw($param)
public function withdrawCoinbase($param)
public function withdrawCrypto($param)
public function getPaymentMethods()
public function getCoinbaseAccounts()
public function createReport($param)
public function getReportStatus($reportId)
public function getTrailingVolume()
public function getProducts()
public function getProductOrderBook($productId, $param)
This API is paginated.
public function getProductTicker($productId)
This API is paginated.
public function getProductTrades($productId, $param)
public function getProductHistoricRates($productId, $param)
public function getProduct24HrStats($productId)
public function getCurrencies()
public function getTime()
Contents
Contributions
Suggestions and code modifications are welcome. Create a pull/merge request, and tell me what you are thinking., (*11)