In our last two tutorials, we covered how to access live pricing data from any exchange. You can find those two tutorials here:

A Script for Bitcoin Price Live Ticker

A Python Script for Cryptocurrency Price Charts

Python Scripts for Crypto Trading Bots [API Trading Tutorial]

Instead of collecting more market data, let’s try something different. Most cryptocurrency users currently have funds located on an exchange. We can leverage the exchange’s APIs to access the asset balances and calculate their value without ever going to the exchange.

The following tutorial will take a step-by-step approach to detail how you can connect to an exchange account through APIs and access your balance data without ever logging into your exchanges.

Install Library

Before we can start writing our scripts and connecting to exchanges, let’s set up our development environment by first installing the libraries we need to complete our project.

pip install shrimpy-python

For this script, we will only need access to the Shrimpy Developer API Python library. This library makes it easy to connect to exchanges, manage assets, collect data, and build applications. The full APIs and their documentation can be found here.

Import Library

Once the library is installed, we can import it into our script to access all the features it provides.

import shrimpy

That’s it for libraries, so we’re ready to start constructing the meat of our script.

API Keys

There are 2 different sets of API keys we need in order to access our balance data on the exchange.

Exchange Account

The first set of API keys we need is the exchange API keys. These keys are found by going to your exchange of choice and creating a new set of API keys.

Our script will require the ability to access balance data, so make sure you have enabled the permissions on your exchange API keys for reading balance data from your exchange account. In this tutorial, you won’t need to enable the permissions for trading or withdrawals.

You can find a number of helpful tutorials on how to access exchange API keys here.

Once you’ve generated your API keys, save them as variables which we will use in the next steps to access your exchange account balances.

exchange_name = 'bittrex' exchange_public_key = 'fbnnn1xzimjustkeyboardmashing8xn1t8' exchange_secret_key = '09672v4n09xn0morekeyboardmashing947'

Note: Shrimpy supports 16 different exchanges. Use the following names when creating your exchange_name variable: binance, bittrex, bittrexinternational, kucoin, poloniex, bibox, huobi, hitbtc, bitstamp, bitfinex, coinbasepro, kraken, gemini, huobiglobal, bitmart, and okex.

Shrimpy Account

The second set of API keys we need to access our balance data on the exchange is the Shrimpy Developer API Master Keys.

To get your set of keys, sign up for your free Shrimpy account and select the option to “Create Api Master Key”.

When selecting the permissions for this key, ensure you have enabled the options for “User” and “Account”. These are required to link and access exchange accounts for users.

With your Shrimpy Developer API key in hand, assign it to variables which can be accessed in a future step.

shrimpy_public_key = '8x7138n1xzimjustkeyboardmashing8xn1t8jv5098' shrimpy_secret_key = '771dc5n72v4n09xn0morekeyboardmashing9475c0294n50'

Create Client

Use the keys we generated in the previous step to create your Shrimpy client.

client = shrimpy.ShrimpyApiClient(shrimpy_public_key, shrimpy_secret_key)

Create User

The Shrimpy APIs require exchange accounts to be connected to users. That way we can generate any number of users who manage multiple individual exchange accounts. This provides a built-in way to determine who owns which exchange account.

Let’s create our user who will be the one which gets linked to our exchange accounts.

create_user_response = client.create_user('Satoshi Nakamoto') user_id = create_user_response['id']

Link Exchange Account

Once the user is created, linking an exchange to the user is straight forward. Simply specify the user, exchange name, and the API keys for that exchange.

link_account_response = client.link_account( user_id, exchange_name, exchange_public_key, exchange_secret_key ) account_id = link_account_response['id']

As soon as the exchange account is linked, Shrimpy will automatically begin collecting data from the exchange regarding the account’s balances. This process can take a moment, so let’s take 5 seconds before going on to the next step.

Note: You can link up to 20 exchange accounts to a single user. That means if you have accounts with Bittrex, Binance, KuCoin, BitStamp, Kraken, and Coinbase Pro (and more) you can link them all to a single user.

Get Asset Balances

We’re almost done. Let’s grab the exchange account balance from Shrimpy. Simply call the library and ask for the balance data for a specific user and account.

The response to this call will return every asset we own, the balances for these assets, and the dollar values.

balance = client.get_balance(user_id, account_id) holdings = balance['balances']

Print Out Balances

The final step is to calculate the balances and display them. In this case, we will just print them out, but you can certainly do anything you wish with the balances.

We will iterate over each asset in the returned list and display the asset and the value of our asset holdings.

total = 0 for asset in holdings: print(asset['symbol'] + ' ' + str(asset['usdValue'])) total += asset['usdValue'] print("Total" + ' $' + str(total))

That’s it! You have successfully connected to your exchange account and accessed your balances without logging into the exchange through the UI.

Whenever you wish to update your balances, you can simply request the latest balances from Shrimpy again by passing in your user ID and account ID. Shrimpy will handle the rest by accessing your linked exchange account and collecting the balance data. You DO NOT need to re-link your keys every time or create a new user each time. Those steps are a one-time thing.