How to use infura API with web3 js? Where should I put secret key?


1 Answer 1


First, import web3 as such:

const Web3 = require('web3')

Then instantiate a variable (in my case web3) and set a provider:

const web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/INFURA_ACCESS_TOKEN:8545'))

Then you can use all the available web3 methods.

Hope it helped you!

  • 1
    This metod stopped working in browser right? It is telling about CORS headers
    – gkucmierz
    Commented Nov 5, 2018 at 15:41
  • @gkucmierz I think you have to use NodeJS. Browsers won't allow CORS unless it's HTTP(S) apparently. Commented Sep 16, 2020 at 1:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.