Optimism
准备好在生产环境中调用了吗?
免费套餐涵盖个人项目。按量付费,无需绑卡即可扩展。
Optimism
免费套餐涵盖个人项目。按量付费,无需绑卡即可扩展。
For JavaScript and TypeScript work on OP Mainnet you have two well-established choices: web3.js, the original and most widely deployed library, and ethers.js, the leaner modern alternative. Both are actively maintained and both speak the standard eth_* JSON-RPC that OP Mainnet (chain ID 10, ETH for gas) exposes as an EVM-equivalent rollup. For new projects we recommend ethers.js v6 — it ships stronger TypeScript types and a noticeably smaller bundle, which matters for the browser-based Superchain dApps that front protocols like Velodrome.
Both web3.js and ethers.js are actively maintained and production-ready against OP Mainnet. For a fresh build, reach for ethers.js v6 first: its first-class TypeScript support and smaller bundle make it the better default for new Optimism projects.
web3.js is the original and still most widely used JavaScript API for talking to OP Mainnet, with a deep ecosystem of examples and plugins behind it. Install it with npm install web3, then instantiate a client by passing the OP Mainnet endpoint straight to the constructor: new Web3('https://optimism.therpc.io/YOUR_API_KEY'). That object exposes the full web3.eth namespace for balances, gas, and transactions.
ethers.js is the modern, compact library with first-class TypeScript support, which makes it a natural pick for typed OP Mainnet front ends and backends. Install it with npm install ethers, then connect by passing the endpoint to a JsonRpcProvider: new ethers.JsonRpcProvider('https://optimism.therpc.io/YOUR_API_KEY'). The provider is your read gateway to chain ID 10 — balances, blocks, and call data.
Both web3.js and ethers.js bundle their own TypeScript declarations, so you can import and use them on OP Mainnet with no separate @types/* package. That built-in typing pays off three ways: IDE autocompletion across the whole API, compile-time catches when a parameter or return shape is wrong, and types that double as inline documentation while you code.
Both libraries run unchanged in Node.js, whether your project uses CommonJS require() or ESM import. That makes them a good fit for OP Mainnet backend services, indexers, and cron jobs that need to read chain ID 10 state or relay transactions away from the browser.
In the browser, import either library as an ES module and let a bundler like webpack or Vite tree-shake it into your OP Mainnet dApp. When you need the user's own wallet rather than a read-only endpoint — for example to have them sign a swap on a Superchain app — wrap the injected provider with new ethers.BrowserProvider(window.ethereum), which surfaces MetaMask and other EIP-1193 wallets through the same ethers API.