I cannot connect to klaytn API. Anyone can help me ? Thanks!

Hi. I want to use klaytn api service. But I never succeed. I used both python web3py and requests library. But neither of them worked. Can you help me?

My usage:

from web3 import Web3
from requests.auth import HTTPBasicAuth

klaytn = 'https://node-api.klaytnapi.com/v1/klaytn'

# The username and password are from Kayltn API service. They are accessKeyId and secretAccessKey.

web3 = Web3(Web3.HTTPProvider(klaytn, request_kwargs={
    'auth': HTTPBasicAuth(username='KASKEZHJZD45D********', password='7Nuf8ixH7ywwRrz0e00tkW******')}))

import requests
from requests.auth import HTTPBasicAuth

header = {'Authorization': 'Basic *********=', 'x-chain-id': '8217'}

auth = HTTPBasicAuth('KASKEZHJZD45D********', '7Nuf8ixH7ywwRrz0e00tkW******')

r = requests.get('https://node-api.klaytnapi.com/v1/klaytn', headers=header)


Neither of them succeed. I dont’ know how to solve it.
For example. When I use bsc. I could set the rpc url to

But I don’t know how to use klaytn api service.

And another question. Is the mainnet and testnet use the same rpc url. Because it seems like this.


Hi @Light_Spoon ,
Thank you for your interest in Klaytn.

Unfortunately, Klaytn doesn’t officially support web3 or caver in Python.
We can help you with caver-js or caver-java.

Thanks for filing an issue on KlaytnForum.

Since we do not provide Python SDK, please use the following code for Node API.

import requests, json

header = {'Content-Type': 'application/json', 'Authorization': 'Basic ***', 'x-chain-id': '8217'}

data ={"jsonrpc":"2.0","method":"klay_blockNumber","params":[],"id":1}
r = requests.post('https://node-api.klaytnapi.com/v1/klaytn', headers=header, data=json.dumps(data))