Error: CONNECTION ERROR: Couldn't connect to node https://mainnet.infura.io/v3/<my infura key>

Hello everyone can you please help me to solve this error : The conditions I want to run: I use NodeJs with Web3.js to find the balance of the account address in Blockchain using ‘getBalance()’ function. My code is run perfectly on ‘localhost’ and I get the correct account balance .For checking the result I return the balance using express route to the webpage. But the problem is… when we upload the program on the server it can’t get balance It gets error(i.e catch block is executed ) and return ‘{}’ empty parenthesis. For solving this error I try many approaches like:

  • Changing web3 provider.
  • Changing different address.
  • and I also use web3 ‘privateKeyToAccount(privateKey)’ function for getting account address using private key and I am geting excited that this function is executed successfully and give a correct address. But ‘getBalance’ function is not excuted correctly.
  • And I also use transaction function but it is also not run correctly.
  • I search many documentations and tutorials but I not get solution.
  • versions I use for nodejs ‘v18.14.1’ and for web3 ‘1.8.2’.

Here is my code:

const Web3 = require('web3');
const express = require('express');

const web3 = new Web3('https://mainnet.infura.io/v3/<MYINFURAKEY>');

const app = express();
app.use(express.json());

app.get('/', (req, res) => {

web3.eth.getBalance("0x7830c87C02e56AFf27FA8Ab1241711331FA86F43").then(balance => {
    res.send(balance);
  })
  .catch(error => {
      res.send(error);
  });
});

app.listen(3000, () => {
console.log('Server listening on port 3000');
});

I hope you guys give me my solution soon…

I try to get balance of account address using nodejs and web3. For ofline it executed correctly but when we upload on server it not execute successfully it gets error…

but after storing this error on DB i get this error…

1 Like

Hi @user63 and welcome to our community.

You can also try increasing the timeout for the web3 provider, as this may help resolve any connectivity issues. For example, you can try setting the timeout to 60 seconds by adding the following code:

const web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/v3/<MYINFURAKEY>', {timeout: 60000}));

If the above solutions do not work, it may be helpful to log any errors you are receiving to get more information on what is causing the issue. You can add additional logging statements to your code to help identify where the error is occurring:

const Web3 = require('web3');
const express = require('express');

const web3 = new Web3('https://mainnet.infura.io/v3/<MYINFURAKEY>');

const app = express();
app.use(express.json());

app.get('/', (req, res) => {

web3.eth.getBalance("0x7830c87C02e56AFf27FA8Ab1241711331FA86F43").then(balance => {
    console.log('Balance:', balance);
    res.send(balance);
  })
  .catch(error => {
      console.log('Error:', error);
      res.send(error);
  });
});

app.listen(3000, () => {
console.log('Server listening on port 3000');
});

With these additional logging statements, you can check the logs on the server to see if any errors are being generated and get more information on what is causing the issue.

2 Likes

I try this timeout function but this gives the same error … and I also check all the lines the error occur on getBalance() function and also try to change Rpc but same error is occur.
Can you give me the solution.

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.