[Ethereum] Project ID is required

nodejsweb3js

I got an error at sendSignedTransaction is : Project ID is required.
Can you please let me know that what is the actual problem?

This is my code:

try {
    const ethereumUri = 'https://ropsten.infura.io/API_KEY'; //process.env.API_URL

    var web3 = new Web3(new Web3.providers.HttpProvider(ethereumUri));

    const keystore = web3.eth.accounts.encrypt(req.body.privateKey, 'Drc@1234');

    const privateKey = new Buffer.from(req.body.privateKey.substr(2), 'hex')

    const decryptedAccount = web3.eth.accounts.decrypt(keystore, 'Drc@1234');

    var amount = web3.utils.toWei(req.body.transaction_value.toString(), "ether");

    var fee_eth = req.body.transaction_fee * 1000000000;
    var convertTOGwei = fee_eth / 21000;
    var decimalLimit = convertTOGwei.toFixed(2);
    var fee = web3.utils.toWei(decimalLimit.toString(),'gwei'); 

    var sender = req.body.from_address;
    var receiver = req.body.to_address;

    /*var rawTransaction = {
        "from": sender,
        "to": receiver,
        "gasPrice": web3.utils.toHex(fee),
        "value": web3.utils.toHex(amount),
        "gas": 21000,
        "chainId": 3
    };*/

    const txCount = await web3.eth.getTransactionCount(sender)

    // Transaction
     const txObject = {
        nonce:    web3.utils.toHex(txCount),
        to:       receiver,
        value:    web3.utils.toHex(amount),
        gasLimit: web3.utils.toHex(21000),
        gasPrice: web3.utils.toHex(fee)
     }

    // Sign the transaction
    const tx = new Tx(txObject, { chain: 'ropsten', hardfork: 'istanbul' })
    const pk = Buffer.from(privateKey, 'hex')
    tx.sign(pk)

    const serializedTx = tx.serialize()
    const raw = '0x' + serializedTx.toString('hex')
     // Broadcast the transaction
    const receipt = await web3.eth.sendSignedTransaction(raw)
    return receipt;
} catch (e) {
    console.error(`try/catch Error:(${e})`);
}

Best Answer

You need to:

  1. Sign up with Infura.io
  2. Login into Infura.io
  3. Click CREATE NEW PROJECT button and enter project name

Then in project details screen you will see your endpoint URL. Something like this: mainnet.infura.io/v3/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx. This URL you should use in your code. You may also configure additional security settings for your project, either to require project secret (good for backend applications) or to whitelist origins (good for frontends). Origin is basically where you frontend app is deployed. If you are just developing locally, origin like localhost:3000 could be a good choice.

Related Topic