API Reference
DocumentationLog In

This guide demonstrates how to integrate your Coinbase Cloud Node project with Web3Py.

Prerequisites

In order to complete this guide, you need Python installed.

To check if you have Python installed, run:

python3 –version

Install

Once you have Python 3 installed, install Web3Py.

To install Web3Py, run the following command:

pip3 install web3

Setup

Import

Before you can start using Web3Py in your project’s code, you need to import it.

Add the following line of code to the top of your file to import Web3Py:

from web3 import Web3

Authorization

Users that create a Coinbase Node project, are provided with the following credentials:

  • An API endpoint
  • A username
  • A password

These credentials allow you to make authorized requests for reading and writing to the blockchain.

To add these credentials to your project:

  1. Create a file named .env to store environment variables
  2. Add the following environment variables to the .env file, replacing the values with your project’s credentials.
NODE_ENDPOINT=<YOUR_ENDPOINT>
NODE_USERNAME<YOUR_USERNAME>
NODE_PASSWORD<YOUR_PASSWORD>

To access these environment variables within your project, add the following lines of code to your Python file:

import os
 
ENDPOINT=os.environ.get('NODE_ENDPOINT')
USER = os.getenv('NODE_USERNAME')
PASSWORD = os.environ.get('NODE_PASSWORD')

📘

Note: For more information on how to create a Coinbase Node project, see Using Ethereum Clients.

Connecting to a node

In order to access data from the blockchain, you need to connect to the Ethereum node provided by your Coinbase Node project.

To connect to a node using Web3Py, you can use the HTTPProvider method.

HTTPProvider accepts an endpoint URL, and a Session object with auth data (i.e. a username and password).

Copy the following code to create a node provider using Web3Py and your Coinbase Node project’s credentials:

from web3 import Web3
import requests
import json
import os
 
ENDPOINT=os.environ.get('NODE_ENDPOINT')
USER = os.getenv('NODE_USERNAME')
PASSWORD = os.environ.get('NODE_PASSWORD')
 
#Create a session with username and password
session = requests.Session()
session.auth = (USER, PASSWORD)
 
#Connect to your Node
w3 = Web3(Web3.HTTPProvider(ENDPOINT, session=session))

Accessing blockchain data

Now that you have a node provider, you can access data from the blockchain.

To demonstrate reading data from the blockchain using Web3Py, copy the following code:

# Get latest block
latest_block = w3.eth.block_number
print('Latest Ethereum Block is', latest_block)
 
# Get info about block
blockInfo = w3.eth.get_block(latest_block)
list_of_transactions= w3.eth.get_block_transaction_count(latest_block)
print('Information about block', blockInfo)
 
transaction_list=[]
for transaction_index in range(list_of_transactions):
   transaction_info=w3.eth.get_transaction_by_block(latest_block,transaction_index)
   transaction_list.append(dict(transaction_info))
 
print('Information about transaction', transaction_list[0])
 
list_of_transactions = w3.toJSON(transaction_list)
parsed = json.loads(list_of_transactions)
indented_JSON = json.dumps(parsed, indent=4)

The code above performs the following:

  1. Gets the latest Ethereum block number
  2. Gets a information for a block using it’s block number
  3. Gets information about a transaction within a block

To run the code above, run the following command:

python3 app.py

Running the code results in an output similar to the following:

