0

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",
    },
)
tinom9
  • 369
  • 2
  • 12
  • stupid question, but you *are* using the actual API key in python `requests.get()`? HTTP 403 means [Forbidden](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/403). – Edo Akse Sep 24 '21 at 09:40
  • Yes, MyApiKey stands for the API key from Etherscan. It should work I guess... – tinom9 Sep 24 '21 at 18:16

1 Answers1

3

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'})
ca098
  • 68
  • 7