Search code examples
node.jsweb3js

How to connect to a protected infura project with web3?


I'm using web3 in node js to create new Web3.providers.HttpProvider with a infura project, which is configure with "Require project secret for all requests"

In infura:

https://infura.io/docs/gettingStarted/authentication

explains calling by curl is:

curl --user :YOUR-PROJECT-SECRET
https://.infura.io/v3/YOUR-PROJECT-ID

I've used:

const client = new Web3(new Web3.providers.HttpProvider('https://YOUR-PROJECT-SECRET@<network>.infura.io/v3/YOUR-PROJECT-ID');

And it doesn't work.

How can I add the --user tag and its value into new Web3.providers.HttpProvider, please?

Thanks in advance!


Solution

  • I'm awsering my question:

    I solved it adding ":" before YOUR-PROJECT-SECRET, like this:

    const client = new Web3(new Web3.providers.HttpProvider('https://:YOUR-PROJECT-SECRET@<network>.infura.io/v3/YOUR-PROJECT-ID');
    

    It's working now!