Berachain API Overview

Welcome to the Berachain API Quickstart and Reference Guide. This guide provides everything you need to start building on Berachain 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 Berachain seamlessly into your applications.

This page includes:

  • A Quickstart Guide to help you set up and make your first Berachain API request.
  • A detailed list of all Berachain API methods, categorized for easy reference.

Scroll to API Methods

Berachain API Quickstart#

To get started with building on Berachain using dRPC's Berachain RPC API, follow these steps:

1. Create a dRPC Account or Use Public Berachain RPC endpoints

By signing up for a free dRPC account, you can access their Premium Berachain RPC endpoints.

Public endpoints:

HTTPS

https://berachain.drpc.org/

WSS:

wss://berachain.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 Berachain-drpc-quickstart
cd Berachain-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 Berachain RPC endpoints section. Copy the HTTPS endpoint URL for the Berachain 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=berachain&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=berachain&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 Berachain block number printed in your console.

Additional Information

dRPC offers various features such as high performance nodes, access to multiple chains, and MEV protection. For more details, visit their Berachain API documentation (opens in a new tab).

By following these steps, you can start building on Berachain using dRPC's reliable and efficient RPC endpoints.

Berachain API Methods#

These endpoints enable the retrieval of information about blocks, transactions, balances, logs, and more, supporting seamless blockchain development and integration.

Accounts info#

Accesses details about account balances, contract code, and stored data on the blockchain.

eth_accounts : Enumerates all accounts managed by the client.
eth_getBalance : Retrieves the Ether balance of a specified account in Wei.
eth_getCode : Obtains the smart contract bytecode deployed at a particular address.
eth_getProof : Generates a Merkle proof for an account’s balance or its storage contents.
eth_getStorageAt : Fetches the value stored at a specific storage slot of an account.

Blocks info#

Provides comprehensive data on blocks, including transactions, receipts, and block specifics.

eth_blockNumber : Retrieves the most recent block number on the chain.
eth_getBlockByHash : Obtains the details of a block using its unique hash identifier.
eth_getBlockByHash#full : Retrieves comprehensive information of a block based on its hash.
eth_getBlockByNumber : Accesses block information using its numerical position within the blockchain.
eth_getBlockByNumber#full : Fetches full details of a block identified by its block number.
eth_newBlockFilter : Available only on paid tier. Sets up a filter to listen for newly added blocks on the network.
eth_getBlockReceipts : Retrieves 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 : Provides the count of transactions in a block based on its block number.

Chain info#

Supplies details on the network's configuration, protocol version, and overall status.

eth_chainId : Obtains the unique chain identifier of the Berachain network.
eth_protocolVersion : Displays the version of the Ethereum protocol that the client is currently utilizing.
net_listening : Checks whether the client is actively accepting incoming network connections.
net_version : Retrieves the current network ID of the Berachain network.
net_peerCount : Indicates the number of peers currently connected to the node.
eth_syncing : Shows whether the node is in the process of synchronizing with the Berachain blockchain.
eth_hashrate : Provides the node’s current mining hashrate.

Debug and trace#

Available exclusively on the premium tier, these methods allow for transaction tracing, inspecting block execution, and replaying transactions or blocks for in-depth analysis.

trace_filter : Applies a filter to retrieve trace information based on specified criteria.
trace_rawTransaction : Replays a raw transaction to assist in debugging processes.
trace_block : Observes the execution flow of a particular block.
trace_replayBlockTransactions : Re-executes all transactions within a block for detailed scrutiny.
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 : Maps out the execution pathway of a specific transaction.
debug_traceTransaction : Provides detailed trace information for a particular transaction.
trace_replayTransaction : Re-executes a transaction to support detailed analysis.
trace_replayTransaction#vmTrace : Offers a virtual machine-level trace during the replay of a transaction.
trace_callMany : Executes multiple trace operations concurrently.
trace_get : Fetches existing trace records based on specified identifiers.
trace_call : Performs a single trace operation for a designated action.
debug_traceCall : Monitors a specific call without altering the blockchain state.

Event logs#

Captures logs of events emitted by smart contracts, useful for monitoring token transfers, state changes, and other activities.

eth_getLogs : Fetches event logs that match specified filter criteria.
eth_newFilter : Available only on paid tier. Creates a new filter to monitor particular events.
eth_getFilterChanges : Available only on paid tier. Retrieves new events or updates for an existing filter since the last request.
eth_uninstallFilter : Removes a previously set filter from the active monitoring list.
eth_getFilterLogs : Available only on paid tier. Obtains all event logs associated with a specified filter.

Executing transactions#

Enables sending transactions, executing smart contracts, and performing other on-chain operations.

eth_call : Performs a read-only function invocation on a smart contract without altering the blockchain state.
eth_sendRawTransaction : Sends a raw, signed transaction to the Berachain network for inclusion in the blockchain.

Gas estimation#

Calculates transaction gas fees and provides both historical and current gas price data.

eth_feeHistory : Accesses past gas fee information over a defined block range.
eth_estimateGas : Projects the gas amount necessary to carry out a specific transaction.
eth_gasPrice : Retrieves the prevailing gas price on the Berachain network.
eth_createAccessList : Constructs an access list to enhance efficiency and minimize gas usage.
eth_maxPriorityFeePerGas : Obtains the highest priority fee per gas unit that can be offered.

Getting uncles#

Retrieves information about "uncle" blocks, which are valid but not included in the main blockchain.

eth_getUncleByBlockHashAndIndex : Retrieves the specifics of an uncle block using the parent block’s hash and the uncle’s position.
eth_getUncleByBlockNumberAndIndex : Obtains details of an uncle block based on the parent block’s number and the uncle’s index.
eth_getUncleCountByBlockHash : Provides the total count of uncle blocks linked to a particular block hash.
eth_getUncleCountByBlockNumber : Retrieves the number of uncle blocks associated with a block identified by its number.

Mining#

Provides details about the node's mining status and rewards.

eth_coinbase : Fetches the address assigned to receive mining rewards.
eth_mining : Indicates whether the node is actively engaged in mining new blocks.

Subscriptions#

Allows subscribing to real-time blockchain events using WebSockets.

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#

Fetches data on individual transactions, including their status, receipt, and inclusion in a block.

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#

Provides general network configuration data, such as client version and cryptographic functions.

web3_clientVersion : Fetches the current version of the node client in use.
web3_sha3 : Computes the Keccak-256 (SHA3) hash for the given input data.