Polygon API Examples
Real-world examples on how to query information from Polygon using modern Web3 libraries.
If you’re looking to start querying Polygon blockchain data, here’s the place to start! We’ll walk you through setting up Viem and Ethers.js with Alchemy and show you common use cases.
Getting Started with Polygon
This guide assumes you already have an Alchemy account and access to our Dashboard.
1. Create an Alchemy Key
To access Alchemy’s free node infrastructure, you need an API key to authenticate your requests.
You can create API keys from the dashboard. Check out this video on how to create an app, replacing the chain “Ethereum” with “Polygon”.
Or follow the written steps below:
First, navigate to the “create app” button in the “Apps” tab.
Fill in the details under “Create App” to get your new key. You can also see apps you previously made and those made by your team here. Make sure to select the Polygon chain.
Pull existing keys by clicking on “View Key” for any app.
You can also pull existing API keys by hovering over “Apps” and selecting one. You can “View Key” here, as well as “Edit App” to whitelist specific domains, see several developer tools, and view analytics.
2. Install and set up Web3 libraries
To get started with Polygon, you can use either Viem or Ethers.js. Create a project and install your preferred library:
Next, create a file named index.js
and add the following contents:
Replace demo
with your Alchemy API key from the dashboard.
Unfamiliar with the async stuff? Check out this Medium post.
3. Run your dApp using Node
You should now see the latest block number output in your console!
Below, we’ll list a number of examples on how to make common requests on Polygon or EVM blockchains.
How to Get the Latest Block Number on Polygon
If you’re looking for the latest block on Polygon, you can use the following:
How to Get a Block By Its Block Hash on Polygon
Every block on Polygon corresponds to a specific hash. If you’d like to look up a block by its hash, you can use the following code:
How to Get a Block By Its Block Number on Polygon
Every block on Polygon corresponds to a specific number. If you’d like to look up a block by its number, you can use the following code:
How to Get Logs for a Polygon Transaction
Logs are essentially a published list of user-defined events that have happened on the blockchain during a Polygon transaction. You can learn more about them in Understanding Logs: Deep Dive into eth_getLogs.
Here’s an example on how to write a getLogs query on Polygon:
How to Make an Eth_Call on Polygon
An eth_call in Polygon is essentially a way to execute a message call immediately without creating a transaction on the blockchain. It can be used to query internal contract state, to execute validations coded into a contract or even to test what the effect of a transaction would be without running it live.
Here’s an example on how to write a call query on Polygon:
How to Get a Transaction by Its Hash on Polygon
Every transaction on Polygon corresponds to a specific hash. If you’d like to look up a transaction by its hash, you can use the following code:
How to Get a Transaction Receipt on Polygon
Every transaction on Polygon has an associated receipt with metadata about the transaction, such as the gas used and logs printed during the transaction. If you’d like to look up a transaction’s receipt, you can use the following code:
How to get a User’s Transaction Count on Polygon
The number of transactions a user has sent is particularly important when it comes to calculating the nonce for sending new transactions on Polygon. Without it, you’ll be unable to send new transactions because your nonce won’t be set correctly.
How to Fetch Historical Transactions on Polygon
Oftentimes, you’ll want to look up a set of transactions on Polygon between a set of blocks, corresponding to a set of token types such as ERC20 or ERC721, or with certain attributes. Alchemy’s proprietary Transfers API allows you to do so in milliseconds, rather than searching every block on the blockchain.
How to Subscribe to New Blocks on Polygon
If you’d like to open a WebSocket subscription to send you a JSON object whenever a new block is published to the blockchain, you can set one up using the following syntax.
How to Estimate the Gas of a Transaction on Polygon
Oftentimes, you’ll need to calculate how much gas a particular transaction will use on the blockchain to understand the maximum amount that you’ll pay on that transaction. This example will return the gas used by the specified transaction:
How to Get the Current Gas Price in Polygon
You can retrieve the current gas price in wei using this method: