Polygon
Pronto para usar isso em produção?
O plano gratuito cobre projetos pessoais. O pay-as-you-go escala sem cartão de crédito.
Polygon
O plano gratuito cobre projetos pessoais. O pay-as-you-go escala sem cartão de crédito.
There are two main library choices for building on Polygon in JavaScript and TypeScript: web3.js and ethers.js. Both are actively maintained and both work unchanged against Polygon PoS, since chain ID 137 is EVM-equivalent — you just point the library at https://polygon.therpc.io/YOUR_API_KEY. For new projects we recommend ethers.js v6: it has stronger first-class TypeScript support and a smaller bundle size, which matters most for browser dApps where load time is user-facing.
Both web3.js and ethers.js are actively maintained and fully support Polygon. For new Polygon projects, prefer ethers.js v6 — its better TypeScript support and smaller bundle size make it the more comfortable default for both backend services and browser dApps.
web3.js is the original and one of the most widely used JavaScript APIs for EVM chains, so it remains a solid choice for Polygon and has a large pool of existing code and examples to draw on. Install it with npm install web3, then create a client by passing the Polygon endpoint straight into the constructor: new Web3('https://polygon.therpc.io/YOUR_API_KEY'). Balances are returned in wei, so convert with web3.utils.fromWei(...) to display MATIC.
ethers.js is a modern, compact library with first-class TypeScript support, which is why it is our default recommendation for new Polygon work. Install it with npm install ethers, then connect by constructing a JsonRpcProvider with the endpoint: new ethers.JsonRpcProvider('https://polygon.therpc.io/YOUR_API_KEY'). Use ethers.formatEther(...) to turn wei balances into readable MATIC amounts.
Both libraries ship their own type definitions, so there are no separate @types/* packages to install for either one when building on Polygon. That means you get IDE autocompletion across the full API surface, compile-time checking that catches mistakes before they reach the network, and types that double as inline documentation while you code.
Both web3.js and ethers.js run in Node.js, which makes them a good fit for Polygon backends, indexers, and bots. They support either module style: require() if your project uses CommonJS, or import if it is set up for ES modules — the connection to the Polygon endpoint is identical in both cases.
In the browser, import either library as an ES module and let a bundler like webpack or Vite package it for the web. When your Polygon dApp needs to act on behalf of a user — sending MATIC or signing transactions — connect to their injected wallet (such as MetaMask) with new ethers.BrowserProvider(window.ethereum) instead of a read-only RPC provider, so the wallet handles signing and the user approves each action.