Recently I’ve been playing around with the Python Stellar SDK and thought I’d share some code snippets for any new Python programmers looking to experiment with Stellar. If you have any suggestions feel free to make a PR.

In this tutorial we’ll be creating a script that takes in a Stellar address and returns the XLM balance. We will also convert that XLM balance to USD using the new ticker API for the Stellar DEX.

I’m going to assume you already have Python installed if you’re reading this and plan on following along.

The next thing you’ll need to do is run the following commands in your command line:

The first command installs the Python requests library, allowing us to make HTTP requests. The second command installs the Python Stellar SDK, allowing us to communicate with the Horizon API and retrieve data from the Stellar network.

Create a new file called stellar_wallet_reader.py and let’s start writing.

We need to import the necessary libraries/classes and get some data. To do that, enter the following lines of code:

Line 1 imports the Address object from the SDK, we’ll use this later.

The next two lines import the requests library and the json library. These libraries will allow us to retrieve data from the Stellar ticker API — which is just data from the SDEX. Line 6 executes a request to retrieve data from https://ticker.stellar.org/markets.json and stores the response in a variable r. Line 7 does some work for us and grabs the JSON object, storing it in json_obj.

Next we will create a function get_price() to search through json_obj:

This function takes in json_obj and a parameter called name, which represent the trading pair we are searching for — in this case ‘XLM_USD.’ After finding the pair in the market data, it returns the real-time ‘price.’

Note: You could use any pair you want and adjust the rest of the code to fit your needs. e.g. ‘XLM_BTC’ or ‘XLM_CNY’

On line 8 create a variable called xlm_price. Here we need to do a little math — our get_price() function returns the price of USD in lumens (~9 lumens for $1), so we need to get the inverse.

Next, we’ll set up our script to take in a public key and store the input in a variable pub_key — we’ll save error checking the input for another time.

The print() is to format the output later.

After an address gets entered and stored in pub_key, we need to get some information about it:

Line 2 uses the Address object we imported earlier and comes from the SDK. We initialize Address by providing our pub_key variable, we leave secret=None as we do not provide a private key, and we set our network to ‘public.’ Then we ping Horizon in line 5 using address.get() to retrieve the latest network information.

Line 8 requires a little digging. Stellar addresses can hold many assets so when we use address.balances it can return a looong list of balances. Fortunately for us, the XLM balance always takes up the last item in the list and can be accessed with address.balances[-1]. Finally, we use address.balances[-1][‘balance’] to retrieve the balance attribute (the # of XLM), converting it to a float to handle some math later.

Now we can finally add our last line of code to print our output:

Print() simply outputs to console. Inside of print we are first outputting the XLM balance via the xlm_balance variable. Finally, we multiply the xlm_price found earlier by the xlm_balance to get the equivalent value in USD.

You now have a Python script that can check a Stellar address, read its XLM balance, and convert the balance to USD using Stellar DEX market data 💻

To run it, save your file and if you used the name suggested earlier, run the following command in your console to compile and see the results:

Your final code should look something like this:

📋 For the GitHub repo: https://github.com/koltenb/stellar-address-reader.

Feel free to leave any feedback or questions!