How to get started building on Vana and using the JSON-RPC API
What is the Vana API?
The Vana API allows applications to connect to an Vana node that is part of the Vana blockchain. Developers can interact with on-chain data and send different types of transactions to the network by utilizing the endpoints provided by the API. The API follows a JSON-RPC standard.
JSON-RPC is a stateless, lightweight, remote procedure call (RPC) protocol that is commonly used when interacting with Vana.
Getting Started Instructions
For this guide, we will be using npm or yarn as our package manager to install necessary packages.
npm
To get started with npm, follow the documentation to install Node.js and npm for your operating system: https://docs.npmjs.com/downloading-and-installing-node-js-and-npm
yarn
To get started with yarn, follow these steps: https://classic.yarnpkg.com/lang/en/docs/install
To kickstart your project, open your terminal and execute the following commands:
mkdir ethereum-api-quickstart
cd ethereum-api-quickstart
npm init --yes
mkdir vana-api-quickstart
cd vana-api-quickstart
yarn init --yes
For making API requests, we'll use Axios, a widely-used HTTP client. Install Axios with the following command:
npm install axios
yarn add axios
You are all set now to use Vana API and make your first request. For instance, let's make a request to get the latest block. Create an index.js file in your project directory and paste the following code snippet into the file.
import axios from "axios";
const url = `https://rpc.{network}.vana.org/`;
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);
});
You can later make requests on our 'mainnet' by just replacing the request URL:
To execute your script and make a request to the Vana network, run:
node index.js
You should see the latest block number outputted to your console.
The latest block number is 13043966