Fantom API Overview
Welcome to the Fantom API Quickstart and Reference Guide. This guide provides everything you need to start building on Fantom 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 Fantom seamlessly into your applications.
This page includes:
- A Quickstart Guide to help you set up and make your first Fantom API request.
- A detailed list of all Fantom API methods, categorized for easy reference.
Fantom API Quickstart#
To get started with building on Fantom using dRPC's Fantom RPC API, follow these steps:
1. Create a dRPC Account or Use Public Fantom RPC endpoints
By signing up for a free dRPC account, you can access their Premium Fantom RPC endpoints.
Public endpoints:
HTTPS
https://fantom.drpc.org/
WSS:
wss://fantom.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 Fantom-drpc-quickstart
cd Fantom-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 Fantom RPC endpoints section. Copy the HTTPS endpoint URL for the Fantom 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=fantom&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=fantom&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 Fantom block number printed in your console.
Additional Information
dRPC offers various features such as high performance nodes, access to multiple chains.
By following these steps, you can start building on Fantom using dRPC's reliable and efficient RPC endpoints. For more details use Fantom Documentation (opens in a new tab).
Fantom API Methods#
Fantom provides a robust set of API endpoints for retrieving information about blocks, transactions, balances, logs, and more, enabling seamless blockchain development and integration.
Accounts info#
Retrieve details about account balances, contract code, and stored data on the Fantom blockchain.
eth_accounts : Lists all accounts managed by the client.
eth_getBalance : Retrieves the balance of an account in Wei.
eth_getCode : Fetches the smart contract code deployed at a specific address.
eth_getProof : Generates a Merkle proof for an account or its storage data.
eth_getStorageAt : Accesses the value stored at a particular storage slot of an account.
Blocks info#
Access detailed information about blocks, including transactions, receipts, and metadata.
eth_blockNumber : Retrieves the latest block number on the chain.
eth_getBlockByHash : Fetches the details of a block using its unique hash.
eth_getBlockByHash#full : Obtains complete information of a block by its hash.
eth_getBlockByNumber : Accesses block details based on its numerical position in the blockchain.
eth_getBlockByNumber#full : Retrieves full details of a block using its block number.
eth_newBlockFilter : Available only on paid tier. Creates a filter to listen for newly added blocks.
eth_getBlockReceipts : Obtains the transaction receipts associated with a specific block.
eth_getBlockTransactionCountByHash : Returns the total number of transactions within a block identified by its hash.
eth_getBlockTransactionCountByNumber : Retrieves the count of transactions in a block based on its block number.
Chain info#
Retrieve details about the network's configuration, protocol version, and synchronization status.
eth_chainId : Retrieves the unique chain ID of the Fantom network.
eth_protocolVersion : Displays the protocol version that the client is currently using.
net_listening : Determines whether the client is actively listening for incoming network connections.
net_version : Fetches the current version of the Fantom network.
net_peerCount : Shows the number of active peers connected to the node.
eth_syncing : Indicates if the node is in the process of synchronizing with the Fantom blockchain.
eth_hashrate : Provides the current mining hashrate of the node.
Debug and trace#
Available on paid tiers, these methods allow advanced debugging, transaction tracing, and block execution analysis.
trace_filter : Filters trace information based on defined criteria.
trace_rawTransaction : Replays a raw transaction to assist in debugging.
trace_block : Monitors the execution flow of a specific block.
trace_replayBlockTransactions : Replays all transactions within a block for detailed analysis.
trace_replayBlockTransactions#vmTrace : Provides a virtual machine-level trace during the replay of block transactions.
debug_traceBlockByHash : Traces the execution of a block identified by its hash.
debug_traceBlockByNumber : Traces the execution of a block based on its numerical position in the blockchain.
trace_transaction : Traces the execution path of a specific transaction.
debug_traceTransaction : Provides in-depth trace details for a particular transaction.
trace_replayTransaction : Replays a transaction to facilitate analysis.
trace_replayTransaction#vmTrace : Offers a virtual machine-level trace during the replay of a transaction.
trace_callMany : Executes multiple trace calls simultaneously.
trace_get : Retrieves existing trace data based on provided identifiers.
trace_call : Executes a single trace call for a specified operation.
debug_traceCall : Traces a specific call without making any state changes to the blockchain.
Event logs#
Retrieve logs emitted by smart contracts for tracking token transfers, state changes, and other events.
eth_getLogs : Retrieves event logs based on specified filtering parameters.
eth_newFilter : Available only on paid tier. Initializes a new filter to observe particular events.
eth_getFilterChanges : Available only on paid tier. Fetches updates for an existing filter since the last retrieval.
eth_uninstallFilter : Deletes an active filter from the monitoring list.
eth_getFilterLogs : Available only on paid tier. Obtains all logs associated with a specified filter.
Executing transactions#
Enable transaction submission, smart contract execution, and other on-chain interactions.
eth_call : Executes a read-only function call to a smart contract without modifying the blockchain state.
eth_sendRawTransaction : Submits a raw, signed transaction to the Fantom blockchain for processing.
Gas estimation#
Estimate gas fees for transactions and retrieve historical gas price data.
eth_feeHistory : Retrieves historical gas fee data across a specified range of blocks.
eth_estimateGas : Calculates the estimated amount of gas needed to execute a transaction.
eth_gasPrice : Returns the current gas price available on the Fantom network.
eth_createAccessList : Generates an access list to help optimize and reduce gas costs.
eth_maxPriorityFeePerGas : Retrieves the maximum priority fee per gas unit that can be offered.
Getting uncles#
Retrieve details about "uncle" blocks that are valid but excluded from the main chain.
eth_getUncleByBlockHashAndIndex : Obtains details of an uncle block using the parent block's hash and the uncle's index.
eth_getUncleByBlockNumberAndIndex : Fetches information of an uncle block based on the parent block's number and the uncle's index.
eth_getUncleCountByBlockHash : Returns the total number of uncle blocks associated with a specific block hash.
eth_getUncleCountByBlockNumber : Retrieves the count of uncle blocks for a block identified by its number.
Mining#
Retrieve data about node mining status and rewards.
eth_coinbase : Returns the address designated to receive mining rewards.
eth_mining : Indicates whether the node is currently mining blocks.
Subscriptions#
Subscribe to real-time blockchain events via WebSocket connections.
eth_subscribe : Subscribes to various blockchain events such as new blocks, pending transactions, or event logs.
eth_unsubscribe : Cancels an existing event subscription.
Transactions info#
Fetch data on individual transactions, including status, receipts, and block inclusion details.
eth_getTransactionByHash : Retrieves detailed information about a transaction using its unique hash.
eth_getTransactionCount : Returns the total count of transactions initiated by a particular address.
eth_getTransactionReceipt : Fetches the receipt for a transaction that has been processed.
eth_newPendingTransactionFilter : Available only on paid tier. Sets up a filter to monitor and track transactions that are pending confirmation.
eth_getTransactionByBlockHashAndIndex : Retrieves a specific transaction using the block hash and its index within that block.
eth_getTransactionByBlockNumberAndIndex : Obtains a transaction by referencing the block number and its index in that block.
txpool_content : Displays the current state of the transaction pool, including pending and queued transactions.
Web3#
Access general network configuration data, including client version and cryptographic utilities.
web3_clientVersion : Retrieves the version information of the node client currently in operation.
web3_sha3 : Generates the Keccak-256 (SHA3) hash for the supplied data.