I have a webpage that talks to a smart contract on testrpc. I connect using: web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

I have deployed my contract to Ropsten Test Network with Metamask. How do I change my webpage to point to the Ropsten test network? Is it as simple as changing the web address from "localhost" to a Ropsten web address?



5 Answers 5


If the Geth node on the localhost is on Ropsten network i.e. you started geth with a networkid of 3 , then your webpage will point to Test network (Ropsten) by default


Edit : As i did a little playing around on Metamask plugin i could see how to change enter image description here

Localhost is nothing to do with your question unless you try to connect remote Ethereum clients which are running on Robsten. It looks like you are using Metamask's plugin as an Ethereum client (you can use Geth or Parity too). If this is your case you will continue use localhost to connect your Ethereum client and change check blockchain network as it is documented in Github repo of MetaMask :

web3.version.getNetwork((err, netId) => {
  switch (netId) {
    case "1":
      console.log('This is mainnet')
    case "2":
      console.log('This is the deprecated Morden test network.')
    case "3":
      console.log('This is the ropsten test network.')
    case "4":
      console.log('This is the Rinkeby test network.')
    case "42":
      console.log('This is the Kovan test network.')
      console.log('This is an unknown network.')

In case you are using Geth client you need to run your Geth client with : --testnet flag, or if you are using parity you need to use --chain ropsten flag. Check documentation for further reading.


You'll have to set up a Ropsten node either remotely or locally. You can use the infura.io node by subscribing to their service. They will email you a link of the format https://ropsten.infura.io/API_KEY which you can add in your code like this:

web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/API_KEY"));

var HDWalletProvider = require("truffle-hdwallet-provider");

var infura_apikey = "Your infura key";<br>
var mnemonic = "Your 12 word metamask key";<br>

module.exports = {
    networks: {
    development: {
    host: "localhost",
      port: 8545,
      network_id: "*" // Match any network id
    ropsten: {
      provider: new HDWalletProvider(mnemonic, "https://ropsten.infura.io/"+infura_apikey),
      network_id: 3,
      gas: 500000      

Add the above code in your truffle.js
and then run the command at your terminal
truffle deploy --network='ropsten' --reset


Even easier. The MetaMask plugin automatically injects itself. See their guide.

  • 1
    Does this mean you expect users to enter their private keys on your website?
    – user19510
    Jan 8, 2018 at 4:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.