Friday, May 23, 2025
No Result
View All Result
Coins League
  • Home
  • Bitcoin
  • Crypto Updates
    • Crypto Updates
    • Altcoin
    • Ethereum
    • Crypto Exchanges
  • Blockchain
  • NFT
  • DeFi
  • Metaverse
  • Web3
  • Scam Alert
  • Regulations
  • Analysis
Marketcap
  • Home
  • Bitcoin
  • Crypto Updates
    • Crypto Updates
    • Altcoin
    • Ethereum
    • Crypto Exchanges
  • Blockchain
  • NFT
  • DeFi
  • Metaverse
  • Web3
  • Scam Alert
  • Regulations
  • Analysis
No Result
View All Result
Coins League
No Result
View All Result

Complete Blockchain API Tutorial with API Examples

November 1, 2023
in Web3
Reading Time: 14 mins read
0 0
A A
0
Home Web3
Share on FacebookShare on TwitterShare on E Mail


As we speak’s blockchain API tutorial will spotlight quite a few API examples and reveal easy methods to fetch important blockchain information. As we transfer ahead, we’ll dive into Moralis’ industry-leading Blockchain API – the final word instrument for blockchain information. It options quite a few endpoints, permitting you to effortlessly get blocks, transactions, logs, occasions, and far more. To present you slightly sneak peek, right here’s a blockchain API instance of how one can get the contents of a block with a single name to Moralis’ Blockchain API:

const response = await Moralis.EvmApi.block.getBlock({
“chain”: “0x1”,
“blockNumberOrHash”: “15863321”
});

All you must do is configure the chain and blockNumberOrHash parameters to suit your question earlier than calling the endpoint within the instance above. In return, you’ll get a response containing the block’s timestamp, quantity, hash, log bloom, miner, and far more. That is what it may appear like: 

{
“timestamp”: “2021-05-07T11:08:35.000Z”,
“quantity”: 12386788,
“hash”: “0x9b559aef7ea858608c2e554246fe4a24287e7aeeb976848df2b9a2531f4b9171”,
“parent_hash”: “0x011d1fc45839de975cc55d758943f9f1d204f80a90eb631f3bf064b80d53e045”,
“nonce”: “0xedeb2d8fd2b2bdec”,
“sha3_uncles”: “0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347”,
“logs_bloom”: “0xdde5fc46c5d8bcbd58207//…”,
“transactions_root”: “0xe4c7bf3aff7ad07f9e80d57f7189f0252592fee6321c2a9bd9b09b6ce0690d27”,
“state_root”: “0x49e3bfe7b618e27fde8fa08884803a8458b502c6534af69873a3cc926a7c724b”,
“receipts_root”: “0x7cf43d7e837284f036cf92c56973f5e27bdd253ca46168fa195a6b07fa719f23”,
“miner”: “0xea674fdde714fd979de3edf0f56aa9716b898ec8”,
“problem”: “7253857437305950”,
“total_difficulty”: “24325637817906576196890”,
“dimension”: “61271”,
“extra_data”: “0x65746865726d696e652d6575726f70652d7765737433”,
“gas_limit”: “14977947”,
“gas_used”: “14964688”,
“transaction_count”: “252”,
“transactions”: {
//…
}
}

And that’s it; when working with Moralis, getting blockchain information doesn’t must be harder. Nevertheless, in order for you a extra detailed tutorial and discover some extra endpoints, learn on! Additionally, if you wish to comply with alongside and make comparable calls your self, don’t neglect to enroll with Moralis. You’ll be able to create an account free of charge and instantly unlock the ability of blockchain information! 

Overview 

In at the moment’s tutorial, we’ll kickstart issues by diving straight into the Moralis Blockchain API. In doing so, we’ll briefly discover what this instrument does and canopy a few of its key advantages. From there, we’ll discover some outstanding use instances and provide you with a sensible instance of how one can leverage the Blockchain API in every case. Lastly, to prime issues off, we’ll soar into an in-depth tutorial exhibiting you easy methods to get the contents of a block by its quantity in three easy steps: 

