Get started building on Sonic and using the JSON-RPC API
To use the Sonic Chain API you'll need to create a free Alchemy account first!
Introduction
Sonic Chain is an EVM-compatible, high-performance, scalable, and secure layer-1 blockchain. With over 10,000 TPS, one-second confirmation times, and low-cost transactions, it provides a robust platform for decentralized applications (dApps) and Ethereum-based deployments.
What is the Sonic API?
The Sonic API allows interaction with the Sonic network through a set of JSON-RPC methods. Its design is familiar to developers who have worked with Ethereum's JSON-RPC APIs, making it intuitive and straightforward to use.
Getting Started Instructions
1. Choose a Package Manager (npm or yarn)
Select a package manager to manage your project's dependencies. Choose between npm
and yarn
based on your preference or project requirements.
npm | yarn |
---|---|
Begin with npm by following the npm documentation. | For yarn , refer to yarn's installation guide. |
2. Set Up Your Project
Open your terminal and execute the following commands to create and initialize your project:
mkdir sonic-api-quickstart
cd sonic-api-quickstart
npm init --yes
mkdir sonic-api-quickstart
cd sonic-api-quickstart
yarn init --yes
This creates a new directory named sonic-api-quickstart
and initializes a Node.js project within it.
3. Make Your First Request
Install Axios, a popular HTTP client, to make API requests:
npm install axios
# Or with yarn
# yarn add axios
Create an index.js
file in your project directory and paste the following code:
const axios = require('axios');
const url = 'https://sonic-mainnet.g.alchemy.com/v2/${your-api-key}';
const payload = {
jsonrpc: '2.0',
id: 1,
method: 'eth_blockNumber',
params: []
};
axios.post(url, payload)
.then(response => {
console.log('Latest Block:', response.data.result);
})
.catch(error => {
console.error(error);
});
Remember to replace your-api-key
with your actual Alchemy API key that you can get from your Alchemy dashboard.
4. Run Your Script
Execute your script to make a request to the Sonic mainnet:
node index.js
You should see the latest block information from Sonic's mainnet outputted to your console:
Latest Block: 0x...
Next Steps
Congratulations! You've made your first request to the Sonic network. You can now explore the various JSON-RPC methods available on Sonic and start building your dApps on this innovative platform.