Query from EvmApi and SolApi

It's possible to read responses from EvmApi and SolApi in three different ways: .toJson(), .raw, .result, or using .format() for default formatting type.

📘

Complete API Reference

Using the SDK is the simplest way to query blockchain data. This section demonstrates how to implement the API calls.

Please head over to API references to see the full power of the APIs and all possible methods.

Querying the APIs can be done via the EvmApi and SolApi modules as follows:

import Moralis from 'moralis'

const result1 = await Moralis.EvmApi.'<DOMAIN>'.'<METHOD>'
const result2 = await Moralis.SolApi.'<DOMAIN>'.'<METHOD>'

See the SDK reference for all the possible domains and methods.

The returned value is an ApiResultAdapter instance with the following methods:

  • .toJSON()
  • .result
  • .raw
  • .next() for paginated responses
  • .pagination for paginated responses

.toJSON()

Return values as formatted primitive data types that you can use in your dapp as output directly. These values are:

  • Always primitive JavaScript types (number, string, objects, etc.)
  • Formatted with .format() for some data types (such as EvmAddress and EvmChain) and can be configured in the config

Use this method when you want to use the data directly as output:

import { EvmChain } from '@moralisweb3/evm-utils';

const response = await Moralis.EvmApi.token.getWalletTokenBalances({
    chain: EvmChain.ETHEREUM,
    address: '0xa9C4c85DD0495E32D08EF64d51fDEB35D200EDfe',
})
console.log(response.toJSON())

//console output:
[
   {
      "value":"20.0",
      "token":{
         "decimals":18,
         "name":"ChainLink Token",
         "symbol":"LINK",
         "contractAddress":"0x01be23585060835e02b77ef475b0cc51aa1e0709",
         "chain":"0x4"
      }
   }
]

.result

Returns data with all the internal data types.

Use this method when you want to run logic on the returned data, as it comes with a lot more properties/utilities compared to the .toJSON() result:

import { EvmChain } from '@moralisweb3/evm-utils';

const response = await Moralis.EvmApi.token.getWalletTokenBalances({
    chain: EvmChain.ETHEREUM,
    address: '0xa9C4c85DD0495E32D08EF64d51fDEB35D200EDfe',
})

// An array of Erc20Value 
const balances = response.result

console.log(balances[0].token.contractAddress.equals("0x01be23585060835e02b77ef475b0cc51aa1e0709"))
//console output:
true

console.log(balances[0].value)
//console output:
"20.0"

console.log(balances[0].amount)
//console output:
BigNumber<20000000000000000000>

.raw

Returns raw data as it is; it comes directly from the API.

Use this method if you want the data as it was in v1 of the SDK. This will output the data without any formatting, and the types are different than the result from .toJSON and .result. This method is not recommended (as it lacks a lot of utilities, validations, and formatting) and should only be used when you want to migrate from the v1 version of the SDK:

import { EvmChain } from '@moralisweb3/evm-utils';

const balances = await Moralis.EvmApi.token.getWalletTokenBalances({
    chain: EvmChain.ETHEREUM,
    address: '0xa9C4c85DD0495E32D08EF64d51fDEB35D200EDfe',
})
console.log(balances.raw)

//console output:
[
   {
      "token_address":"0x01be23585060835e02b77ef475b0cc51aa1e0709",
      "name":"ChainLink Token",
      "symbol":"LINK",
      "logo":null,
      "thumbnail":null,
      "decimals":18,
      "balance":"20000000000000000000"
   }
]

Did this page help you?