Search code examples
pythonpython-requestsethereumetherscan

Etherscan API request 403 forbidden in Ropsten network


I am trying to send a request to Etherscan API in the Ropsten network and it is not working as it shows 403 error:

response = requests.get(
    "https://api-ropsten.etherscan.io/api",
    params={
        "module": "account",
        "action": "balance",
        "address": "0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae",
        "tag": "latest",
        "apikey": "MyApiKey",
    },
)

It is very awkward because when I do the same from Postman with this url, it works:

https://api-ropsten.etherscan.io/api?module=account&action=balance&address=0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae&tag=latest&apikey=MyApiKey

And, when I do the same request to the Ethereum Mainnet, it works as well:

response = requests.get(
    "https://api.etherscan.io/api",
    params={
        "module": "account",
        "action": "balance",
        "address": "0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae",
        "tag": "latest",
        "apikey": "MyApiKey",
    },
)

Solution

  • I was struggling with the same issue. For anyone else that is struggling, I found the answer here. Essentially Etherscan is blocking requests that don't provide a User-agent so add a User-agent header property if using the Python requests module.

    response = requests.get(
            "https://api-ropsten.etherscan.io/api",
            params={
                "module": "account",
                "action": "balance",
                "address": "0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae",
                "tag": "latest",
                "apikey": "API_KEY",
            },
            headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, '
                                   'like Gecko) Chrome/50.0.2661.102 Safari/537.36'})