DEV Community

OnFinality
OnFinality

Posted on Originally published at onfinality.io

Sonic RPC Node: Connect to Sonic Mainnet Endpoints

Quick Start: Sonic RPC Endpoint

If you just need a Sonic RPC endpoint to start building, here's the public endpoint provided by OnFinality:

https://sonic.api.onfinality.io/public
Enter fullscreen mode Exit fullscreen mode

You can use this endpoint to send JSON-RPC requests, but for production applications you'll want a more reliable and scalable solution. OnFinality offers dedicated Sonic nodes and managed RPC services with higher rate limits, archive data, and WebSocket support.

Chain Settings at a Glance

Before connecting to Sonic, you need the correct network parameters. Here's a summary:

Parameter Value
Network Name Sonic Mainnet
Chain ID 146
Native Currency S (Sonic)
Block Explorer https://sonicscan.org
Public RPC URL https://sonic.api.onfinality.io/public

These settings are essential for configuring wallets, dApps, and development tools.

How to Add Sonic to Your Wallet

To interact with Sonic-based dApps, you'll need to add the network to your wallet (e.g., MetaMask). Here's a step-by-step guide:

  1. Open your wallet and navigate to the network settings.
  2. Click "Add Network" or "Custom RPC".
  3. Enter the following details:
    • Network Name: Sonic Mainnet
    • New RPC URL: https://sonic.api.onfinality.io/public
    • Chain ID: 146
    • Currency Symbol: S
    • Block Explorer URL: https://sonicscan.org
  4. Save the network and switch to it.

Once added, you can view your Sonic balance and interact with Sonic dApps.

Making Your First JSON-RPC Call

To verify your connection, you can make a simple JSON-RPC call using curl. For example, to get the current block number:

curl https://sonic.api.onfinality.io/public \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}'
Enter fullscreen mode Exit fullscreen mode

You should receive a response like:

{"jsonrpc":"2.0","result":"0x...","id":1}
Enter fullscreen mode Exit fullscreen mode

The result is the hexadecimal block number. This confirms your RPC endpoint is working.

Using Sonic RPC with ethers.js or viem

For JavaScript developers, you can integrate Sonic RPC using popular libraries like ethers.js or viem. Here's an example using ethers.js:

import { ethers } from "ethers";

const provider = new ethers.JsonRpcProvider("https://sonic.api.onfinality.io/public");

async function getBlockNumber() {
  const blockNumber = await provider.getBlockNumber();
  console.log("Current block number:", blockNumber);
}

getBlockNumber();
Enter fullscreen mode Exit fullscreen mode

With viem:

import { createPublicClient, http } from "viem";

const client = createPublicClient({
  chain: {
    id: 146,
    name: "Sonic Mainnet",
    network: "sonic",
    nativeCurrency: { name: "Sonic", symbol: "S", decimals: 18 },
    rpcUrls: { default: { http: ["https://sonic.api.onfinality.io/public"] } },
  },
  transport: http(),
});

const blockNumber = await client.getBlockNumber();
console.log("Current block number:", blockNumber);
Enter fullscreen mode Exit fullscreen mode

WebSocket Subscriptions for Real-Time Data

If your dApp needs real-time updates (e.g., new blocks, pending transactions), you'll need a WebSocket endpoint. OnFinality provides WebSocket support for Sonic RPC nodes. The WebSocket URL typically follows the pattern:

wss://sonic.api.onfinality.io/public/ws
Enter fullscreen mode Exit fullscreen mode

Note: The exact WebSocket URL may vary. Check the Sonic network page for the latest supported transports.

Here's an example of subscribing to new block headers using ethers.js:

import { ethers } from "ethers";

const provider = new ethers.WebSocketProvider("wss://sonic.api.onfinality.io/public/ws");

provider.on("block", (blockNumber) => {
  console.log("New block:", blockNumber);
});
Enter fullscreen mode Exit fullscreen mode

