Logo
RPC Assistant

How do I set up a custom RPC for Optimism?

Summary

Setting up a custom RPC for Optimism means configuring your wallet or dApp to use a specific endpoint instead of the default public one. This guide covers the chain settings you need, how to add Optimism to MetaMask, and how to choose a reliable RPC provider for production use.

Quick Recommendation: When to Use a Custom RPC for Optimism

If you're reading this, you've likely hit a rate limit on the public Optimism endpoint or need WebSocket support for real-time data. A custom RPC gives you a dedicated endpoint that you control, which is essential for production dApps, trading bots, or any application that depends on consistent access to OP Mainnet.

For most developers, the decision comes down to three scenarios:

  • You're just testing or building a small project: The public endpoint https://mainnet.optimism.io is fine for development, but it's rate-limited and doesn't support WebSockets.
  • You're deploying a production app: You need a reliable, scalable RPC provider. OnFinality offers a managed Optimism RPC endpoint that you can use with a simple API key.
  • You need WebSocket subscriptions: For real-time event listening, you'll need a provider that supports WSS. OnFinality's Optimism endpoint supports both HTTP and WebSocket.

If you're unsure which path to take, start with a free tier from a provider like OnFinality and monitor your usage. You can always upgrade to a dedicated node later if your needs grow.

Optimism Chain Settings at a Glance

Before you can connect to Optimism, you need the correct network parameters. Here's a quick reference table for OP Mainnet and OP Sepolia:

ParameterOP MainnetOP Sepolia
Network NameOP MainnetOP Sepolia
Chain ID1011155420
Currency SymbolETHETH
Block Explorerhttps://optimistic.etherscan.iohttps://sepolia-optimism.etherscan.io
Public RPC URLhttps://mainnet.optimism.iohttps://sepolia.optimism.io
OnFinality Public RPChttps://optimism.api.onfinality.io/publichttps://optimism-sepolia.api.onfinality.io/public

These settings are what you'll need to add Optimism to MetaMask or configure your dApp. The OnFinality public endpoints are rate-limited but offer a more reliable alternative to the official public ones, and they support both HTTP and WebSocket.

How to Add Optimism to MetaMask with a Custom RPC

Adding Optimism to MetaMask is a common first step. Here's how to do it manually:

  1. Open MetaMask and click the network dropdown at the top.
  2. Click Add Network.
  3. At the bottom, click Add a network manually.
  4. Fill in the following details:
    • Network Name: OP Mainnet
    • New RPC URL: https://optimism.api.onfinality.io/public
    • Chain ID: 10
    • Currency Symbol: ETH
    • Block Explorer URL: https://optimistic.etherscan.io
  5. Click Save.

You can now switch to OP Mainnet and interact with the network. For OP Sepolia, use the corresponding settings from the table above.

Connecting to Optimism with ethers.js and viem

Once you have your RPC URL, you can connect to Optimism using popular JavaScript libraries. Here's an example using ethers.js:

const { ethers } = require("ethers");

const provider = new ethers.JsonRpcProvider("https://optimism.api.onfinality.io/public");

async function getBlockNumber() {
  const blockNumber = await provider.getBlockNumber();
  console.log("Current block number:", blockNumber);
}

getBlockNumber();

And with viem:

import { createPublicClient, http } from 'viem';
import { optimism } from 'viem/chains';

const client = createPublicClient({
  chain: optimism,
  transport: http('https://optimism.api.onfinality.io/public')
});

const blockNumber = await client.getBlockNumber();
console.log(blockNumber);

For WebSocket subscriptions, you can use the WSS endpoint. OnFinality's public endpoint supports WebSocket, so you can do:

const provider = new ethers.WebSocketProvider("wss://optimism.api.onfinality.io/public");

provider.on("block", (blockNumber) => {
  console.log("New block:", blockNumber);
});

Testing Your RPC Endpoint with curl

Before integrating, you can test your endpoint with a simple curl command. Here's how to check the current block number:

curl https://optimism.api.onfinality.io/public \
  -X POST \
  -H "Content-Type: application/json" \
  --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}'

You should receive a response like:

{"jsonrpc":"2.0","result":"0x10d4f","id":1}

The result is a hex-encoded block number. This confirms your endpoint is working.