Get a Moralis API KeyWrite a Script Calling the getBlock() EndpointExecute the Code

Additionally, in the event you’re severe about constructing blockchain tasks, take into account trying out some extra Web3 information instruments Moralis presents. As an example, discover the Worth API to learn to seamlessly get costs of crypto and combine this information into your tasks! 

Nonetheless, with out additional ado, let’s discover Moralis’ industry-leading Blockchain API!

What’s the Moralis Blockchain API? 

The Moralis Blockchain API is an industry-leading interface that permits you to seamlessly unlock the ability of uncooked blockchain information with velocity and precision. With solely single traces of code, you’ll be able to effortlessly get blocks, transactions, logs, occasions, and far more with the Blockchain API. As such, when working with the Moralis, it has by no means been simpler to construct Web3 tasks! 

Art image showing a blockchain API example

So, what are the options of the Blockchain API? 

Get Block Knowledge: Get data on explicit blocks – together with timestamps, gasoline used, miners, transactions, and so on. – throughout a number of blockchain networks. Fetch Transactions and Inner Transactions: Seamlessly entry all historic and real-time transactions of an deal with. Question Logs and Occasions: Use the Blockchain API to question logs and occasions of any good contract throughout all main EVM chains. Uncooked and Decoded Knowledge: Get each uncooked and decoded information based mostly on contract ABIs.Labels: Efforlessly interpret transactions with enriched information labeling, together with receiver and sender deal with labels. And extra! 

Moreover, the Blockchain API is cross-chain suitable. This implies you’ll be able to question blockchain information throughout all main networks, together with Ethereum, Polygon, BNB Good Chain, Arbitrum, and plenty of others! 

Advantages of the Moralis Blockchain API 

Together with many highly effective options, the Blockchain API additionally gives many advantages. Listed below are 4 outstanding examples: 

Scalable: Our Blockchain API is constructed for development. Consequently, as your tasks see elevated adoption, the interface may have no bother delivering at the same time as your consumer base grows.Actual-Time: The Blockchain API is quick, indexes real-time information, and ensures quick entry to the most recent blockchain data. Exact: Knowledge accuracy is important when constructing Web3 tasks, which is why the Blockchain API at all times presents exact and dependable information. Organized: Blockchain information could be messy, and we consider in digestible and detailed info. As such, our Blockchain API organizes blocks into transfers, transactions, and logs, supplying you with a structured option to navigate the crypto panorama. 

Now, with an outline of our industry-leading Blockchain API, let’s discover a few of its use instances! 

Blockchain API Examples and Use Instances

The Moralis Blockchain API has many outstanding use instances, and on this part, we’ll have a look at three examples:

Block Explorers: Block explorers are on-line instruments enabling you to seek for real-time and historic information a few blockchain. This often contains block information, addresses, transactions, and far more. Some outstanding examples of already current block explorers are Etherscan, PolygonScan, and BscScan. 

Since explorers help you seek for details about a blockchain, you want seamless entry to this information (e.g., transactions, blocks, logs, and so on.) when constructing one. And the best option to question this data is to make use of the Moralis Blockchain API. 

Showing components involved in an API call for a block explorer

Web3 Wallets: Web3 wallets are software program permitting you to retailer and handle digital property, together with fungible and non-fungible tokens. Some examples of outstanding pockets suppliers embody MetaMask and Rainbow. 

A typical function of most Web3 wallets is to show every consumer’s pockets exercise. This offers customers an outline of all their trades, which could be helpful in lots of instances. To show this info, you want entry to transaction data and occasions, which is the place our Blockchain API enters the equation. 

Web3 Wallet Blockchain API Call Example with Response Data

Decentralized Software (Dapp) Alerts: When constructing dapps, it may be extremely helpful to arrange alert techniques to inform customers of vital occasions. This may, as an illustration, be alerts for when crypto whales promote a big portion of their holdings. 

