I am using Infura to interact with the Ropsten testnet. I would like to send a transaction from one account to another, but the following code does not work:

w3 = Web3(Web3.HTTPProvider('InfuraUrl'))

transaction = {
    'from': 'accountAddress',
    'to': 'accountAddress',
    'value': w3.toWei(1, "ether"),
    'gas': 2000000,
    'chainId': 3


I suppose I have to use the private key obtained from Metamask. Please help if you know how to send a transaction using a local private key.


Infura is a public hosted node. It has no way to unlock your local accounts. Even if there was a way, it would be a security risk because anyone can connect to the same node and transfer your funds.

In order to send a transaction over Infura you will need to sign the transaction locally using web3.eth.sendRawTransaction(). The following example is from the web3.py documentation:

>>> transaction = {
...     'to': '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
...     'value': 1000000000,
...     'gas': 2000000,
...     'gasPrice': 234567897654321,
...     'nonce': 0,
...     'chainId': 1
... }
>>> key = '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318'
>>> signed = w3.eth.account.sign_transaction(transaction, key)
>>> signed.rawTransaction
>>> signed.hash
>>> signed.r
>>> signed.s
>>> signed.v

# When you run sendRawTransaction, you get back the hash of the transaction:
>>> w3.eth.sendRawTransaction(signed.rawTransaction)  
  • Thanks, It work but I don't see wallet update on the Metamask – Marta Sędziwa Sep 30 '19 at 9:28
  • I'm unsure of how to trigger an update in MetaMask, sorry. – iamdefinitelyahuman Sep 30 '19 at 10:10

To get access to the private key for using web3.js or pyethereum, uou need to export it from Metamask, by clicking the "..." next to the account name, choose "Account Settings" and scrolling down until you see a box labeled "Private Key". Use that as the key variable in the pyethereum example above, and be careful not to remove it afterwards and not to share it with anyone. Afterwards, you can check the transaction hash on Etherscan, e.g. etherscan.io/tx/… (except that tx hash was never submitted in the pyethereum example above, so there are no results for it).

Good luck!


you can try web3-hdwallet-provider to sign transactions for addresses derived from a 12-word mnemonic.

const Web3 = require('web3');
const Web3HDWalletProvider = require('web3-hdwallet-provider');

const httpProvider = new Web3.providers.HttpProvider('InfuraUrl');
const mnemonic = 'YOUR PRIVATE KEY';
const web3 = new Web3HDWalletProvider(mnemonic, httpProvider)

transaction = {
'from': 'accountAddress',
'to': 'accountAddress',
'value': w3.toWei(1, "ether"),
'gas': 2000000,
'chainId': 3}


Note: be careful not to expose your private key; use the file path instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.