How to get started building on zkSync Era and using the JSON-RPC API
To use the zkSync Era API, you'll need to create a free Alchemy account first!
Introduction
zkSync is a Layer 2 scaling solution for Ethereum, leveraging zkRollup technology. It provides low gas fees, high throughput, and enhanced user privacy while maintaining a secure and decentralized architecture.
What is zkSync Era API?
The zkSync Era API enables developers to interact seamlessly with the zkSync Era network. Through the API, developers can submit transactions, query state changes, and more, all while benefiting from the scalability and security zkSync Era offers.
Getting Started Instructions
1. Choose a Package Manager (npm or yarn)
Start by selecting a package manager for managing your project's dependencies. The most commonly used package managers in the Node.js ecosystem are npm
and yarn
. Select according to your preference.
npm
If you're using npm
, begin by installing Node.js and npm
by following the guide here: https://docs.npmjs.com/downloading-and-installing-node-js-and-npm
yarn
If you prefer yarn
, follow these installation instructions: https://classic.yarnpkg.com/lang/en/docs/install
2. Set up your project (npm or yarn)
To set up a Node.js project, open your terminal and execute the following:
mkdir alchemy-zksync-api
cd alchemy-zksync-api
npm init --yes
mkdir alchemy-zksync-api
cd alchemy-zksync-api
yarn init --yes
This creates a new directory named alchemy-zksync-api
and initializes a Node.js project within it.
3. Make Your First Request
For interacting with the zkSync Era API, you will use an HTTP client. This guide uses Axios, a well-regarded HTTP client for Node.js. Install Axios in your project as follows:
npm install axios
yarn add axios
Next, create a script for sending a request to the zks_getAllAccountBalances
JSON-RPC method on zkSync Era. This method retrieves the balances of all tokens for a specified account address. Create a file named index.js
in your project directory and input this code:
const axios = require('axios');
const apiKey = 'YOUR_API_KEY'; // Replace this with your Alchemy API key
const url = `https://zksync-mainnet.g.alchemy.com/v2/${apiKey}`;
const payload = {
jsonrpc: '2.0',
id: 1,
method: 'zks_getAllAccountBalances',
params: ['YOUR_ACCOUNT_ADDRESS'] // Replace with your account address
};
axios.post(url, payload)
.then(response => {
console.log('Account Balances:', response.data.result);
})
.catch(error => {
console.error(error);
});
Be sure to replace YOUR_API_KEY
with your actual Alchemy API key, obtainable from your Alchemy dashboard, and YOUR_ACCOUNT_ADDRESS
with the zkSync Era account address you want to query.
4. Run Script
To execute the script and send the request to the zkSync Era API, use this command:
node index.js
You should see the balances of all tokens owned by the specified zkSync Era account displayed in your console:
{
"jsonrpc": "2.0",
"result": {
"0x0000000000000000000000000000000000000000": "0x2fbd72a1121b3100"
},
"id": 2
}
Next Steps
Well done! You've now made your initial request to the zkSync Era API through Alchemy. Proceed to explore the various JSON-RPC methods available on zkSync Era to develop your decentralized applications on this powerful Layer 2 platform!