Pagination on Coinbase Python API

by Travis Pettijohn   Last Updated July 12, 2019 05:26 AM

I am trying to get all of the transactions on a Coinbase account, which requires pagination. The documentation is sparse on how to do this in Python, but I have managed to make it work:

client = Client(keys['apiKey'], keys['apiSecret'])
accounts = client.get_accounts()

for account in accounts.data:
    txns = client.get_transactions(account.id, limit=25)
    while True: 
        for tx in txns.data:
            print(tx.id)

        if txns.pagination.next_uri != None:
            starting_after_guid = re.search('starting_after=([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})', txns.pagination.next_uri, re.I)[1]
            txns = client.get_transactions(account.id, limit=25, starting_after=starting_after_guid)
        else:
            break

The pagination object only contains next_uri everything else is null/None--it is supposed to contain a dict that includes starting_after among other helpful data. The regex search seems silly, but it works.

Is there a better way?



Answers 2


Above snippet encountered error. This works

client = Client(keys['apiKey'], keys['apiSecret'])
accounts = client.get_accounts()

for account in accounts.data:
    txns = client.get_transactions(account.id, limit=25)
    while True:
        for tx in txns.data:
            print(tx.id)

        if txns.pagination.next_uri != None:
            starting_after_guid = re.search('starting_after=(.*)', str(txns.pagination.next_uri), re.I).group(1)
            txns = client.get_transactions(account.id, limit=25, starting_after=starting_after_guid)
        else:
            break
Troy
Troy
March 15, 2018 13:30 PM

This is how it can work without regex:

_cb = Client(keys['apiKey'], keys['apiSecret'])
accounts = client.get_accounts()

for account in accounts.data:
    """ Gets the transactions history from coinbase """
    all_txns = []
    starting_after = None
    while True:
        txns = _cb.get_transactions(account['id'], limit=100, starting_after=starting_after)
        if txns.pagination.next_uri is not None:
            starting_after = txns.pagination.next_starting_after
            for tx in txns.data:
                all_txns.append(tx)
            time.sleep(1)  # Let's not hit the rate limiting
        else:
            for tx in txns.data:
                all_txns.append(tx)
            break
tlex
tlex
July 12, 2019 05:24 AM

Related Questions


Updated January 09, 2018 01:26 AM

Updated May 28, 2017 18:26 PM

Updated May 19, 2018 10:26 AM

Updated July 18, 2016 08:11 AM

Updated December 10, 2017 02:26 AM