Stream Ledger Data from the Stellar Network Using Python


This tutorial walks you through writing a simple Python script that streams ledger data from the Stellar network to your console.

The tutorial assumes you have some knowledge of Python. It's also a good idea to install the latest version of the Python Stellar SDK:

`pip install -U stellar-sdk`.

For an example of what this will look like, head over to StellarExpert to see their version of it:


To get the ledger data, we’ll be using the Python Stellar SDK to access the Horizon API. If you‘re new to Horizon — it is an API server that acts as an interface between Stellar Core and applications or developers who want access to the Stellar network. Horizon has a /ledgers endpoint that we will use to stream ledger data. Try visiting to see the latest ledger.

Note: Horizon has multiple endpoints that support streaming — accounts, effects, ledgers, offers, operations, orderbook, payments, transactions, and trades. Feel free to re-purpose this script to experiment with those.

To get started, import the Horizon object from the Python Stellar SDK. The Horizon object allows us to easily access Horizon API endpoints.

Next, create two functions — one to handle the responses from Horizon and one to call the Horizon /ledgers endpoint in streaming mode. The handle function will only print the response to console for now.

Next, we’ll have to build out the get_ledger_data() function. This function will do a couple of things: create a new Horizon object, call the /ledgers endpoint in streaming mode, and for each response we get we’ll pass it to data_handler():

Line 9 creates the new Horizon object and sets the horizon_uri to the main net —

Line 11 calls the /ledgers endpoint with some arguments. cursor=’now’ specifies that we want to start returning records once the streaming object is created — you can also set this to start at a specific ledger (e.g. 25310841). order=’asc’ specifies the order in which we want to return rows, in this case ascending order. Lastly, sse=True specifies that we want to use server side events for streaming responses.

Line 13 uses a for loop to pass each response to the data_handler() function.

When you compile and run this script as is, you should start getting responses printed to console like this:


This response should look familiar if you have used Python get requests before. The Python Stellar SDK sends a get request to the ledgers endpoint for us and returns a dictionary (created from a JSON response) with information from the /ledgers endpoint.

 These responses give you all kinds of information — current ledger sequence, total operations, total transactions, etc. 

Now that we can stream ledger data from the network, let’s make it easier on the eyes by accessing specific information and formatting it similarly to the StellarExpert example.
To do that, alter the data_handler() function as follows:

>Python dictionaries are made up of keys and values, to grab specific data, we simply use the relevant key (e.g. response[‘sequence’]). Here we only get data for the ledger sequence, successful transactions, failed transactions, and total operations in the ledger.<

Note: Stellar transactions consist of a list of operations (e.g. payments or buy/sell offers). Transactions can fail for a couple of reasons and may be included in the ledger as described here.

Now run the script to see the final result:

The basic idea behind this script can be re-purposed or reused for Stellar applications, data analytics, etc.

📋 The full code can be found here:

0 out of 0 found this helpful



Please sign in to leave a comment.