Connecting to a Provider
RPC and wallets.
Connecting to a Provider is a free Web3 & DApp Development Fundamentals lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Web3 & DApp Development Fundamentals learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Talking to a Blockchain
To interact with Ethereum from JavaScript you need a provider — an object that connects to a node and lets you read chain data and send transactions.
Libraries like ethers.js and web3.js wrap the JSON-RPC protocol nodes speak.
Providers vs Signers
Two key abstractions:
- Provider — read-only connection to the chain (balances, blocks, view calls).
- Signer — holds a private key and can sign and send transactions.
You read with a provider and write with a signer.
A JSON-RPC Provider
The most common provider connects to an RPC endpoint such as Infura, Alchemy, or a local node:
const { ethers } = require("ethers");
const provider = new ethers.JsonRpcProvider(
"https://mainnet.example-rpc.io/KEY"
);With it you can immediately query public chain data.
const { ethers } = require("ethers");
const provider = new ethers.JsonRpcProvider(
"https://mainnet.example-rpc.io/KEY"
);Reading Network Info
Once connected, query basic network state:
const network = await provider.getNetwork();
console.log(network.chainId);
const block = await provider.getBlockNumber();
console.log("Latest block:", block);The chainId identifies which network you are on (1 = mainnet).
const network = await provider.getNetwork();
console.log(network.chainId);
const block = await provider.getBlockNumber();
console.log("Latest block:", block);Checking a Balance
Get an account's ETH balance through the provider:
const balance = await provider.getBalance("0xAbc...");
console.log(ethers.formatEther(balance), "ETH");The raw value is in wei (a BigInt); formatEther converts it to a human-readable ETH string.
const balance = await provider.getBalance("0xAbc...");
console.log(ethers.formatEther(balance), "ETH");Browser Wallets
In a browser dApp, the wallet (like MetaMask) injects a provider at window.ethereum. Wrap it with a BrowserProvider:
const provider = new ethers.BrowserProvider(window.ethereum);
await provider.send("eth_requestAccounts", []);The eth_requestAccounts call prompts the user to connect.
const provider = new ethers.BrowserProvider(window.ethereum);
await provider.send("eth_requestAccounts", []);Getting a Signer
From a browser provider, obtain the connected account's signer:
const signer = await provider.getSigner();
console.log("Connected as:", await signer.getAddress());This signer can now send transactions approved by the user in their wallet.
const signer = await provider.getSigner();
console.log("Connected as:", await signer.getAddress());Wallet from a Private Key
On a backend you can create a signer from a private key. Connect it to a provider so it can broadcast:
const wallet = new ethers.Wallet(
process.env.PRIVATE_KEY,
provider
);Never hardcode private keys — load them from environment variables.
const wallet = new ethers.Wallet(
process.env.PRIVATE_KEY,
provider
);web3.js Comparison
web3.js is the older alternative. Its connection looks like:
const { Web3 } = require("web3");
const web3 = new Web3("https://mainnet.example-rpc.io/KEY");
const block = await web3.eth.getBlockNumber();Concepts map closely, but ethers.js separates providers and signers more cleanly.
const { Web3 } = require("web3");
const web3 = new Web3("https://mainnet.example-rpc.io/KEY");
const block = await web3.eth.getBlockNumber();Keeping Secrets Safe
RPC keys and private keys are sensitive:
- Store them in
.env, never in source control. - Use read-only providers in front-end code.
- Private keys belong only on secure backends or in the user's own wallet.
Default and Fallback Providers
ethers can combine several backends for reliability. A fallback provider queries multiple endpoints and agrees on a result:
const provider = ethers.getDefaultProvider("mainnet", {
alchemy: process.env.ALCHEMY_KEY,
infura: process.env.INFURA_KEY,
});If one backend is down, others keep your app working.
const provider = ethers.getDefaultProvider("mainnet", {
alchemy: process.env.ALCHEMY_KEY,
infura: process.env.INFURA_KEY,
});Quick Check
Test your understanding of providers and signers.
Recap
You learned to connect to a blockchain.
- A provider reads chain data; a signer sends transactions.
JsonRpcProviderconnects to an RPC URL;BrowserProviderwraps wallet injections.getSignerreturns the connected account;new Wallet(key, provider)signs on a backend.- Values are in wei; use
formatEtherto display them. - Keep RPC and private keys in
.env, never in code.
Frequently asked questions
Is the “Connecting to a Provider” lesson free?
Yes — the full text of “Connecting to a Provider” is free to read here on the web, and the Web3 & DApp Development Fundamentals course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Web3 & DApp Development Fundamentals course, upgrade to CoddyKit PRO.
What will I learn in “Connecting to a Provider”?
RPC and wallets. You practise Web3 & DApp Development Fundamentals with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Web3 & DApp Development Fundamentals?
No prior experience is required. Web3 & DApp Development Fundamentals on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Connecting to a Provider” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Web3 & DApp Development Fundamentals lesson?
Yes. Every Web3 & DApp Development Fundamentals lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Connecting to a Provider
- Reading Contract Data
- Sending Transactions
- Listening to Events