Skip to content

Access Moonbeam Data via Moralis APIs

Introduction

As a one-stop solution for blockchain development, Moralis offers a comprehensive platform that empowers developers to create, launch, and scale decentralized applications (dApps) with ease.

It provides a suite of tools and services designed to streamline the process of accessing Web3 data and interacting with blockchain networks. Its offerings are structured into these primary categories:

  • EVM API - The EVM API allows developers to query essential data for Ethereum Virtual Machine (EVM)-compatible blockchains. Moralis provides dedicated APIs for four key areas: NFTs, tokens, wallets, and general blockchain information

  • Streams API - the Streams API enables developers to listen for on-chain events, such as smart contract event emissions, in real-time. Popular use cases include real-time wallet notifications, asset monitoring, and gaming event notifications

  • RPC API - the RPC API offers a secure and reliable connection to various blockchain networks. It provides a high-performance gateway for developers to interact with blockchain nodes, ensuring stable and efficient communication between dApps and the blockchain

This guide will show you how to access the API endpoints for Moonbeam using curl commands and the Moralis SDK using JavaScript and Python snippets.

The information presented herein is for informational purposes only and has been provided by third parties. Moonbeam does not endorse any project listed and described on the Moonbeam docs website (https://docs.moonbeam.network/).

Checking Prerequisites

To interact with Moralis' API endpoints, an account and API key are required.

Head to the sign up page to create an account. Once you're in, navigate to the API keys section. There, you can generate your own unique API key. Be sure to copy it for future use.

Moralis API key

Querying the EVM API

With the API key, you can try Moralis' REST APIs. The following examples show how the EVM API works:

Get the token balance of a wallet:

curl --request GET \
--url 'https://deep-index.moralis.io/api/v2.2/0xd4d7fb1f98dD66f6D1f393E8e237AdF74c31F3ea/erc20?chain=moonbeam' \
--header 'accept: application/json' \
--header 'X-API-Key: INSERT_YOUR_API_KEY' 

Get the list of owners of an NFT:

curl --request GET \
--url 'https://deep-index.moralis.io/api/v2.2/nft/0xfffffffffffffffffffffffffffffffffffffffff/owners?chain=moonbeam&format=decimal' \
--header 'accept: application/json' \
--header 'X-API-Key: INSERT_YOUR_API_KEY' 

For a comprehensive overview of EVM APIs and their capabilities, please refer to Moralis' official documentation.

Using the Moralis SDK

Moralis has an SDK that allows developers to seamlessly integrate Moralis' API into their backend infrastructure. This SDK offers a wide array of features, including:

  • Data querying from EVM APIs
  • Integration of Web3 authentication
  • A collection of utility functions for efficient data transformation and formatting

Moralis currently provides official SDK support for two primary programming languages:

  • Node.js
  • Python

You can install Moralis SDK with the following commands:

npm install moralis
yarn add moralis
pnpm add moralis
pip install moralis

Once the SDK is ready, you can leverage it to query Moralis APIs. Here's an example of how to interact with the EVM API using a JSON-RPC interface:

import Moralis from 'moralis';

try {
  await Moralis.start({
    apiKey: 'INSERT_YOUR_API_KEY',
  });

  const response = await Moralis.EvmApi.block.getBlock({
    chain: '0x504',
    blockNumberOrHash: '1',
  });

  console.log(response.raw);
} catch (e) {
  console.error(e);
}
from moralis import evm_api

api_key = "INSERT_YOUR_API_KEY"

params = {"chain": "moonbeam", "block_number_or_hash": "1"}

result = evm_api.block.get_block(
    api_key=api_key,
    params=params,
)

print(result)

For more information on advanced features and configurations within the Moralis SDK, please refer to the official JavaScript and Python documentation.

Accessing the Stream API

The Moralis Streams API is a tool for developers to listen to and react to events happening on blockchains in real time. This API lets you listen for specific events, such as a new transaction being added to a block, a particular smart contract function being called, or an NFT being transferred and delivering the event via Webhook.

Head over to the Streams page from your Moralis dashboard and click Create a new stream to get started.

stream API page

To get started quickly, choose a predefined template. This example focuses on transactions related to xcDOT. Select the Contract Activity template to listen for events related specifically to xcDOT.

template selection page

On the next page, enter the following details:

  1. Enter the smart contract address for xcDOT:
    0xFfFFfFff1FcaCBd218EDc0EbA20Fc2308C778080
    
  2. Select Moonbeam from the available network options. Moralis supports listening to events on Moonbeam, Moonriver, and Moonbase Alpha
  3. Customize the events you want to receive. For this example, choose All contract events. You may also test the stream to confirm it captures the needed data
  4. Connect the stream to an endpoint to receive updates from Moralis
  5. Click Save to activate your stream

details input page

Now you're all set. Once you activate your stream, it will be added to your list of streams on your dashboard.

List of streams on Moralis dashboard

Accessing the RPC API

To use the Moralis Moonbeam RPC endpoint, visit the Nodes page, click Create Node, and take the following steps:

  1. Select Moonbeam as the protocol
  2. Choose Mainnet as the chain
  3. Click Create Node

RPC API setup page

Your RPC endpoint will be generated for you to easily copy, and your Moonbeam node will be up and running in seconds, ready to handle your RPC requests. You can view your nodes at any time from your dashboard.

node ready page

And that's it! Hope this guide has been helpful. For advanced features and more complex use cases, refer to the official Moralis documentation for further details.

The information presented herein has been provided by third parties and is made available solely for general information purposes. Moonbeam does not endorse any project listed and described on the Moonbeam Doc Website (https://docs.moonbeam.network/). Moonbeam Foundation does not warrant the accuracy, completeness or usefulness of this information. Any reliance you place on such information is strictly at your own risk. Moonbeam Foundation disclaims all liability and responsibility arising from any reliance placed on this information by you or by anyone who may be informed of any of its contents. All statements and/or opinions expressed in these materials are solely the responsibility of the person or entity providing those materials and do not necessarily represent the opinion of Moonbeam Foundation. The information should not be construed as professional or financial advice of any kind. Advice from a suitably qualified professional should always be sought in relation to any particular matter or circumstance. The information herein may link to or integrate with other websites operated or content provided by third parties, and such other websites may link to this website. Moonbeam Foundation has no control over any such other websites or their content and will have no liability arising out of or related to such websites or their content. The existence of any such link does not constitute an endorsement of such websites, the content of the websites, or the operators of the websites. These links are being provided to you only as a convenience and you release and hold Moonbeam Foundation harmless from any and all liability arising from your use of this information or the information provided by any third-party website or service.
Last update: August 27, 2024
| Created: July 10, 2024