Web3 and Blockchain Development

Using Infura and Web3j

Written by Christian Felde | Mar 15, 2022 2:45:00 PM
Ethereum is a network of nodes, and in a fully decentralised world you’d be running your own node locally and connect to that when making transactions or querying for data. 
However, not everyone can or wants to run their own node, and because of this we have infrastructure providers like Infura that allow you to connect to the Ethereum blockchain network using their nodes. 
 
Using Infura rather than your own node is super simple. Because you keep your own wallet private keys, switching between these services is as easy as pointing Web3j to another node URL. 
 
You can sign up for Infura at no cost, using their free core account offering. After doing that, you’ll be able to create a project to obtain a project ID. The Infura URL will follow this structure: https://<network>.infura.io/v3/YOUR-PROJECT-ID
 
An example of this can be found in our deployer example project:
 
 
And that’s practically all there is to it, very straightforward.

Using a Project Secret

The previous URL didn’t need anything more than the project ID to enable the connection, but Infura also supports having a project secret. Such a project secret is passed to Infura using HTTP Basic Authentication. 
 
We can also support this in Web3j by passing in a OkHttpClient that is preconfigured to authenticate itself, as shown: 
 
 
While securing the Infura endpoint doesn’t matter from your wallet security point of view, as you always keep your private keys private, it does stop others using your Infura request allowance.