Blockchain development can be complex. And no wonder—when working within a decentralized system operating on a set of complex protocols there are many factors you need to consider. Even for even the simplest of activities, you need to access blockchain nodes, check gas prices, pay for gas, sign transactions, and more.
Infura is a powerful suite of APIs and developer tools that abstracts away these complexities on the most popular blockchains, including Ethereum, Linea, Arbitrum, StarkNet, IPFS, and more. By using Infura, you can spend more time building your dapp, and less time worrying about details such as scalability and uptime.
This guide provides all the steps necessary to set up and get started using Infura. You’ll be able to set up an Infura API key, access the Ethereum (Sepolia) blockchain, and use Infura to build your dapp. By the end of this guide, you’ll see why leading dapps and tools such as MetaMask, Uniswap, and MakerDAO rely on the reliability, ease of use, and scale of Infura to power their ventures.
To get started, you’ll need a one-time setup of some tools and software essential to creating and deploying your dapp.
Set up MetaMask
First, in order to conduct transactions on a blockchain (for example, deploying a contract or minting an NFT), you’ll need a crypto wallet.
Several options for wallets are available in the market, but we’ll use the most popular and easy to use, MetaMask. MetaMask is a self-custodial wallet that gives users full control over their crypto and transaction authorizations. MetaMask is available for free as a browser extension.
Once you install the extension, MetaMask will ask you to complete a series of simple steps to set up a wallet for you. In the process, you’ll receive a secret phrase for accessing and managing your wallet. Make sure you store this secret phase safely (ideally both in a digital and physical space), and never share this publicly. If someone learns your secret phrase, they can then steal the assets in your wallet. We recommend creating a separate wallet address for development purposes, where your valuable assets wont be stored.
Switch to the Sepolia test network
Once your wallet is set up, navigate to the top-right of the wallet window and click on Show/Hide Test Networks. After showing test networks, you’ll see the drop-down option for Sepolia. Switch your network from Ethereum mainnet to Sepolia test network.
If this is your first time using a wallet, you should see a balance of 0 SepoliaETH. We’ll fund this wallet in a later step.
Node.js and npm
We’ll use Truffle to create the foundation of your project. Truffle is an industry-leading Ethereum development environment that allows you to create, test, and deploy smart contracts to EVM-based blockchains.
To use Truffle, you need to first install Node.js and npm. You can install both on your local machine by following these instructions.
You can verify their successful installations using the command line. (Your version numbers might be different from what’s shown below.)
Step #1: Obtain an Infura API key
As mentioned earlier, Infura is a tool that gives you easy, fast, and reliable access to most major public blockchains and L2s, including Ethereum. Infura does this by giving you access to RPC endpoints. Think of RPC endpoints as API endpoints to which you can send requests. In order to access these endpoints, you’ll need an Infura API key.
Create an account
To get an API key, you’ll first need to sign up for an Infura account that is free. Once you’ve signed up, you’ll be taken to the dashboard. This dashboard allows you to complete a variety of tasks, such as:
- Create new API Keys
- View analytics
- Access support and other helpful resources
- Setup project security
- Upgrade your plan and activate add-on networks
- Get network endpoints for mainnet and testnets
- And more
Create API key
From the dashboard, click Create New API Key. This will open a pop-up form. For network, choose Web3 API. You can name the project anything you want.
Once you click Create, Infura will create a new API key for you and redirect you to the key’s dashboard page. Navigate to the Endpoints page for your key. project.
On this page, you’ll see all the RPC endpoints that you can access. Under Ethereum endpoints, select the endpoint for Sepolia. The endpoint should be of the format https://sepolia.infura.io/v3/[YOUR API KEY].
After this step, we recommend that you add your own RPC endpoint to MetaMask by adding a new customer network. This will allow you to track your MetaMask requests directly from the Infura dashboard and understand what is happening in the backend when you send transactions from your MetaMask wallet.
Step #2: Set up your development environment
With a MetaMask wallet and an Infura RPC endpoint in hand, you can begin setting up your development environment using Truffle.
Initialize a Node.js project
First, let’s set up a Node project and install all necessary packages. Create an empty Node repository by running the following commands:
Next, install Truffle:
Remember that we installed MetaMask so that we could pay for transactions conducted on the blockchain. You’ll need to give Truffle access to your wallet so it can perform that action on your behalf. For this, you’ll need another package: hdwallet-provider.
You’ll be sharing your wallet secret phrase with your Truffle project. Under no circumstance do we want these secrets to ever be made public. You should never hard-code your secret phrase or credentials into your project. Instead, provide them through environment variables.
We’ll use dotenv and gitignore to do this. This prevents you from being committed to version control systems and reduces the risk of accidental exposure when sharing code repositories like GitHub.
Add your project keys here, and wallet address.
Next, add “.env” to make sure GitHub doesn’t include the file containing your keys publicly.
Create a Truffle project
Now, we can create a barebones Truffle project. Run the following command from within your Node.js project root folder:
To ensure that everything works correctly, run the following command:
If you don’t receive any errors, you can proceed with your project. If you have any issues, check out the Truffle support and help page.
In your project, you should now see a contracts subfolder. Within this folder, let’s create an extremely simple smart contract in a file called Donations.sol. This contract does the following:
- Allow anyone to make a donation.
- Allow the owner of the contract to withdraw all the donations made.
The code Donations.sol should look like this:
Compile the contract with the following command:
Configure Truffle and environment variables
Next, we’ll need a file to store our environment variables. This is where we’ll put your project secrets—namely, your MetaMask wallet secret phrase and the Sepolia RPC endpoint which contains your Infura API key.
In your project root folder, create a file called .env with the following information:
Next, let’s inject this information and configure your Truffle project. Replace the contents of truffle.config.js with the following:
Step #3: Deploy your smart contract to Sepolia
Now that we’ve configured our Truffle project and written the smart contract, we can deploy it to the Sepolia blockchain.
Use the Infura faucet to get some test SepoliaETH
Although we’ve set up your MetaMask wallet and configured it to use the Sepolia network, your wallet balance is still 0 SepoliaETH. In order to conduct any transactions—such as deploying your contract, making donations, and even retrieving donations—you’ll be required to pay gas. So, you’ll need some SepoliaETH in your wallet.
Since we’re deploying our contract to a test network (Sepolia), you can obtain these tokens for free through the Infura Sepolia faucet.
Once you’ve filled up your wallet, you should see a small SepoliaETH balance.
Deploy smart contract to Sepolia
In order to deploy your contract to Sepolia, we’ll write a script called 1_deploy_contract.js. Create the file in the migrations folder and paste in the following code:
Then, deploy the smart contract:
If all goes well, you should see output that looks something like this:
Congratulations! Your contract is now live! You can double-check this by searching for your contract address on Sepolia Etherscan.
You can also verify your contract on Etherscan by uploading the source code, which Etherscan will match and compare against the code on the blockchain. On the Etherscan page, click the Contract tab and then click Verify and Publish.
After this verification is complete, you’ll see the green check mark over the Contract tab.
Step #4: Send a transaction using MetaMask
Now let’s donate a small amount (0.001 ETH) to this contract. Navigate to the Contract tab and then to the Write Contract tab. Click on Connect to Web3.
This will open a modal to prompt you to connect to a wallet. Select your MetaMask wallet.
Once your wallet is connected, you can click donate. Enter an amount to donate (0.001), and then click Write.
Your MetaMask wallet will pop up, and you’ll be asked to confirm this transaction. Click Confirm.
After you’ve confirmed the transaction, you can view the transaction details. The list of transactions for the smart contract also shows the recent transaction.
And that’s it! With just a few clicks, you were able to make a donation through MetaMask on your smart contract. Every single step in the process was powered by Infura.
Step #5: Track smart contract activity on the Infura dashboard
To see the work done by Infura under the hood, you can check out the analytics associated with our Infura project. Navigate to the main Infura dashboard, find your project, and click View Stats.
If this is a project that you’ve just created, change the timeline to Last 1 Hour.
You will be able to see the number of requests made during that window.
You should also be able to see the specific requests made to the Sepolia Ethereum blockchain. On the surface, you’ve only made a few clicks, but hundreds of requests have been propagated to make what we wanted to do possible.
To also see your MetaMask transactions, you will need to update your MetaMask RPC endpoint by switching from the default MetaMask RPC to your Infura one.
Step #6: Start building
Infura simplifies your job as a blockchain developer by allowing you to connect to different blockchain networks without having to run your own nodes. This takes care of the scalability, uptime, and the reliability of your dapp so you can focus on the core logic of your business or product.
Now that you’re set up, let’s start building with the following resources:
- How to Choose the Right Blockchain for your Application
- The Developer’s Guide to the Web3 Stack
- Full Stack Frontend using IPFS
- Infura NFT API + SDK
- Infura Help Center
- Infura Discord
Stay up to update on all Infura product releases and news by following us on Twitter.