Choosing the Right Sonic RPC Provider

When selecting an RPC provider for Sonic, consider the following factors:

Factor What to Check Why It Matters
Rate Limits Requests per second, daily quota Public endpoints often have strict limits that can throttle your dApp
Archive Data Support for eth_getLogs and historical state Needed for analytics, indexing, and debugging
WebSocket Support Availability of wss endpoints Essential for real-time features
Dedicated Nodes Option for a private, isolated node Provides consistent performance and no noisy neighbors
Uptime & Reliability Historical uptime, redundancy Downtime can break your application

OnFinality offers both shared public RPC and dedicated Sonic nodes. Dedicated nodes give you full control over your infrastructure, with clear rate limits and access to archive data.

Public vs. Dedicated Sonic RPC Nodes

For development and testing, a public RPC endpoint is often sufficient. However, for production workloads, you should consider a dedicated node. Here's a comparison:

Aspect Public RPC Dedicated Node
Cost Free or low cost Subscription-based
Rate Limits Yes, often strict No (or very high)
Performance Shared, variable Consistent, isolated
Archive Data Usually not available Available on request
WebSocket May be limited Full support
Control None Full access to node config

If your dApp handles a high volume of requests or requires historical data, a dedicated node is the better choice.

Troubleshooting Common Sonic RPC Issues

Even with a reliable provider, you may encounter issues. Here are common problems and how to fix them:

1. Connection Timeouts

If you're experiencing timeouts, check:

  • Your network connection
  • The RPC endpoint URL (ensure it's correct)
  • Whether you're using the right transport (HTTP vs WebSocket)

2. Rate Limit Errors

Public endpoints often return 429 Too Many Requests. To avoid this:

  • Implement caching on your client
  • Use a dedicated node for high-traffic applications
  • Consider using multiple endpoints with failover

3. Incorrect Chain ID

Ensure your wallet or dApp is configured with the correct chain ID (146). Using the wrong chain ID can cause transactions to fail.

4. Missing Archive Data

If you need historical data, ensure your provider supports archive requests. Public endpoints often only serve recent state.

Monitoring Your Sonic RPC Node

Once your RPC connection is live, you should monitor its health. You can set up a simple health check using a script:

#!/bin/bash

# Check if the RPC endpoint is responding
response=$(curl -s -o /dev/null -w "%{http_code}" -X POST \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' \
  https://sonic.api.onfinality.io/public)

if [ "$response" -eq 200 ]; then
  echo "RPC is healthy"
else
  echo "RPC is down"
fi
Enter fullscreen mode Exit fullscreen mode

You can run this script periodically to ensure your endpoint is operational.

Key Takeaways

  • Sonic is an EVM-compatible Layer 1 blockchain with chain ID 146.
  • The public RPC endpoint is https://sonic.api.onfinality.io/public.
  • For production, consider a dedicated Sonic node to avoid rate limits and get archive data.
  • Always verify your chain settings and use the correct transport (HTTP/WebSocket).
  • Monitor your RPC endpoint to ensure high availability.

Frequently Asked Questions

Q: What is the Sonic RPC URL?
A: The public RPC URL is https://sonic.api.onfinality.io/public. For production, you may want a dedicated endpoint.

Q: What is the Sonic chain ID?
A: The chain ID is 146.

Q: Does OnFinality support Sonic WebSocket?
A: Yes, OnFinality provides WebSocket support for Sonic RPC nodes. Check the Sonic network page for details.

Q: Can I get archive data for Sonic?
A: Yes, dedicated Sonic nodes on OnFinality can be configured with archive data. Contact us for more information.

Q: How do I choose between public and dedicated RPC?
A: Public RPC is fine for development, but for production apps with high traffic, dedicated nodes offer better performance and reliability.

For more information about RPC providers and network support, visit our RPC pricing page and supported networks list.

Related resources

Originally published at OnFinality.

Top comments (0)