Home > Net >  Python: Request large payload from URL
Python: Request large payload from URL

Time:04-28

I am trying to get data from a URL using Python. The code I am using is this:

response = requests.get(
                url="https://api.covalenthq.com/v1/" Chain_id "/address/" Address "/transactions_v2/?    key=API_KEY",
            headers={
                "Authorization": "Basic AUTHORIZATION",
            },
        )
result = response.json()
result = result['data']['items']

So I am entering a Chain_id and wallet address in the URL to get all the transactions for that pair. However, I only get the latest 100 transactions at most. Is there a way to get all transactions? Is there a parameter I can add to requests to get all the data points?

For example, putting this wallet address, 0x60b86AF869f23aEb552fB7F3CaBD11B829f6Ab2F, into etherscan.io (enter image description here

CodePudding user response:

As @rodrigo-cava said, you can retrive 100 items (default value) for request. This means that you have to fetch the page in order to collect all the result.

A solution may be the following:

CHAIN_ID = "your chain id"
ADDRESS = "your address"
API_KEY = "your api key"

response_list = list()   

url = f"https://api.covalenthq.com/v1/{CHAIN_ID}/address/{ADDRESS}/transactions_v2/?key={API_KEY}"
response = requests.get(url, headers={"Authorization": "Basic AUTHORIZATION"}).json()

while response["pagination"]["has_more"]:
    page_number = response["pagination"]["page_number"]   1
    url = f"https://api.covalenthq.com/v1/{CHAIN_ID}/address/{ADDRESS}/transactions_v2/?page-number={page_number}&page-size=100&key={API_KEY}"

    response = requests.get(url, headers={"Authorization": "Basic AUTHORIZATION"})
    if not response.ok:
        raise Exception("Error during the request")
    
    response_list.append(response.json())
  • Related