1. 程式人生 > >Check your balance on Coinbase using Python

Check your balance on Coinbase using Python

Check your balance on Coinbase using Python

credits: pixabay

Even though Coinbase has a mobile application for you to check your balance on the go, I prefer using their API instead. Because I can setup custom alerts that are not available on their platform. You can also use their API to buy, sell and send crypto. However I currently only use it to check my total balance. Documentation for their API can be found

here. The Github repo for their Python library can be found here

Alright, enough talk, lets dive in and write some code..

To start, install the Python library for Coinbase

pip install coinbase

Once installed, you can then create a Client Object for interacting with the API

from coinbase.wallet.client import Client
api_key = 'XXXXXXX'
api_secret = 'XXXXXXXXXX'
client = Client(api_key, api_secret)

In the code block above, ensure you provide your api_key and api_secret. You can obtain those from here within your Coinbase account.

Once we’ve setup the client, you can now interact with the API. To get your balance for all wallets and total balance, you can use the code below

total = 0
message = []
accounts = client.get_accounts()
for wallet in accounts.data:
message.append( str(wallet['name']) + ' ' + str(wallet['native_balance']) )
value = str( wallet['native_balance']).replace('USD','')
total += float(value)
message.append( 'Total Balance: ' + 'USD ' + str(total) )
print '\n'.join( message )

Since Coinbase API does not provide the total balance, we have to add the value of each wallet to get the total.

To take this a step further, I setup a cron job for this to run hourly and also to send it via push notifications using Pushover. The full script and the cron job are below:

from coinbase.wallet.client import Client
import json
import httplib
import urllib
# replace with your key and secret
api_key = 'xxxxxxx'
api_secret = 'xxxxxxxxxx'
client = Client(api_key, api_secret)
# get balance for each wallet
total = 0
message = []
accounts = client.get_accounts()
for wallet in accounts.data:
message.append( str(wallet['name']) + ' ' + str(wallet['native_balance']) )
value = str( wallet['native_balance']).replace('USD','')
total += float(value)
message.append( 'Total Balance: ' + 'USD ' + str(total) )
# print out total
print '\n'.join( message )
# send balance as push notification
# replace value of token and user with your keys from Pushover
conn = httplib.HTTPSConnection("api.pushover.net:443")
conn.request("POST", "/1/messages.json",
urllib.urlencode({
"token": "xxxxxx",
"user": "xxxxxxxx",
"message": message,
}), { "Content-type": "application/x-www-form-urlencoded" })
conn.getresponse()

Cron job set to run hourly

0 */1 * * * python /home/user/crypto.py > /dev/null 2>&1

When run from the command line, the script will give output similar to the following

BCH Wallet USD 0.00
LTC Wallet USD 0.50
ETH Wallet USD 0.55
BTC Wallet USD 2.11
Total Balance: USD 3.16

I hope you found this useful. Go have some fun with Crypto!!