SOLANA SERIES

How to get Native balance of Solana wallet

A guide to fetch the native balance of Solana wallet using Moralis Solana API

Chikku George
Coinmonks
Published in
3 min readJan 2, 2023

--

In this tutorial, we’ll go over utilizing the Moralis Solana API and NodeJS to retrieve the native balance of a solana wallet.

New to trading? Try crypto trading bots or copy trading on best crypto exchanges

To set up the application, follow the instructions below.

  1. Create a package.json file as below
npm init -y

2. Install moralis and @moralisweb3/common-sol-utilsdependency as follows

npm i moralis @moralisweb3/common-sol-utils

3. Your package.json should contain the following

{
"name": "moralis-solana-balance",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"start": "node server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@moralisweb3/common-sol-utils": "^2.7.4",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"moralis": "^2.7.4"
}
}

4. Create a .env file which contains the Moralis API_KEY

API_KEY = “Your Moralis API Key”

5. Start the server in server.js

require('dotenv').config();
const express = require('express');

const app = express();

app.use(express.json());

app.listen(8080, async () => {
console.log('server started');
});

6. Start Moralis in server.js

require('dotenv').config();
const express = require('express');
const Moralis = require('moralis').default;

const app = express();
const { API_KEY } = process.env;

app.use(express.json())

async function startMoralis() {
try{
await Moralis.start({
apiKey: API_KEY,
});
}catch(err) {
console.log(err);
}
}

app.listen(8080, async () => {
startMoralis();
console.log('server started');
})

7. Add a GET endpoint /sol-balance to get the native solana balance

require('dotenv').config();
const express = require('express');
const Moralis = require('moralis').default;
const { SolNetwork } = require('@moralisweb3/common-sol-utils');

const app = express();
const { API_KEY } = process.env;

app.use(express.json())

async function startMoralis() {
try{
await Moralis.start({
apiKey: API_KEY,
});
}catch(err) {
console.log(err);
}
}

app.get('/sol-balance', async (req, res) => {
const network = SolNetwork.MAINNET;
const { address } = req.body;
try{
const response = await Moralis.SolApi.account.getBalance({
address,
network,
});
res.status(200).send(response.toJSON());
}catch(err) {
console.log(err);
}
})

app.listen(8080, async () => {
startMoralis();
console.log('server started');
})

8. Test the endpoint using Postman

The test result is given below. Give the wallet address in req.body

Postman Result for Native Solana Balance API

Summary

Up to this point, we have talked about utilizing the Moralis Solana API and NodeJs to obtain the native balance of a solana wallet. Open a Moralis account and play around with their APIs.

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Also, Read

--

--

Chikku George
Coinmonks

Software Engineer | ReactJs | NodeJs | Blockchain Enthusiast