Public_rpc
Chapter 20: Setting Up Public RPC Endpoints
Section titled “Chapter 20: Setting Up Public RPC Endpoints”Overview
Section titled “Overview”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.
20.1 Types of RPC Endpoints
Section titled “20.1 Types of RPC Endpoints”┌─────────────────────────────────────────────────────────────────────────────┐│ 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 │ ││ └─────────────────────────────────────────────────────────────────────┘ ││ │└─────────────────────────────────────────────────────────────────────────────┘20.2 Popular RPC Providers
Section titled “20.2 Popular RPC Providers”Comparison
Section titled “Comparison”| Provider | Free Tier | Networks | Features |
|---|---|---|---|
| Infura | 100k req/day | Ethereum, Polygon, Arbitrum, Optimism | WebSocket, Archive |
| Alchemy | 100k req/day | Ethereum, Polygon, Solana, Arbitrum | WebSocket, NFT API |
| Ankr | Unlimited | Ethereum, BSC, Polygon, Solana | Public & Premium |
| QuickNode | 100k req/day | Multi-chain | Growth features |
| Tenderly | 5M req/month | Multi-chain | Debugging tools |
Public (Free) Endpoints
Section titled “Public (Free) Endpoints”┌─────────────────────────────────────────────────────────────────┐│ 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 ││ │└─────────────────────────────────────────────────────────────────┘20.3 Setting Up Infura
Section titled “20.3 Setting Up Infura”Step 1: Create Account
Section titled “Step 1: Create Account”- Visit infura.io
- Sign up for free account
- Create new project
Step 2: Get Endpoint
Section titled “Step 2: Get Endpoint”┌─────────────────────────────────────────────────────────────────┐│ 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} │ ││ └─────────────────────────────────────────────────────────┘ ││ │└─────────────────────────────────────────────────────────────────┘Step 3: Test Connection
Section titled “Step 3: Test Connection”# Test with cURLcurl -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 }'20.4 Setting Up Alchemy
Section titled “20.4 Setting Up Alchemy”Step 1: Create Account
Section titled “Step 1: Create Account”- Visit alchemy.com
- Sign up for free account
- Create new app
Step 2: Get API Key
Section titled “Step 2: Get API Key”┌─────────────────────────────────────────────────────────────────┐│ 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}│ ││ └─────────────────────────────────────────────────────────┘ ││ │└─────────────────────────────────────────────────────────────────┘Step 3: Test Connection
Section titled “Step 3: Test Connection”# Test with cURLcurl -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 }'20.5 Configuring Your Application
Section titled “20.5 Configuring Your Application”Environment Variables
Section titled “Environment Variables”# .env fileETHEREUM_RPC_URL=https://mainnet.infura.io/v3/YOUR_PROJECT_IDETHEREUM_WSS_URL=wss://mainnet.infura.io/ws/v3/YOUR_PROJECT_IDPOLYGON_RPC_URL=https://polygon-rpc.comJavaScript Configuration
Section titled “JavaScript Configuration”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;Python Configuration
Section titled “Python Configuration”import osfrom 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))20.6 Setting Up Your Own RPC Node
Section titled “20.6 Setting Up Your Own RPC Node”Using Geth
Section titled “Using Geth”# Start Geth with RPC enabledgeth \ --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/gethUsing Erigon (Faster Sync)
Section titled “Using Erigon (Faster Sync)”# Start Erigon with RPCerigon \ --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/erigonNginx Reverse Proxy
Section titled “Nginx Reverse Proxy”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; }}20.7 Rate Limiting & Best Practices
Section titled “20.7 Rate Limiting & Best Practices”Rate Limits by Provider
Section titled “Rate Limits by Provider”| Provider | Free Tier Limit |
|---|---|
| Infura | 100k requests/day |
| Alchemy | 100k requests/day |
| Ankr | Unlimited (throttled) |
| QuickNode | 100k requests/day |
Best Practices
Section titled “Best Practices”┌─────────────────────────────────────────────────────────────────┐│ 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 ││ │└─────────────────────────────────────────────────────────────────┘20.8 Interview Questions
Section titled “20.8 Interview Questions”| Question | Answer |
|---|---|
| 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 |
Summary
Section titled “Summary”- 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
Next Chapter
Section titled “Next Chapter”In Chapter 21: WebSocket Connections, we’ll explore WebSocket-based real-time communication.
Last Updated: 2026-02-20