Coinbase Interaction with Python

Get Coinbase balances using code? Lets do it. 

Let's build on the experience from my previous post on connecting to GDAX with Python. If I can connect to both my Coinbase Wallets and my GDAX accounts... I will be well on my way to understanding my cryptocurrency portfolio in ways that are not provided in the web UI.

The good news is that there is a official python Coinbase module.

A WORD OF CAUTIONCoinbase handles financial assets. Always use the sandbox when testing code and ideas. Its never a good idea to put secrets in your code. Use appropriate security measures when using API credentials. I recommend using a tool such as bcrypt to protect your secrets.

Goals

  • Securely authenticate to Coinbase REST service using official Coinbase module
  • Get a list of accounts/wallets for the authenticated user
  • Get a list of all transactions for all accounts
  • Display the results
  • Display the execution duration in seconds

Prep

  1. Login to Coinbase and generate an API key. I specifically set my API key to have all available "read" permissions.
  2. I recommend using the "Allowed IP Addresses" option during API key creation.
  3. Save the key and secret in a secure location, such as your favorite password manager.
  4. Install the Coinbase API for python 

Python Code

 I am using the example code from the Coinbase documentation as a starting point.

import time
from coinbase.wallet.client import Client

# Tracking execution time
start = time.time()

# Authenticate with Coinbase
client = Client(
    'YOUR KEY',
    'YOUR SECRET',
    api_version='2017-09-01')

# Gather account data
accounts = client.get_accounts()

# Display balance and transactions for each account
for account in accounts.data:
  balance = account.balance
  print "%s: %s %s" % (account.name, balance.amount, balance.currency)
  print account.get_transactions()
  
print "\nExecution Time: " + str(time.time() - start)

Notes 

  • You can verify the API version from the API key creation page.
  • Remember to secure your secret and passphrase after (or even during) testing using bcrypt/scrypt/pbkdf2
  • JSON is formatted by the coinbase module
  • Execution time is presented in seconds

Result

Here is a sample of the output I received. I have masked some data, so just live with it.

LTC Wallet: 0.00372555 LTC
{
  "data": [
    {
      "amount": {
        "amount": "-1.00000000", 
        "currency": "LTC"
      }, 
      "created_at": "XXXX-XX-XXTXX:XX:XXX", 
      "description": null, 
      "details": {
        "subtitle": "to GDAX", 
        "title": "Transferred litecoin"
      }, 
      "id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", 
      "instant_exchange": false, 
      "native_amount": {
        "amount": "-81.65", 
        "currency": "USD"
      }, 
      "resource": "transaction", 
      "resource_path": "/v2/accounts/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/transactions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", 
      "status": "completed", 
      "type": "exchange_deposit", 
      "updated_at": "XXXX-XX-XXTXX:XX:XXX"
    },
...

Execution Time: 1.54399991035

And Then?

Next time, let's leverage both the GDAX REST API and the Coinbase API to see the value of my investments. We will play with live market data and wallet balances to calculate the profit/loss of my cryptocurrency purchases.

Comments

Popular posts from this blog

Hacking the Sonoff Wifi Switch - Part 2

Creating Alexa Skills for IoT and Nodemcu - Part 2 AWS IoT

Hacking the Sonoff Wifi Switch - Part 3 Alexa Smart Home