Polygon API Overview
Welcome to the Polygon API Quickstart and Reference Guide. This guide provides everything you need to start building on Polygon using dRPC’s JSON-RPC API. Whether you're retrieving account balances, fetching block details, executing transactions, or estimating gas fees, this comprehensive guide will help you integrate Polygon seamlessly into your applications.
This page includes:
- A Quickstart Guide to help you set up and make your first Polygon API request.
- A detailed list of all Polygon API methods, categorized for easy reference.
Polygon API Quickstart#
To get started with building on Polygon using dRPC's Polygon RPC API, follow these steps:
1. Create a dRPC Account or Use Public Polygon RPC endpoints
By signing up for a free dRPC account, you can access their Premium Polygon RPC endpoints.
Public endpoints:
HTTPS
https://polygon.drpc.org/
WSS:
wss://polygon.drpc.org
2. Set Up Your Development Environment
RPC can be requested in various ways (opens in a new tab). In this example, we are going to use Node.js. Ensure you have Node.js (opens in a new tab) and npm installed on your system. You can verify their installation by running:
node -v
npm -v
If not installed, download and install them from the official Node.js website (opens in a new tab).
3. Initialize Your Project
Create a new directory for your project and initialize it:
mkdir Polygon-drpc-quickstart
cd Polygon-drpc-quickstart
npm init --yes
4. Install Axios
Axios is a popular HTTP client for making API requests. Install it using npm:
npm install axios
5. Obtain dRPC Endpoint
Log in to your dRPC account and navigate to the Polygon RPC endpoints section. Copy the HTTPS endpoint URL for the Polygon Mainnet.
6. Write Your First Script
Create an index.js
file in your project directory and add the following code:
const axios = require('axios');
const url = 'https://lb.drpc.org/ogrpc?network=polygon&dkey=YOUR_DRPC_API_KEY';
const payload = {
jsonrpc: '2.0',
id: 1,
method: 'eth_blockNumber',
params: []
};
axios.post(url, payload)
.then(response => {
console.log('The latest block number is', parseInt(response.data.result, 16));
})
.catch(error => {
console.error('Error fetching block number:', error);
});
Replace ‘https://lb.drpc.org/ogrpc?network=polygon&dkey=YOUR_DRPC_API_KEY'
with the actual endpoint URL you obtained from dRPC.
7. Run Your Script
Execute your script using Node.js:
node index.js
You should see the latest Polygon block number printed in your console.
Additional Information
dRPC offers various features such as high performance nodes, access to multiple chains, and MEV protection.
By following these steps, you can start building on Polygon using dRPC's reliable and efficient RPC endpoints. For more details, visit their Polygon Documentation (opens in a new tab)
Polygon API Methods#
Polygon is a Layer 2 scaling solution for Ethereum, designed to improve transaction speed and reduce costs while maintaining the security and functionality of the Ethereum blockchain.
Accounts info#
Retrieves essential data about an account, including its balance, contract code, and stored data.
eth_accounts : Retrieves a list of accounts managed by the client.
eth_getBalance : Returns the balance of an account.
eth_getCode : Fetches the contract code stored at a specific address.
eth_getProof : Provides a Merkle proof of an account's state.
eth_getStorageAt : Retrieves stored data from a specified slot within an account.
Blocks info#
Retrieves detailed information from a specified block, including transactions, receipts, and block data.
eth_blockNumber : Fetches the current block number.
eth_getBlockByHash : Retrieves block details by hash.
eth_getBlockByHash#full : Returns full block details by hash.
eth_getBlockByNumber : Fetches block data by block number.
eth_getBlockByNumber#full : Retrieves full block details by number.
eth_newBlockFilter : Available only on paid tier. Creates a filter for new blocks.
eth_getBlockReceipts : Fetches receipts of all transactions in a block.
eth_getBlockTransactionCountByHash : Returns the number of transactions in a block by hash.
eth_getBlockTransactionCountByNumber : Retrieves the transaction count for a block by number.
Chain info#
Provides key details about the Polygon network’s state and performance
eth_chainId : Returns the network's chain ID.
eth_protocolVersion : Retrieves the protocol version used by the client.
net_listening : Indicates whether the node is listening for network connections.
net_version : Returns the current network version.
net_peerCount : Displays the number of connected peers.
eth_syncing : Shows the synchronization status of the node.
eth_hashrate : Provides the network’s current mining hashrate.
Debug and trace#
Available only on paid tier. Provides advanced debugging and tracing tools to analyze and troubleshoot transactions and blocks on the Polygon network
trace_filter : Filters trace data based on criteria.
trace_block : Traces the execution of all transactions in a block.
trace_replayBlockTransactions : Replays block transactions for debugging.
trace_replayBlockTransactions#vmTrace : Provides VM-level trace during block replay.
debug_traceBlockByHash : Traces block execution by hash.
debug_traceBlockByNumber : Traces block execution by number.
trace_transaction : Traces a specific transaction.
debug_traceTransaction : Provides detailed trace information for a transaction.
trace_replayTransaction : Replays a transaction for debugging.
trace_replayTransaction#vmTrace : Provides VM-level trace during transaction replay.
trace_callMany : Executes multiple trace calls.
trace_get : Retrieves trace data based on criteria.
trace_call : Executes a trace call.
debug_traceCall : Traces a specific call without changing state.
Event logs#
Provides methods for filtering and retrieving event logs
eth_getLogs : Retrieves logs based on specified filter criteria.
eth_newFilter : Available only on paid tier. Creates a new filter for monitoring specific events.
eth_getFilterChanges : Available only on paid tier. Retrieves changes for an active filter since its last check.
eth_uninstallFilter : Uninstalls a specified filter.
eth_getFilterLogs : Available only on paid tier. Fetches logs matching a filter.
Executing transactions#
Provides essential methods for sending transactions, executing smart contract calls, and performing on-chain operations
eth_call : Executes a read-only call to a smart contract without modifying the blockchain state.
eth_sendRawTransaction : Sends a raw, signed transaction to the blockchain for execution.
Gas estimation#
Provides methods to estimate the gas required for transactions and fetch information related to gas prices
eth_feeHistory : Retrieves historical gas prices over a specified range of blocks.
eth_estimateGas : Estimates the gas needed for executing a transaction.
eth_gasPrice : Returns the current gas price on the network.
eth_createAccessList : Generates an access list to optimize gas usage.
eth_maxPriorityFeePerGas : Provides the maximum priority fee for gas.
Getting uncles#
Retrieves information about "uncle" blocks, which are valid but not included in the blockchain.
eth_getUncleCountByBlockHash : Returns the number of uncle blocks for a given block hash.
eth_getUncleCountByBlockNumber : Retrieves the number of uncle blocks for a specific block number.
Mining#
Contains methods related to mining operations
eth_coinbase : Returns the address of the miner receiving rewards.
eth_mining : Indicates whether the node is currently mining new blocks.
Subscriptions#
Allows to subscribe to specific events
eth_subscribe : Subscribes to various blockchain events like new blocks or logs.
eth_unsubscribe : Unsubscribes from an active event subscription.
Transactions info#
Provides methods to retrieve transaction details, counts, and receipts
eth_getTransactionByHash : Retrieves details of a transaction by its hash.
eth_getTransactionCount : Returns the number of transactions sent from an address.
eth_getTransactionReceipt : Fetches the receipt of a processed transaction.
eth_newPendingTransactionFilter : Available only on paid tier. Sets up a filter for monitoring pending transactions.
eth_getTransactionByBlockHashAndIndex : Retrieves a transaction by block hash and index.
eth_getTransactionByBlockNumberAndIndex : Fetches a transaction by block number and index.
txpool_content : Displays the contents of the transaction pool.
Web3#
Provides methods for retrieving basic network configuration data related to nodes
web3_clientVersion : Returns the version of the node client in use.
web3_sha3 : Computes the Keccak-256 (SHA3) hash of the provided input data.
Bor specific#
Contains methods for interacting with the Polygon Bor client, which is responsible for block production
bor_getAuthor : Retrieves the address of the block proposer for a given block.
bor_getCurrentProposer : Returns the current proposer responsible for producing the next block.
bor_getCurrentValidators : Retrieves the list of current validators.
bor_getRootHash : Returns the root hash of a block.
bor_getSignersAtHash : Fetches the list of signers for a block by its hash.