API Reference
DocumentationLog In

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

Geth is one of the native clients to run Ethereum nodes. You could host your own node if you wanted to use the Geth client. However, there is a lot of overhead with managing Ethereum and protocols at scale. Instead, you can use the Geth client to interact with your hosted node.

Install

Install Geth by following the instructions here.

To ensure Geth is installed on your machine, run:

geth –help

This should output a list of commands that the geth CLI accepts.

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 geth, you can use the attach command:

geth attach "https://<USERNAME>:<PASSWORD>@<ENDPOINT>"

Before running the command, replace <USERNAME>, <PASSWORD>, and <ENDPOINT> with your Coinbase Node project’s username, password, and endpoint URL respectively.

Example:

geth attach "https://user:[email protected]

Accessing blockchain data

Running the attach command opens up the Geth JavaScript console, which you can use to interact with your Node.

To demonstrate accessing data from the blockchain, in the Geth JavaScript console, add the following code and press “enter” to run:

var latestBlock = eth.blockNumber;
console.log("Latest Ethereum Block is ", latestBlock);

var blockInfo = eth.getBlockByNumber(latestBlock);
console.log("Information about block", JSON.stringify(blockInfo))
var transactionHash = blockInfo.transactions[0];

var transactionInfo = eth.getTransaction(transactionHash)
console.log("Information about transaction", JSON.stringify(transactionInfo))

The code above performs the following:

  • Gets the latest Ethereum block number
  • Gets a information for a block using it’s block number
  • Gets information about a transaction within a block

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

> var latestBlock = eth.blockNumber;
undefined
> console.log(latestBlock);
14923691
null
>
> var blockInfo = eth.getBlockByNumber(latestBlock);
undefined
> var transactionHash = blockInfo.transactions[0];
undefined
> console.log(transactionHash)
0x09f2a7266d1017195ee34dcac51a6d0d3fb212f1d9da5cef6ee894af8cf99bd6
null
}