Sonic API

Sonic API Overview

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

This page includes:

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

Scroll to API Methods

Sonic API Quickstart#

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

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

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

Public endpoints:

HTTPS

https://sonic.drpc.org/

WSS:

wss://sonic.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 Sonic-drpc-quickstart
cd Sonic-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 Sonic RPC endpoints section. Copy the HTTPS endpoint URL for the Sonic 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=sonic&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=sonic&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 Sonic 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 Sonic API documentation (opens in a new tab).

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

Sonic API Overview#

These endpoints provide access to blockchain data, enabling efficient development and integration by retrieving details on blocks, transactions, accounts, logs, and more.

Accounts Info

Provides information about an account’s balance, contract code, and stored data on the Sonic network.

eth_accounts: Displays a list of accounts managed by the client.
eth_getBalance: Fetches an account’s balance in Wei.
eth_getCode: Retrieves the smart contract code deployed at a given address.
eth_getProof: Generates a Merkle proof for an account or its storage slots.
eth_getStorageAt: Obtains the value stored at a specific storage slot.

Blocks Info

Fetches comprehensive information from a specific Sonic block, including transactions, receipts, and metadata. These methods facilitate detailed analysis of block contents, transaction volume, and execution outcomes.

eth_blockNumber: Retrieves the latest block number.
eth_getBlockByHash: Retrieves block information using its hash.
eth_getBlockByHash#full: Provides full block details, including all transactions.
eth_getBlockByNumber: Gets block information by its number.
eth_getBlockByNumber#full: Returns a complete block with all transaction data.
eth_newBlockFilter: Sets up a filter to track new blocks in real time.
eth_getBlockReceipts: Retrieves transaction receipts for a specific block.
eth_getBlockTransactionCountByHash: Returns the number of transactions in a block using its hash.
eth_getBlockTransactionCountByNumber: Fetches the transaction count for a block based on its number.

Chain Info

Offers information about the Sonic network, including its protocol version and configuration details.

eth_chainId: Retrieves the network’s unique chain ID.
eth_protocolVersion: Returns the current protocol version.
net_listening: Shows whether the client is actively listening for incoming connections.
net_version: Provides the network version.
net_peerCount: Displays the number of peers connected to the network.
eth_syncing: Verifies if the node is currently syncing with the network.
eth_hashrate: Reports the mining hash rate of the node.

Debug & Trace (Paid Tier)

Enables advanced debugging, transaction tracing, and block execution inspection.

Available exclusively on the paid tier. Enables transaction tracing, block execution inspection, and replaying transactions or blocks for in-depth analysis.

trace_filter: Filters trace data based on defined criteria.
trace_rawTransaction: Replays a raw transaction for debugging.
trace_block: Traces the execution flow of a block.
trace_replayBlockTransactions: Replays all transactions in a block for detailed analysis.
trace_replayBlockTransactions#vmTrace: Provides a virtual machine (VM)-level execution trace during block replay.
debug_traceBlockByHash: Traces the execution of a block using its hash.
debug_traceBlockByNumber: Traces block execution based on its number.
trace_transaction: Traces the execution of a specific transaction.
debug_traceTransaction: Generates a detailed execution trace for a transaction.
trace_replayTransaction: Replays a transaction for behavioral analysis.
trace_replayTransaction#vmTrace: Provides a VM-level trace during transaction replay.
trace_callMany: Executes multiple trace calls concurrently.
trace_get: Retrieves execution trace data.
trace_call: Executes a trace call without altering state.
debug_traceCall: Traces a contract call for debugging purposes.

Event Logs

Captures logs of events emitted by smart contracts, which are crucial for monitoring token transfers and state changes.

eth_getLogs: Retrieves event logs based on specified filter criteria.
eth_newFilter: Sets up a new filter to monitor targeted events.
eth_getFilterChanges: Retrieves new event logs since the last filter check.
eth_uninstallFilter: Deletes an existing event filter.
eth_getFilterLogs: Fetches all historical logs matching a specified filter.

Executing transactions#

Enables interaction with smart contracts, token transfers, and other blockchain operations by executing transactions.

eth_call: Executes contract calls without broadcasting a transaction.
eth_sendRawTransaction: Submits signed transactions to the network.

Gas estimation#

Calculates gas fees for transactions and offers both historical and real-time gas price data.

eth_feeHistory: Provides historical gas fee data for trend analysis.
eth_estimateGas: Estimates the gas required for a specific transaction.
eth_gasPrice: Retrieves the current gas price from the network.
eth_createAccessList: Generates an access list to improve gas efficiency.
eth_maxPriorityFeePerGas: Suggests a priority fee for faster transaction processing.

Getting uncles#

Retrieves information about "uncle" blocks—valid blocks that were mined but not included in the main blockchain.

eth_getUncleByBlockHashAndIndex: Retrieves an uncle block using its hash and index.
eth_getUncleByBlockNumberAndIndex: Fetches an uncle block based on its number and index.
eth_getUncleCountByBlockHash: Returns the total number of uncle blocks for a specific block hash.
eth_getUncleCountByBlockNumber: Provides the count of uncle blocks for a given block number.

Mining#

Retrieves details about the mining state of a Sonic node.

eth_coinbase: Returns the address receiving mining rewards.
eth_mining: Indicates whether the node is actively mining.

Subscriptions#

Allows real-time monitoring of specific Sonic events through WebSockets.

eth_subscribe: Subscribes to blockchain events like new blocks or logs.
eth_unsubscribe: Terminates an active subscription.

Transactions info#

Retrieves detailed information from a specified Sonic block, including transactions, receipts, and block metadata. These methods enable in-depth analysis of block details, transaction volume, and execution results.

eth_blockNumber: Returns the latest block number.
eth_getBlockByHash: Fetches block details using its hash.
eth_getBlockByHash#full: Retrieves a complete block, including all transactions.
eth_getBlockByNumber: Obtains block details by number.
eth_getBlockByNumber#full: Fetches a full block with transaction data.
eth_newBlockFilter: Creates a filter to monitor new blocks.
eth_getBlockReceipts: Retrieves transaction receipts for a block.
eth_getBlockTransactionCountByHash: Returns the number of transactions in a block by hash.
eth_getBlockTransactionCountByNumber: Gets the transaction count in a block by number.

Web3#

Fetches general network configuration information, including client version and cryptographic functions.

web3_clientVersion: Retrieves the version of the client software.
web3_sha3: Generates a Keccak-256 hash from the provided data.