Using Infura (or a custom provider)

Infura is a hosted Ethereum node cluster that lets your users run your application without requiring them to set up their own Ethereum node or wallet. Infura

You may not be familiar with Infura by name, but if you've used MetaMask then you've used Infura, as it is the Ethereum provider that powers MetaMask.

For security reasons, Infura does not manage your private keys, which means Infura cannot sign transactions on your behalf.

However, Truffle can sign transactions through the use of its HDWalletProvider. This provider can handle the transaction signing as well as the connection to the Ethereum network. (Read more about HDWalletProvider.)

This tutorial will show you how to use Infura to migrate an existing dapp to an Ethereum network supported by Infura. In this specific instance, we'll migrate to Ropsten. We'll assume that you already have a dapp to migrate. If you want a test dapp, feel free to use our Pet Shop tutorial dapp.

 Install HDWalletProvider

Truffle's HDWalletProvider is a separate npm package:

npm install truffle-hdwallet-provider

Note: If you are on Windows and get an MSBUILD error, you may need to install the Windows build tools. In a terminal with Administrator rights, run npm install -g windows-build-tools and then try installation again.

 Register with Infura

Before you can use Infura, you need to register for an Infura Access Token.

Fill out the form and you will receive your token. Your information will be sent to your email and displayed on the screen. Make sure you save this token and keep it private!

 Configure your Truffle project

The next step is to edit your truffle.js file to use HDWalletProvider and provide all the necessary configuration for deploying to Ropsten.

  1. First, define the HDWalletProvider object in your configuration file. Add this line at the top of your truffle.js file:

    var HDWalletProvider = require("truffle-hdwallet-provider");
  2. Next, provide a reference to your mnemonic that generates your accounts.

    var mnemonic = "orange apple banana ... ";

    Warning: In production, we highly recommend storing the mnemonic in another (secret) file, to reduce the risk of the mnemonic becoming known. If someone knows your mnemonic, they have all of your addresses and private keys!

  3. Add a Ropsten network definition:

    module.exports = {
      networks: {
        ropsten: {
          provider: function() {
            return new HDWalletProvider(mnemonic, "<INFURA_Access_Token>")
          network_id: 3

    Things to notice:

    • While the example has only a single network defined, you can define multiple networks as normal.

    • The provider for the ropsten network definition instantiates the HDWalletProvider.

    • The HDWalletProvider takes as arguments a mnemonic and the desired network. A list of Infura-supported networks is available on the Infura homepage.

    • Make sure to replace <INFURA_Access_Token> with the Infura Access Token you were granted above.

    • The provider value is wrapped in a function, which ensures that it won't get initialized until it's needed. This is especially important if connecting to multiple networks. (See the Networks configuration section of the documentation for more on this topic.)

    • Without any other arguments, the account in charge of migration will be the first one generated by the mnemonic. But if desired, you can pass in an argument to specify which account to use. As an example, to use the third account:

      new HDWalletProvider(mnemonic, "<Infura_Access_Token>", 2);

      (Recall that the index is zero-based, so 2 is the third address.)

 Use an ether faucet

Make sure you have enough ether in your account to do the deployment. You can acquire ether on the Ropsten network through a service known as a faucet. While there are multiple faucet sites out there, one service we recommend is hosted on EthTools.

  1. Navigate to the EthTools Ether Faucet.

  2. Enter your mnemonic, and select how much ether you would like (maximum of 5).

  3. The faucet will link to your first account. Click "Request Ether" to submit your request.

  4. Within a short period of time, your account will be populated with the requested ether.

    Note: You can also request Ether through MetaMask. Connect to your account on Ropsten, and click the "Buy" button, which will give you a link to MetaMask's Ropsten Test Faucet, which works similarly to above.

We are now ready to deploy to Ropsten!

 Deploy the contract

  1. Compile your project, if not already done:

    truffle compile
  2. Deploy to the Ropsten network:

    truffle migrate --network ropsten

    If all goes well, you should see a response that looks similar to the following:

    Using network 'ropsten'.
    Running migration: 1_initial_migration.js
      Deploying Migrations...
      ... 0xd79bc3c5a7d338a7f85db9f86febbee738ebdec9494f49bda8f9f4c90b649db7
      Migrations: 0x0c6c4fc8831755595eda4b5724a61ff989e2f8b9
    Saving successful migration to network...
      ... 0xc37320561d0004dc149ea42d839375c3fc53752bae5776e4e7543ad16c1b06f0
    Saving artifacts...
    Running migration: 2_deploy_contracts.js
      Deploying MyContract...
      ... 0x7efbb3e4f028aa8834d0078293e0db7ff8aff88e72f33960fc806a618a6ce4d3
      MyContract: 0xda05d7bfa5b6af7feab7bd156e812b4e564ef2b1
    Saving successful migration to network...
      ... 0x6257dd237eb8b120c8038b066e257baee03b9c447c3ba43f843d1856de1fe132
    Saving artifacts...

    Note that your transaction IDs will be different from the ones above.

    Note: If you receive an error Error: Exceeds block gas limit, you may need to manually set the gas limit for your contract. See the Truffle Configuration documentation for details.

  3. If you want to verify that your contract was deployed successfully, you can check this on the Ropsten section of Etherscan. In the search field, type in the transaction ID for your contract. In the above example, the transaction ID is:


    You should see details about the transaction, including the block number where the transaction was secured.

Congratulations! You've deployed your contract to Ropsten using the combined power of Infura and Truffle.