APIs help you create high-quality and maintainable decentralized applications (dApps) that interact with the blockchain to get information about blocks, transactions, metadata, token pricing, etc.
In this article, we’ll talk about different web3 platforms that provide efficient APIs that power communication between your dApps and the blockchain.
DApps are blockchain-based applications that allow users to interact with smart contracts deployed on the blockchain.
Every dApp requires a remote procedure call (RPC) node to perform its functionalities, as it facilitates communication between your dApps and the blockchain.
This implies that without an RPC, your dApp will not be able to perform transactions on the blockchain.
Node: Computer or server
RPC is a traditional way of establishing a communication channel between two or more systems in different locations.
An RPC node is also called the brain-box of crypto, since it manages the interactions between systems. In a distributed system (like your dApp), the execution of subroutines in separate places is frequent.
RPCs can be compared to Rest APIs, which you can host yourself, but they only entertain a GET and POST communication method between two different systems (your dApp and the blockchain). On the other hand, Rest APIs support the GET, POST, PATCH, PUT, and DELETE methods.
Short answer: No! Don’t do it.
Long answer: RPC nodes can be very difficult to develop and maintain by yourself, especially if you’re building one for your dApp with little to no knowledge of when and how to use them correctly.
Below are some of the limitations of developing and self-hosting an RPC node:
RPC limitations can be exhausting. You probably don’t even need to develop or host an RPC node on your own because now, there are many web3 platforms that provide solutions to these limitations.
The RPC service providers offer a web3 backend service and some of the best web3 and NFT APIs to power your dApp with a small fee..
Web3 APIs are blockchain APIs made available by web3 platforms like Moralis, Alchemy and QuickNode. These platforms address the limitations of RCP nodes, and allow anyone to query data across multiple blockchain projects in a simple way that saves time and resources.
With a single line of code, web3 APIs can be used to authenticate a user and retrieve information about blocks, transactions, NFT metadata, token prices, etc.
Before creating an RPC from scratch, you should look at these web3 platforms to see whether they provide an API that caters to the requirements of your dApp.
The following are a few examples of dApps that can be created using the web3 and NFT APIs:
Some of the web3 APIs and NFT APIs provided by Moralis, Alchemy, and QuickNode are listed below:
Moralis is a web3 platform that provides a backend service for blockchain projects. They offer the highest numbers of web3 and NFT APIs for authentication, blockchain account information, etc.
Moralis allows you to authenticate users on any blockchain with just one line of code:COPYCOPY
Moralis.authenticate()
COPYCOPY
const transactions = await Moralis.Web3API.account.getTransactions();
COPYCOPY
const balances = await Moralis.Web3API.account.getTokenBalances();
COPYCOPY
const userTrans = await Moralis.Web3API.account.getTokenTransfers();
COPYCOPY
// sending 0.5 ETH
const options = {
type: "native",
amount: Moralis.Units.ETH("0.5"),
receiver: "0x.."
};
let result = await Moralis.transfer(options);
Moralis provides an endpoint to store and share files on the blockchain. This system is known as IPFSInterPlanetary File System (IPFS).
COPYCOPY
const options = {
abi: [
{
path: "moralis/logo.jpg",
content:
"iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAApgAAAKYB3X3",
},
],
};
const path = await Moralis.Web3API.storage.uploadFolder(options);
Code snippets examples are from the Moralis Web3 API documentation.
COPYCOPY
const userEthNFTs = await Moralis.Web3API.account.getNFTs();
COPYCOPY
const transfersNFT = await Moralis.Web3API.account.getNFTTransfers();
COPYCOPY
const options = { address: "0xd...07", days: "3" };
const NFTLowestPrice = await Moralis.Web3API.token.getNFTLowestPrice(options);
COPYCOPY
const options = { q: "Pancake", chain: "bsc", filter: "name" };
const NFTs = await Moralis.Web3API.token.searchNFTs(options);
COPYCOPY
const options = { address: "0xd...07", chain: "bsc" };
const nftOwners = await Moralis.Web3API.token.getNFTOwners(options);
COPYCOPY
// sending a token with token id = 1
const options = {
type: "erc721",
receiver: "0x..",
contractAddress: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
tokenId: 1,
};
let transaction = await Moralis.transfer(options);
COPYCOPY
// get devnet metadata for a given SPL NFT address
const options = {
network: "devnet",
address: "6XU36wCxWobLx5Rtsb58kmgAJKVYmMVqy4SHXxENAyAe",
};
const nftMetadata = await Moralis.SolanaAPI.nft.getNFTMetadata(options);
Code snippets examples are from the Moralis NFT documentation.
Alchemy Web3 API is a drop-in replacement for web3.js, which was created to operate easily with Alchemy. It also provides powerful APIs to power dApps with features that aren’t available in ordinary nodes.
COPYCOPY
const nfts = await web3.alchemy.getNfts({owner: "0xC33881b8FD07d71098b440fA8A3797886D831061"})
COPYCOPY
const response = await web3.alchemy.getNftMetadata({
contractAddress: "0x5180db8F5c931aaE63c74266b211F580155ecac8",
tokenId: "1590"
})
COPYCOPY
import fetch from 'node-fetch';
var requestOptions = {
method: 'GET',
redirect: 'follow'
};
const apiKey = "demo"
const baseURL = `https://eth-mainnet.alchemyapi.io/v2/${apiKey}/getOwnersForToken`;
const contractAddr = "0x04b14e3383d42685ae16af3c47b21b2d5941d27e";
const tokenId = "867";
const fetchURL = `${baseURL}?contractAddress=${contractAddr}&tokenId=${tokenId}`;
fetch(fetchURL, requestOptions)
.then(response => response.json())
.then(response => JSON.stringify(response, null, 2))
.then(result => console.log(result))
.catch(error => console.log('error', error));
Code snippets examples are from Alchemy’s documentation.
QuickNode is a web3 platform that allows users to build and grow dApps through their RPC endpoints for over 10 networks, including Ethereum and Solana.
COPYCOPY
const heads = await provider.send(
"qn_verifyNFTsOwner",
[
"0x8ae6422631292c31aeeb2efe154d6326f703f46b",
[
"0x60e4d786628fea6478f785a6d7e704777c86a7c6:1090",
]
]
COPYCOPY
const heads = await provider.send("qn_fetchNFTs", [
"0x63a63d7b0a4da84e095bac389845615a09e05546",
[
"0xba30e5f9bb24caa003e9f2f0497ad287fdf95623",
],
]);
COPYCOPY
// not currently supported by solanaJS
const axios = require("axios");
(() => {
const url = "https://quick-infra-structure.solana-mainnet.quiknode.pro/4456aaba19e98f28c900e9d5f997d6f39728f551/";
const config = {
headers: {
"Content-Type": "application/json",
},
};
const data = {
jsonrpc: "2.0",
id: 1,
method: "qn_fetchNFTsByCreator",
params: [{
creator: "5GUrnehPCrVeAeo29sgH3KbPhTvEDaH8HJqonYUceVM",
page: 1,
perPage: 3
}]
};
axios
.post(url, data, config)
.then(function (response) {
// handle success
console.log(response.data);
})
.catch((err) => {
// handle error
console.log(err);
});
})();
COPYCOPY
const ethers = require("ethers");
(async () => {
const provider = new ethers.providers.JsonRpcProvider("http://sample-endpoint-name.network.quiknode.pro/token-goes-here/");
const gasPrice = await provider.getGasPrice();
console.log(gasPrice);
})();
COPYCOPY
const ethers = require("ethers");
(async () => {
const provider = new ethers.providers.JsonRpcProvider("http://sample-endpoint-name.network.quiknode.pro/token-goes-here/");
const balance = await provider.getBalance(
"0x8D97689C9818892B700e27F316cc3E41e17fBeb9",
"latest"
);
console.log(balance);
})();
Code snippets examples are from QuickNode documentation.
You’ve probably realized why building an RPC from the ground up might not be the best solution for your dApp. Instead, it might be easier to leverage web3 and NFT APIs from web3 API providers like Moralis, Alchemy, and Quicknode.
Using web3 and NFT APIs speeds up and ensures the reliability of your development. You should also explore each web3 and NFT API provider to see which one provides the best solutions for your dApp.
Now that you’ve learned about web3 and NFT APIs to power your dApps:
Source: Hashnode Web3