To arrange alert techniques like this, you want entry to good contract occasions and logs, which you’ll effortlessly get with solely single traces of code when working with our Blockchain API.

Dapp Alert Blockchain API Example

Nonetheless, that covers three outstanding use instances. Within the following sections, we’ll develop on this additional by offering examples of easy methods to use the Blockchain API to get the data you want with a number of programming languages! 

Block Explorers: JavaScript Blockchain API Instance 

As talked about above, you want entry to lots of information when constructing a block explorer. With the Moralis Blockchain API, you will get the data you want in a heartbeat!  

All you must do is name the getBlock() endpoint whereas passing alongside two parameters: chain and blockNumberOrHash. Right here’s an instance of what it’d appear like:

const response = await Moralis.EvmApi.block.getBlock({
“chain”: “0x1”,
“blockNumberOrHash”: “15863321”
});

In return for calling the endpoint above, you’ll get a complete response wanting one thing like this: 

{
“timestamp”: “2021-05-07T11:08:35.000Z”,
“quantity”: 12386788,
“hash”: “0x9b559aef7ea858608c2e554246fe4a24287e7aeeb976848df2b9a2531f4b9171”,
“parent_hash”: “0x011d1fc45839de975cc55d758943f9f1d204f80a90eb631f3bf064b80d53e045”,
“nonce”: “0xedeb2d8fd2b2bdec”,
“sha3_uncles”: “0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347”,
“logs_bloom”: “0xdde5fc46c5d8bcbd58207//…”,
“transactions_root”: “0xe4c7bf3aff7ad07f9e80d57f7189f0252592fee6321c2a9bd9b09b6ce0690d27”,
“state_root”: “0x49e3bfe7b618e27fde8fa08884803a8458b502c6534af69873a3cc926a7c724b”,
“receipts_root”: “0x7cf43d7e837284f036cf92c56973f5e27bdd253ca46168fa195a6b07fa719f23”,
“miner”: “0xea674fdde714fd979de3edf0f56aa9716b898ec8”,
“problem”: “7253857437305950”,
“total_difficulty”: “24325637817906576196890”,
“dimension”: “61271”,
“extra_data”: “0x65746865726d696e652d6575726f70652d7765737433”,
“gas_limit”: “14977947”,
“gas_used”: “14964688”,
“transaction_count”: “252”,
“transactions”: {
//…
}
}

Notice: We used JavaScript for the instance above. Nevertheless, you can too use a number of different programming languages, together with Python, TypeScript, and so on. To be taught extra about this, try our official get block by block quantity documentation web page.

Web3 Wallets: Python Blockchain API Instance 

When constructing a Web3 pockets, you want seamless entry to a consumer’s transaction historical past so you’ll be able to show this info. With our Blockchain API, you’ll be able to seamlessly get this information with a single name to the get_wallet_transactions() endpoint whereas passing alongside two parameters: chain and deal with. Right here’s an instance of what it’d appear like: 

params = {
“chain”: “eth”,
“deal with”: “0x1f9090aaE28b8a3dCeaDf281B0F12828e676c326”
}

end result = evm_api.transaction.get_wallet_transactions(
api_key=api_key,
params=params,
)

In return for calling the get_wallet_transactions() endpoint, you get an array of all inbound and outbound transactions of the offered deal with. That is what the response can appear like: 

