GDAX Interaction with Python
Get GDAX balances using code? Lets do it.
I was checking out the Coinbase GDAX REST API the other day and I immediately had some ideas of how I could use it. I am not a software developer by trade, but even for a scripter like myself, a quick review of the GDAX documentation proved to be understandable. It was time well spent and I recommend you have a gander prior to executing the code in this post.We are going to use Python in this example. Why Python? Because I make the decisions here.
A WORD OF CAUTION: GDAX handles financial assets. Always use the sandbox when testing code and ideas. Use appropriate security measures when using API credentials.
The example below is copied from the GDAX documentation. I then sprinkled a few extra seasonings. You will need to insert your passphrase, key, and secret in the "Credentials" section.Goals
- Securely authenticate to the GDAX REST API
- Get a list of accounts for the authenticated user
- Display the accounts details in prettified format
- Display the execution duration in seconds
Prep
- Login to GDAX and generate an API key. I specifically set my API key with "view" permissions.
- Save the Passphrase, key, and secret in a secure location, such as your favorite password manager.
Python Code
import json, hmac, hashlib, time, requests, base64
from requests.auth import AuthBase
# Tracking execution time
start = time.time()
# Create custom authentication for Exchange
class CoinbaseExchangeAuth(AuthBase):
def __init__(self, api_key, secret_key, passphrase):
self.api_key = api_key
self.secret_key = secret_key
self.passphrase = passphrase
def __call__(self, request):
timestamp = str(time.time())
message = timestamp + request.method + request.path_url + (request.body or '')
hmac_key = base64.b64decode(self.secret_key)
signature = hmac.new(hmac_key, message, hashlib.sha256)
signature_b64 = signature.digest().encode('base64').rstrip('\n')
request.headers.update({
'CB-ACCESS-SIGN': signature_b64,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': self.api_key,
'CB-ACCESS-PASSPHRASE': self.passphrase,
'Content-Type': 'application/json'
})
return request
# Credentials - ADD YOUR API KEY CREDS IN THIS SECTION
API_KEY = "YOUR API KEY"
SECRET_KEY = "YOUR SECRET KEY"
API_PASSPHRASE = "YOUR API PASSPHRASE"
# Get accounts
api_url = 'https://api.gdax.com/'
auth = CoinbaseExchangeAuth(API_KEY,SECRET_KEY,API_PASSPHRASE)
r = requests.get(api_url + 'accounts', auth=auth)
# Output account data and code execution time
print json.dumps(r.json(),indent=4)
print "\nExecution Time: " + str(time.time() - start)
Notes
- My initial testing was done using the Public Sandbox. The code above will connect to the your live account.
- Remember to secure your secret and passphrase after (or even during) testing using bcrypt/scrypt/pbkdf2
- JSON is formatted with indentation for ease of reading
- Execution time is presented in seconds
Results
Success! There is nothing special here folks, but it is a starting point. Some account info has been masked to "protect the innocent".
[
{
"available": "0.0000000000000000",
"balance": "0.0000000000000000",
"profile_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"currency": "USD",
"hold": "0.0000000000000000",
"id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
},
{
"available": "12.9900000000000000",
"balance": "12.9900000000000000",
"profile_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"currency": "LTC",
"hold": "0.0000000000000000",
"id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
},
{
"available": "7.1605267800000000",
"balance": "7.1605267800000000",
"profile_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"currency": "ETH",
"hold": "0.0000000000000000",
"id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
},
{
"available": "0.0000000000000000",
"balance": "0.0000000000000000",
"profile_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"currency": "BTC",
"hold": "0.0000000000000000",
"id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
]
Execution Time: 0.315999984741
And Then?
Next time, we'll play with the Coinbase API to check wallets and transaction history.
Comments
Post a Comment