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

Hey @joo , for MY-PROJECT-ID , sign up on infura.io and insert your own project’s API key. That should fix it.

@Sean_Infura @wuehler I’m getting the same issue with just using the curl command… I tried generating a new project key but same error not sure what im missing? Am I suppose to give it the secret key too?

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

basic auth failure`

Nevermind… looks like I was using a v2 project (even though the project page said to use v3 it didnt work). I tried with a new project and it finally worked.

1 Like

Glad to hear you got it working @alkaidiver! Any other questions at all we’re here to help.