{
“page_size”: 100,
“web page”: 0,
“cursor”: “eyJhbGciOiJIUzI1N//…”,
“end result”: [
{
“hash”: “0x4742fc54f2f6f1121d472ebc6cad6821f9d79407237c36782641d9873832b825”,
“nonce”: “337201”,
“transaction_index”: “149”,
“from_address”: “0x1f9090aae28b8a3dceadf281b0f12828e676c326”,
“from_address_label”: “rsync-builder”,
“to_address”: “0xde2a1a6dd691e56fc225d13ab51d4e05ea148f2f”,
“to_address_label”: null,
“value”: “38602051712948608”,
“gas”: “21000”,
“gas_price”: “13881451241”,
“input”: “0x”,
“receipt_cumulative_gas_used”: “29986680”,
“receipt_gas_used”: “21000”,
“receipt_contract_address”: null,
“receipt_root”: null,
“receipt_status”: “1”,
“block_timestamp”: “2023-10-31T07:44:23.000Z”,
“block_number”: “18468703”,
“block_hash”: “0x2c00ee96af806132e858edc91406843258d234783c39d30313644b46c2af2315”,
“transfer_index”: [
18468703,
149
]
//…
]

Notice: We used Python for this instance. Nevertheless, you can too use many different languages, together with JavaScript, TypeScript, and so on. Take a look at the official get transactions of deal with documentation to be taught extra.

Dapp Alerts: TypeScript Blockchain API Instance

When constructing dapps, you usually want a straightforward option to arrange alerts so you’ll be able to notify your finish customers of vital occasions. To take action, you want entry to occasions and logs of good contracts. 

So, to get the logs of any contract, all you want is a single API name to the getContractLogs() endpoint whereas passing alongside three parameters: deal with, chain, and matter. That is what a request may appear like: 

const response = await Moralis.EvmApi.occasions.getContractLogs({
“chain”: “0x1”,
“topic0”: “0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef”,
“deal with”: “0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB”
});

In return for calling the endpoint above, you’ll get a response containing all logs based mostly in your specs. Right here’s an instance of what it’d appear like: 

{
“page_size”: 100,
“web page”: 1,
“cursor”: “eyJhbGciO//…”,
“end result”: [
{
“transaction_hash”: “0x4abb0d08b5fea482ce708b880c60757635a38203b55fd064148c3f6301206efa”,
“address”: “0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb”,
“block_timestamp”: “2023-10-31T07:33:35.000Z”,
“block_number”: “18468649”,
“block_hash”: “0x7fd8e733e3a7c03624b9bdcaa058a6be60089093a1d0976db450fb60dbafc0db”,
“data”: “0x0000000000000000000000000000000000000000000000000000000000000001”,
“topic0”: “0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef”,
“topic1”: “0x000000000000000000000000ff36ff458b51dcd034598c56f65a8491427dc572”,
“topic2”: “0x000000000000000000000000e42abe865a7f83f8662e0d602195ade90a569ff0”,
“topic3”: null,
“transaction_index”: 76,
“log_index”: 73
},
//…
]

Notice: We used TypeScript within the instance above. Nevertheless, you will get logs utilizing different programming languages as properly, together with JavaScript, Python, and others. To be taught extra, try the official get logs for contract documentation. 

That’s it for our three examples. Within the subsequent part, we’ll provide you with a extra in-depth tutorial on how one can name the varied endpoints of the Blockchain API!

Step-by-Step Blockchain API Tutorial 

On this part, we’ll provide you with an in-depth tutorial on how one can get block information by its quantity utilizing the Moralis Blockchain API. Because of the accessibility of this industry-leading Web3 improvement instrument, you will get the data you want in three easy steps:

Get a Moralis API KeyWrite a Script Calling the getBlock() EndpointExecute the Code

Nevertheless, earlier than you’ll be able to name the getBlock() endpoint, you could cope with a couple of stipulations! 

Conditions

In at the moment’s blockchain API instance tutorial, we’ll be utilizing JavaScript. As such, if you wish to comply with alongside, ensure you have the next prepared: 

Step 1: Get a Moralis API Key 

To have the ability to name the Blockchain API and the getBlock() endpoint, you want a Moralis API key. To get an API key, you want a Moralis account. So, in the event you haven’t already, click on on the ”Begin for Free” button on the prime proper and arrange your account: 

Blockchain API Tutorial Step 1 - Sign Up with Moralis For Free

With a Moralis account at hand, head on over to the ”Settings” tab, scroll all the way down to the ”API Keys” part, and duplicate your key: 

Tutorial Step 2 - Copy your Blockchain API key

Preserve it for now, as you want it within the subsequent part! 

Step 2: Write a Script Calling the getBlock() Endpoint 

For the second step, begin by establishing a brand new undertaking in your built-in improvement surroundings (IDE). From there, open a brand new terminal and run the command under within the undertaking’s root folder to put in the Moralis SDK: 

npm set up moralis @moralisweb3/common-evm-utils

Subsequent, create a brand new ”index.js” file and add the next code: 

const Moralis = require(“moralis”).default;
const { EvmChain } = require(“@moralisweb3/common-evm-utils”);

const runApp = async () => {
await Moralis.begin({
apiKey: “YOUR_API_KEY”,
// …and some other configuration
});

const blockNumberOrHash = “15863321”;

const chain = EvmChain.ETHEREUM;

const response = await Moralis.EvmApi.block.getBlock({
blockNumberOrHash,
chain,
});

console.log(response.toJSON());
};

runApp();

You then want to vary the code barely, and you can begin by changing YOUR_API_KEY with the important thing you copied within the earlier step to initialize Moralis: 

Showing where to paste the blockchain API key

Subsequent, configure the blockNumberOrHash and chain constants to suit your question: 

Example code showing how to modify the blockchain api example to fit your query

We then cross blockNumberOrHash and chain as parameters when calling the getBlock() endpoint: 

Modifying the chain and blocknumberorhash parameters for the blockchain api

That’s it; you’re now able to run the code! 

Step 3: Execute the Code 

For the ultimate step, open a brand new terminal, cd into your undertaking’s root folder, and run the next command: 

node index.js

Executing the command above will run the code, and in return, you’ll get a bunch of knowledge associated to the block in query. Some outstanding examples embody a timestamp, log bloom, gasoline used, the miner, an array of transactions, and far more. That is what it’d appear like: 

{
“timestamp”: “2022-10-30T20:39:11.000Z”,
“quantity”: “15863321”,
“hash”: “0x4f5d3bb78f0311301ef282b281d23e178ced236a7ae465820fe6edeba609954a”,
“parent_hash”: “0x27e61d430386d7b4a144bee6e120a57010fbdb3cf963ca37e2d20b5452203621”,
“nonce”: “0x0000000000000000”,
“sha3_uncles”: “0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347”,
“logs_bloom”: “0x11e91824051850625f13 …”,
“transactions_root”: “0x1e0b205c9c48af7dfa3f277c8ec2ba403dc4ab63635a18bbfe532097527fb18e”,
“state_root”: “0xcb1df24273693eb33869961233baac112adbdf51e980e5cf8b8aa7084e8063be”,
“receipts_root”: “0x3a3d89417ad3898d47f66155e0e5fdf3e1efde2a6f389e7b051acf729db28617”,
“miner”: “0x388C818CA8B9251b393131C08a736A67ccB19297”,
“problem”: “0”,
“total_difficulty”: “58750003716598360000000”,
“dimension”: “117595”,
“extra_data”: “0x”,
“gas_limit”: “30000000”,
“gas_used”: “19044124”,
“transaction_count”: “156”,
“base_fee_per_gas”: “8175724594”,
“transactions”: [
{
//…
]
}

Congratulations; that’s it for this blockchain API tutorial. You now know easy methods to simply get a block’s contents utilizing its quantity! 

Additionally, this was only a easy blockchain API instance, and you’ll comply with the identical steps (with minor configurations to the code within the second half) to name some other of our API endpoints. 

To discover all current endpoints, try the official Moralis Blockchain API documentation web page! 

Abstract: Blockchain API Instance and Tutorial

In at the moment’s tutorial, we explored the Moralis Blockchain API, which is an industry-leading instrument for getting and integrating blockchain information into Web3 tasks, together with transactions, logs, occasions, and so on. This instrument boasts many outstanding options and helps all main chains, making Moralis clearly stand out because the premier possibility among the many greatest blockchain API suppliers within the enterprise!

Along with exploring the Blockchain API, we additionally confirmed you easy methods to use this instrument to get the content material of a block by its quantity in three easy steps: 

Get a Moralis API KeyWrite a Script Calling the getBlock() EndpointExecute the Code

As such, you probably have adopted alongside this far, you now have the abilities to fetch information, akin to blocks, occasions, logs, and far more, with Moralis’ Blockchain API!

Moralis Logo and Moralis Blockchain API Tutorial Title - Art Image

When you favored this Blockchain API tutorial, take into account studying extra guides right here on the weblog. As an example, discover the intricacies of meta transactions, examine EIP-4844, or learn to arrange notifications for NFT gross sales. Additionally, don’t neglect to enroll with Moralis. You’ll be able to create an account free of charge, and also you’ll get quick entry to all industry-leading instruments, together with the Blockchain API!  



Source link

Tags: APIBlockchainCompleteExamplesTutorial
Previous Post

Indicators Signal Surge To $38

Next Post

A PSA For Crypto Holders and LastPass Users

Related Posts

YGG Launches New Publishing Arm, Debuts First Game ‘LOL Land’
Web3

YGG Launches New Publishing Arm, Debuts First Game ‘LOL Land’

May 23, 2025
XRP Ledger (XRPL) adds 3 new stablecoins into its ecosystem
Web3

XRP Ledger (XRPL) adds 3 new stablecoins into its ecosystem

May 23, 2025
Why Solana Mobile Is Launching a Token Alongside Its New Android Phone
Web3

Why Solana Mobile Is Launching a Token Alongside Its New Android Phone

May 21, 2025
Shark Tank’s Kevin O’Leary Says Warren Wrong to ‘Tie’ Stablecoin Bill to Trump
Web3

Shark Tank’s Kevin O’Leary Says Warren Wrong to ‘Tie’ Stablecoin Bill to Trump

May 20, 2025
Elton John Slams UK AI Copyright Plan as ‘Criminal’ Theft of Creative Work
Web3

Elton John Slams UK AI Copyright Plan as ‘Criminal’ Theft of Creative Work

May 19, 2025
Guess Who: xAI Blames a ‘Rogue Employee’ for ‘White Genocide’ Grok Posts
Web3

Guess Who: xAI Blames a ‘Rogue Employee’ for ‘White Genocide’ Grok Posts

May 17, 2025
Next Post
A PSA For Crypto Holders and LastPass Users

A PSA For Crypto Holders and LastPass Users

Unlocking the Potential of Decentralized Finance: An In-Depth Look at the Credefi Ecosystem and its Revolutionary CREDI Token

Unlocking the Potential of Decentralized Finance: An In-Depth Look at the Credefi Ecosystem and its Revolutionary CREDI Token

Are They Your Crypto’s Best Friend Or A Risky Gamble?

Are They Your Crypto’s Best Friend Or A Risky Gamble?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Twitter Instagram LinkedIn RSS Telegram
Coins League

Find the latest Bitcoin, Ethereum, blockchain, crypto, Business, Fintech News, interviews, and price analysis at Coins League

CATEGORIES

  • Altcoin
  • Analysis
  • Bitcoin
  • Blockchain
  • Crypto Exchanges
  • Crypto Updates
  • DeFi
  • Ethereum
  • Metaverse
  • NFT
  • Regulations
  • Scam Alert
  • Uncategorized
  • Web3

SITEMAP

  • Disclaimer
  • Privacy Policy
  • DMCA
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2023 Coins League.
Coins League is not responsible for the content of external sites.

No Result
View All Result
  • Home
  • Bitcoin
  • Crypto Updates
    • Crypto Updates
    • Altcoin
    • Ethereum
    • Crypto Exchanges
  • Blockchain
  • NFT
  • DeFi
  • Metaverse
  • Web3
  • Scam Alert
  • Regulations
  • Analysis

Copyright © 2023 Coins League.
Coins League is not responsible for the content of external sites.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In