Latest Ethereum Block is 14981425
Information about block AttributeDict({'baseFeePerGas': 28742754880, 'difficulty': 14159113062801309, 'extraData': HexBytes('0xe4b883e5bda9e7a59ee4bb99e9b1bc4a1c21'), 'gasLimit': 30000000, 'gasUsed': 6499815, 'hash': HexBytes('0xe7fa4a07f51c1b973e4790ea0df5256784b9836323ec8ea1f376e306c627f7f0'), 'logsBloom': HexBytes('0x542a800001000cc15210004080188930a001000400080400001480083400800818001040200a801410006a20020081038a400819880330000480582140fa30000042000080801008e900900d2030002000880210051018201c0000211000c20012600080022244a00120000a04200e20824f04283040282300300014002800000021010109020010801001025051000204000e9409000018101010c0449000008b88812110036c00289148906500000200040000021900228460008410080920700c000241048800421000a430006310004022400c68401100140808208020001910200820010200020030000104000404610058202883000a50081083022022'), 'miner': '0x829BD824B016326A401d083B33D092293333A830', 'mixHash': HexBytes('0x4181a1b9b1d63546d818355e13a7bfdf24f8526bc3f8e756cb43f05f03e19648'), 'nonce': HexBytes('0x859f2066bdbbd5c7'), 'number': 14981425, 'parentHash': HexBytes('0x6e169103d6256a80e18f2fcd51adbb70ef385e57e3457660b8d916e29eb9fcf9'), 'receiptsRoot': HexBytes('0x56e08e9535995ed84d0fae6dd40db3b3200caf033dfda016e2e3ac9d0044403d'), 'sha3Uncles': HexBytes('0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347'), 'size': 34518, 'stateRoot': HexBytes('0x9b271b01e0d133f21c9633b5a712827cf8a936808ec619d457e35f0d0762a204'), 'timestamp': 1655501817, 'totalDifficulty': 51954271592462588081675, 'transactions': [HexBytes('0x52b4dd41f109ad9121a82571e5aa70922fd33f136d8b963facc600381ad96bbd'), HexBytes('0x751639a8924f1ad32148e78b70bfe3616c9da612dbe993b9abcbbd2dfbdc5b66'), HexBytes('0x87c69b5e125f2acf7d25214e8374949fd78f69226f6755d7c5e59a3853d0de9e'), HexBytes('0x2ed1fdc2042a46613246e5d01a6f5e91e9a870bdb29960a20856c33f31e64c32'), HexBytes('0xebf378d9635581ef7823d942ea768530509a54ef38b58924bdf874f5f8817b6a'), HexBytes('0xe3a64754ec294afdc5b53e1724ca7b5f00a0ec79bc58338c37f6b169eecde99f'), HexBytes('0xe4b357811c1a1e4cfc4a9c5499a14332a020fc83d31769b8b160115830ae23b4'), HexBytes('0xacb2df43d1c0457241fb1ec2fd32711102a3e049b5d7f78c83ecd8c287d2c3bd')], 'transactionsRoot': HexBytes('0xf85ad59b27551e6b2cb8ae85c6fa41070d5e1ac2f4ee5fdcf94dd3faddcbe670'), 'uncles': []})
Information about transaction {'blockHash': HexBytes('0xe7fa4a07f51c1b973e4790ea0df5256784b9836323ec8ea1f376e306c627f7f0'), 'blockNumber': 14981425, 'from': '0x1Bf1c0d636343D62526f778A996584A029BC61EB', 'gas': 289790, 'gasPrice': 28742754880, 'maxFeePerGas': 29030182428, 'maxPriorityFeePerGas': 0, 'hash': HexBytes('0x52b4dd41f109ad9121a82571e5aa70922fd33f136d8b963facc600381ad96bbd'), 'input': '0x28016e169103d6256a80010209df50fbde8180c8785842c8e316ebe06f542d344300000000000000002db5c271eda367b6880000c6030601000064000000000000000000000000323aed9304000401e93dc496dbc669d7ee4f03b0eb0a10bb13a4b2a40000000000000000000000003181313f0101e93dc496dbc669d7ee4f03b0eb0a10bb13a4b2a4c5ed7350e0fb3f780c756ba7d5d8539dc242a4140000000000000776bcdc88494a2b756c0108c5ed7350e0fb3f780c756ba7d5d8539dc242a4140300000000000000000ab9ade54888b156010403df50fbde8180c8785842c8e316ebe06f542d3443000000000000002db5c271eda367b688', 'nonce': 6096, 'to': '0xEef86c2E49E11345F1a693675dF9a38f7d880C8F', 'transactionIndex': 0, 'value': 10400, 'type': '0x2', 'accessList': [AttributeDict({'address': '0xdf50fbde8180c8785842c8e316ebe06f542d3443', 'storageKeys': ['0x0000000000000000000000000000000000000000000000000000000000000000', '0x0000000000000000000000000000000000000000000000000000000000000001']}), AttributeDict({'address': '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', 'storageKeys': ['0x4996ce4b490fd9dc8f8b2d6634a89502ba6281ee9ff15f8ccfadd0c7b2c9a958', '0x734f966893beb46994b83667955c623d675ca15b69f29b465585db2ebe28707e', '0xcc95fe5cd9b4f990b88646bff96a70ad3202dc09d3cb9196914a0f424bdb4454']}), AttributeDict({'address': '0xa2327a938febf5fec13bacfb16ae10ecbc4cbdcf', 'storageKeys': []}), AttributeDict({'address': '0x956f47f50a910163d8bf957cf5846d573e7f87ca', 'storageKeys': ['0x1fdd5a7fbf23d3246b969e0497ac3b190949c677b11f70f11ea86e4bc6d7e658', '0xa89dd3c7ee6dcbdc95525abb4427463ccdaf4c7baf5a84f0d1533ad5b228249f']}), AttributeDict({'address': '0xe93dc496dbc669d7ee4f03b0eb0a10bb13a4b2a4', 'storageKeys': ['0x0000000000000000000000000000000000000000000000000000000000000008', '0x0000000000000000000000000000000000000000000000000000000000000009', '0x000000000000000000000000000000000000000000000000000000000000000a']}), AttributeDict({'address': '0xc0ba369c8db6eb3924965e5c4fd0b4c1b91e305f', 'storageKeys': ['0x263eb0832c0a47b6ec34d792b3d1ee76832ff7c193b5b4ddd5d4bdc79af912e0', '0xd9c97c3ddfea523ec56747b0a306e0846530da17843a4bcb5b7d3ad67a347ef2']}), AttributeDict({'address': '0xc5ed7350e0fb3f780c756ba7d5d8539dc242a414', 'storageKeys': ['0x0000000000000000000000000000000000000000000000000000000000000008', '0x0000000000000000000000000000000000000000000000000000000000000009', '0x000000000000000000000000000000000000000000000000000000000000000a']}), AttributeDict({'address': '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', 'storageKeys': ['0x3e2681f7b92690b04113060055c8b4cb436cefab7a97af311fbce9ac6d4d3f78', '0xadce39c64b98efa32c38d621e56887fc4a2beb485bf06504815cba4729066a78']}), AttributeDict({'address': '0x94b0a3d511b6ecdb17ebf877278ab030acb0a878', 'storageKeys': ['0x0000000000000000000000000000000000000000000000000000000000000008', '0x0000000000000000000000000000000000000000000000000000000000000009', '0x000000000000000000000000000000000000000000000000000000000000000a']}), AttributeDict({'address': '0xea674fdde714fd979de3edf0f56aa9716b898ec8', 'storageKeys': []})], 'chainId': '0x1', 'v': 0, 'r': HexBytes('0xbd95cde6634b4b09c27e86fab8e21edf3ec30545c471814a26790a0b981ea256'), 's': HexBytes('0x6bea85e8573316938d82ed2d0a8d6b06e783ad7d47aa932f0ddc956302577d6b')}