How To Use JSON-RPC With for Ethereum Nodes


Using JSON-RPC With

On the most basic level, web3 is just a library that allows the user to interact with Ethereum nodes.

More specifically, is a python library for interacting with Ethereum. Its API is derived from the Web3.js Javascript API.

In this article, we're going to talk about how to use it with your Ethereum node, with a few practical examples.

Step one

Install: can be installed (preferably in a virtualenv) using pip as follows:

$ pip install web3

Step two

Usage: To use the web3 library you will need to initialize the Web3 class.

 NotesRPC connects over standard port 443
Our nodes require a token in the header to give you access to the node. This is a required value to run any command against the node. You can actually pass this in your url in web3.js by using the following URL with the auth param:
You should be able to pass this in constructing your new web3 instance, just like below
var web3 = new Web3(new Web3.providers.HttpProvider(''));

Script examples: 

Print current blocknumber:

from web3 import Web3, HTTPProvider

web3 = Web3(HTTPProvider('')) 


Print current gas price (median of the last blocks)

from web3 import Web3, HTTPProvider

web3 = Web3(HTTPProvider(''))


Send Ether to another account:

Python3 Code examples

import sys

from web3: import Web3, HTTPProvider

Connect to the Blockdaemon Ethereum endpoint

w3 = Web3(HTTPProvider(''))

 Get the sender's private key

senderKey = input("Enter sender private key (e.g. export from MetaMask): ")


senderAccount = w3.eth.account.privateKeyToAccount(senderKey)


print("ERROR: Not a valid private key")


 Get the receiver's address

receiver_address = input("Enter receiver address: ")

if not Web3.isAddress(receiver_address):

print("ERROR: Not a valid address")


 Create the transaction

transaction = {

   'to': receiver_address,
    'value': 1, # Sending 1 wei

    'gas': 90000, # The default in geth
     'gasPrice': w3.eth.gasPrice, # Use the value determined by the node
     'nonce': w3.eth.getTransactionCount(senderAccount.address) # Get correct transaction nonce for sender from the node

 Sign the transaction

signed = w3.eth.account.signTransaction(transaction, senderAccount.privateKey)

 Send it!

txHash = w3.eth.sendRawTransaction(signed.rawTransaction)
print(f"Succesfully sent transaction {Web3.toHex(txHash)}")

 A video walkthrough of using a smart contract with Ethereum

 Helpful docs:

 More Reference articles:

Working with local private keys

Some Common Uses for Local Private Keys

A common reason to work with local private keys is to interact with a hosted node.

Some common things you might want to do with a Local Private Key are:

Questions?  send an email to: Blockdaemon support







0 out of 0 found this helpful



Please sign in to leave a comment.