Skip to content

Public_rpc

Chapter 20: Setting Up Public RPC Endpoints

Section titled “Chapter 20: Setting Up Public RPC Endpoints”

Public RPC endpoints allow applications to connect to blockchain networks without running your own node. Understanding how to set up and use public RPC endpoints is essential for building blockchain applications.


┌─────────────────────────────────────────────────────────────────────────────┐
│ RPC ENDPOINT TYPES │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ PUBLIC ENDPOINTS │ │
│ │ - Free to use (rate limited) │ │
│ │ - Shared infrastructure │ │
│ │ - Best for development/testing │ │
│ │ Examples: infura.io, alchemy.com, ankr.com │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ PRIVATE ENDPOINTS │ │
│ │ - Dedicated infrastructure │ │
│ │ - Higher throughput and reliability │ │
│ │ - Requires authentication │ │
│ │ Examples: Paid plans on Infura, Alchemy, QuickNode │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ SELF-HOSTED ENDPOINTS │ │
│ │ - Run your own node │ │
│ │ - Full control over infrastructure │ │
│ │ - Highest reliability and privacy │ │
│ │ Examples: Geth, Erigon, Nethermind │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘

ProviderFree TierNetworksFeatures
Infura100k req/dayEthereum, Polygon, Arbitrum, OptimismWebSocket, Archive
Alchemy100k req/dayEthereum, Polygon, Solana, ArbitrumWebSocket, NFT API
AnkrUnlimitedEthereum, BSC, Polygon, SolanaPublic & Premium
QuickNode100k req/dayMulti-chainGrowth features
Tenderly5M req/monthMulti-chainDebugging tools
┌─────────────────────────────────────────────────────────────────┐
│ PUBLIC RPC ENDPOINTS │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ETHEREUM MAINNET │
│ ━━━━━━━━━━━━━━━━━━━ │
│ https://ethereum.publicnode.com │
│ https://eth.llamarpc.com │
│ https://rpc.flashbots.net │
│ │
│ ETHEREUM GOERLI (Testnet) │
│ ━━━━━━━━━━━━━━━━━━━━━━━ │
│ https://goerli.infura.io/v3/your-project-id │
│ https://eth-goerli.g.alchemy.com/v2/your-api-key │
│ │
│ POLYGON │
│ ━━━━━━━━ │
│ https://polygon-rpc.com │
│ https://rpc.ankr.com/polygon │
│ │
│ BINANCE SMART CHAIN │
│ ━━━━━━━━━━━━━━━━━━━━━━━ │
│ https://bsc-dataseed.binance.org │
│ https://rpc.ankr.com/bsc │
│ │
│ ARBITRUM │
│ ━━━━━━━━━ │
│ https://arb1.arbitrum.io/rpc │
│ https://rpc.ankr.com/arbitrum_one │
│ │
│ OPTIMISM │
│ ━━━━━━━━━━ │
│ https://mainnet.optimism.io │
│ https://rpc.ankr.com/optimism │
│ │
└─────────────────────────────────────────────────────────────────┘

  1. Visit infura.io
  2. Sign up for free account
  3. Create new project
┌─────────────────────────────────────────────────────────────────┐
│ INFURA PROJECT SETUP │
├─────────────────────────────────────────────────────────────────┤
│ │
│ After creating project, you'll receive: │
│ │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ Project ID: abc123def456... │ │
│ │ Project Secret: ************** │ │
│ └─────────────────────────────────────────────────────────┘ │
│ │
│ Endpoint URL format: │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ HTTP: https://mainnet.infura.io/v3/{PROJECT_ID} │ │
│ │ WSS: wss://mainnet.infura.io/ws/v3/{PROJECT_ID} │ │
│ └─────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────┘
Terminal window
# Test with cURL
curl -X POST https://mainnet.infura.io/v3/YOUR_PROJECT_ID \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "eth_blockNumber",
"params": [],
"id": 1
}'

  1. Visit alchemy.com
  2. Sign up for free account
  3. Create new app
┌─────────────────────────────────────────────────────────────────┐
│ ALCHEMY PROJECT SETUP │
├─────────────────────────────────────────────────────────────────┤
│ │
│ After creating app, you'll receive: │
│ │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ API Key: abc123... │ │
│ └─────────────────────────────────────────────────────────┘ │
│ │
│ Endpoint URL format: │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ HTTP: https://eth-mainnet.g.alchemy.com/v2/{API_KEY}│ │
│ │ WSS: wss://eth-mainnet.g.alchemy.com/ws/v2/{API_KEY}│ │
│ └─────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────┘
Terminal window
# Test with cURL
curl -X POST https://eth-mainnet.g.alchemy.com/v2/YOUR_API_KEY \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "eth_blockNumber",
"params": [],
"id": 1
}'