Common Failure Modes and How to Debug Them

Even with a custom RPC, you may run into issues. Here are some common problems and how to fix them:

  • Rate limiting: If you're getting 429 errors, your requests are being throttled. This is common on public endpoints. Solution: Use a provider with higher rate limits, like OnFinality's paid plans.
  • WebSocket connection drops: If your WebSocket connection is unstable, check your network and ensure you're using the correct WSS URL. Some providers don't support WebSockets on free tiers.
  • Chain ID mismatch: If your transactions fail with an error about chain ID, make sure your wallet or dApp is configured with the correct chain ID (10 for OP Mainnet).
  • Out of sync node: If you're running your own node, it may be behind the chain tip. This can cause stale reads. Consider using a managed provider to avoid this.

How to Choose an Optimism RPC Provider

When evaluating RPC providers for Optimism, consider the following criteria:

CriterionWhat to CheckWhy It Matters
UptimeHistorical uptime statsDowntime means your app is unavailable
Rate LimitsRequests per second (RPS) allowedHigh-traffic apps need higher limits
WebSocket SupportWSS endpoint availabilityNeeded for real-time features
Archive DataAccess to historical stateRequired for certain analytics and debugging
PricingFree tier and paid plansMust fit your budget and scale
SupportSLA and response timesCritical for production issues

OnFinality offers a managed Optimism RPC service with HTTP and WebSocket support, and you can start with a free tier. For production workloads, you can scale to dedicated nodes for more predictable performance. Check our RPC pricing for details.

Production Readiness Checklist

Before going live with your Optimism dApp, run through this checklist:

  • Use a reliable RPC provider (not the public endpoint)
  • Configure fallback endpoints in case of provider outage
  • Monitor your RPC usage and set alerts for rate limit thresholds
  • Test WebSocket subscriptions if you rely on real-time data
  • Verify chain ID and network settings in your wallet and dApp
  • Ensure your provider supports the methods you need (e.g., eth_getLogs for event indexing)

Key Takeaways

  • A custom RPC for Optimism gives you control over rate limits, WebSocket support, and reliability.
  • The chain settings for OP Mainnet are chain ID 10, symbol ETH, and explorer https://optimistic.etherscan.io.
  • You can use OnFinality's public endpoint for development, but for production, consider a paid plan or dedicated node.
  • Always test your endpoint with curl or a simple script before integrating.
  • Choose a provider based on uptime, rate limits, WebSocket support, and pricing.

Frequently Asked Questions

What is a custom RPC? A custom RPC is a specific endpoint you configure in your wallet or dApp to connect to a blockchain network. Instead of using the default public endpoint, you can use a provider's endpoint that offers better performance and reliability.

Why do I need a custom RPC for Optimism? The public Optimism endpoint is rate-limited and doesn't support WebSockets. A custom RPC from a provider like OnFinality gives you higher limits, WebSocket support, and more consistent uptime.

How do I add Optimism to MetaMask? Go to MetaMask > Add Network > Add a network manually, then enter the network name, RPC URL, chain ID, symbol, and explorer URL. You can use the settings from the table above.

Can I use a custom RPC for free? Yes, many providers offer free tiers. OnFinality has a free public endpoint, but for production use, you'll likely need a paid plan to get higher rate limits and support.

What is the difference between HTTP and WebSocket RPC? HTTP is for request-response interactions, like fetching balances or sending transactions. WebSocket maintains a persistent connection for real-time updates, such as new block notifications or event logs.

How do I test my Optimism RPC endpoint? Use curl to send a JSON-RPC request like eth_blockNumber. If you get a valid response, your endpoint is working.

What should I do if I get rate limited? Upgrade to a plan with higher rate limits, or use multiple endpoints with failover. OnFinality's paid plans offer higher RPS and dedicated nodes for demanding workloads.

Does OnFinality support Optimism Sepolia? Yes, OnFinality provides a public endpoint for Optimism Sepolia: https://optimism-sepolia.api.onfinality.io/public. You can find more details on our Optimism network page.

RPC Knowledge Base

Related RPC details

Never Worry about Infrastructure Again

OnFinality takes away the heavy lifting of DevOps so you can build smarter and faster.

Get Started