Eth getBalance "basic auth failure\n"

Hello, I am using web3js for requesting infura.io.

var mainNetHttpHost = ‘https://mainnet.infura.io/v3/MY-PROJECT-ID’;
var mainNetHttpProvider = new web3js.providers.HttpProvider(this.mainNetHttpHost);
mainNetHttp = new web3js(mainNetHttpProvider);
mainNetHttp.eth.getBalance(‘ETH-wallet-addr’);

But I get the error.
Error: Invalid JSON RPC response: “basic auth failure\n”

I tested using postman is success.
What’ wrong?? ㅠ.ㅜ
please help me…

Hi @joo welcome to our community!

Are you able to issue the same command for getBalance directly using a curl to verify that your Project ID is functioning properly?

From our documentation: https://infura.io/docs/ethereum/json-rpc/eth_getBalance

curl https://mainnet.infura.io/v3/YOUR-PROJECT-ID \
    -X POST \
    -H "Content-Type: application/json" \
    -d '{"jsonrpc":"2.0","method":"eth_getBalance","params": ["0xc94770007dda54cF92009BFF0dE90c06F603a09f", "latest"],"id":1}'

Hi @joo,
Check your env variable. Please set it if you haven’t yet.

Thank you

1 Like