Terminal window
# .env file
ETHEREUM_RPC_URL=https://mainnet.infura.io/v3/YOUR_PROJECT_ID
ETHEREUM_WSS_URL=wss://mainnet.infura.io/ws/v3/YOUR_PROJECT_ID
POLYGON_RPC_URL=https://polygon-rpc.com
config.js
require('dotenv').config();
const networks = {
mainnet: {
rpcUrl: process.env.ETHEREUM_RPC_URL,
chainId: 1,
name: 'Ethereum Mainnet'
},
goerli: {
rpcUrl: process.env.GOERLI_RPC_URL || 'https://goerli.infura.io/v3/' + process.env.INFURA_PROJECT_ID,
chainId: 5,
name: 'Goerli Testnet'
},
polygon: {
rpcUrl: process.env.POLYGON_RPC_URL || 'https://polygon-rpc.com',
chainId: 137,
name: 'Polygon Mainnet'
}
};
module.exports = networks;
config.py
import os
from web3 import Web3
NETWORKS = {
'mainnet': {
'rpc_url': os.getenv('ETHEREUM_RPC_URL'),
'chain_id': 1,
'name': 'Ethereum Mainnet'
},
'goerli': {
'rpc_url': os.getenv('GOERLI_RPC_URL'),
'chain_id': 5,
'name': 'Goerli Testnet'
},
'polygon': {
'rpc_url': os.getenv('POLYGON_RPC_URL'),
'chain_id': 137,
'name': 'Polygon Mainnet'
}
}
def get_web3(network='mainnet'):
rpc_url = NETWORKS[network]['rpc_url']
return Web3(Web3.HTTPProvider(rpc_url))

Terminal window
# Start Geth with RPC enabled
geth \
--mainnet \
--http \
--http.addr "0.0.0.0" \
--http.port 8545 \
--http.api "eth,net,web3,debug,txpool" \
--http.corsdomain "*" \
--ws \
--ws.addr "0.0.0.0" \
--ws.port 8546 \
--ws.api "eth,net,web3" \
--ws.origins "*" \
--syncmode "snap" \
--datadir /data/geth
Terminal window
# Start Erigon with RPC
erigon \
--chain mainnet \
--http \
--http.addr "0.0.0.0" \
--http.port 8545 \
--http.api "eth,net,web3,debug,txpool" \
--ws \
--ws.addr "0.0.0.0" \
--ws.port 8546 \
--datadir /data/erigon
/etc/nginx/sites-available/rpc
server {
listen 443 ssl http2;
server_name your-rpc-domain.com;
ssl_certificate /etc/letsencrypt/live/your-rpc-domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your-rpc-domain.com/privkey.pem;
location / {
proxy_pass http://127.0.0.1:8545;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# Rate limiting
limit_req zone=rpc_limit burst=20 nodelay;
}
}

ProviderFree Tier Limit
Infura100k requests/day
Alchemy100k requests/day
AnkrUnlimited (throttled)
QuickNode100k requests/day
┌─────────────────────────────────────────────────────────────────┐
│ RPC BEST PRACTICES │
├─────────────────────────────────────────────────────────────────┤
│ │
│ 1. USE ENVIRONMENT VARIABLES │
│ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━ │
│ Never hardcode API keys in source code │
│ │
│ 2. IMPLEMENT FALLBACK │
│ ━━━━━━━━━━━━━━━━━━━━━━━━ │
│ Have backup RPC providers for redundancy │
│ │
│ 3. CACHE RESPONSES │
│ ━━━━━━━━━━━━━━━━━━━━ │
│ Cache non-changing data (balances, metadata) │
│ │
│ 4. USE WEBSOCKETS FOR REAL-TIME │
│ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ │
│ Better for subscriptions and events │
│ │
│ 5. BATCH REQUESTS │
│ ━━━━━━━━━━━━━━━━ │
│ Combine multiple reads into single request │
│ │
│ 6. MONITOR USAGE │
│ ━━━━━━━━━━━━━━ │
│ Track request counts to avoid rate limits │
│ │
└─────────────────────────────────────────────────────────────────┘

QuestionAnswer
What is an RPC endpoint?Remote procedure call interface for blockchain interaction
What are popular RPC providers?Infura, Alchemy, Ankr, QuickNode
Why use WebSocket over HTTP?Real-time updates, subscriptions, less overhead
How do you secure RPC endpoints?API keys, rate limiting, firewall rules
What’s the difference between public and private RPC?Public is shared/free, private is dedicated
How do you handle rate limiting?Implement exponential backoff, use fallback providers

  • Public RPC endpoints enable blockchain interaction without running nodes
  • Popular providers: Infura, Alchemy, Ankr
  • Set up involves creating account and getting API keys
  • Can also self-host RPC nodes using Geth or Erigon
  • Implement fallback and caching for production use

In Chapter 21: WebSocket Connections, we’ll explore WebSocket-based real-time communication.


Last Updated: 2026-02-20