Skip to Content

Moralis Integration

Moralis is an enterprise-grade Web3 API provider that connects any tech stack to blockchain networks, offering multichain blockchain data for wallets, tokens, NFTs, DeFi protocols, and more through one fast, reliable API.

Moralis provides a comprehensive Backend-as-a-Service (BaaS) platform specifically designed for developers. By abstracting away the complexity of blockchain integration and offering a suite of APIs, SDKs, and scalable infrastructure, Moralis allows developers to focus on building the core functionality of their applications.

What You’ll Learn

By the end of this tutorial, you’ll understand:

  • How to set up and configure Moralis for Sei blockchain integration
  • Working with different Moralis APIs (Wallet, NFT, Token, and Blockchain APIs)
  • Fetching wallet transactions and balances
  • Retrieving NFT holder information and metadata
  • Getting transaction details and token information

Prerequisites

Before starting with this guide, ensure you have:

  1. Moralis Account: Create a free account at moralis.io 
  2. Node.js: Version 14 or higher installed
  3. Package Manager: npm, yarn, or pnpm
  4. TypeScript: Basic knowledge (optional but recommended)

Sei Network Configuration

Following is the configuration details of Sei for Moralis integration:

Mainnet:

Testnet:

Moralis APIs Overview

Moralis offers several powerful APIs that work seamlessly across multiple blockchains:

  • NFT API: Access comprehensive NFT data including metadata, ownership details, and transaction history
  • Wallet API: Retrieve real-time wallet data including balances, transaction history, and token holdings
  • Token API: Get token information, prices, and transfer data
  • Blockchain API: Fetch raw blockchain data - blocks, transactions, and logs
  • Price API: Retrieve real-time and historical crypto token prices
  • DeFi API: Query DeFi protocol data including pools, positions, and yields

Setting Up Your Project

Install Moralis SDK

First, create a new project and install the Moralis SDK:

# Create new project mkdir moralis-sei-integration cd moralis-sei-integration # Initialize package.json npm init -y # Install Moralis SDK npm install moralis # For TypeScript projects, also install: npm install @moralisweb3/common-evm-utils npm install typescript ts-node @types/node --save-dev

Get Your API Key

  1. Sign up at moralis.io 
  2. Navigate to “Project Settings”
  3. Copy your API key from the dashboard

Basic Configuration

Create a .env file in your project root:

MORALIS_API_KEY=your_api_key_here SEI_MAINNET_CHAIN_ID=0x531 SEI_TESTNET_CHAIN_ID=0x530

Initialize Moralis

Create an index.ts or index.js file:

import Moralis from 'moralis'; // Initialize Moralis const initMoralis = async () => { await Moralis.start({ apiKey: process.env.MORALIS_API_KEY }); console.log('Moralis initialized successfully'); }; // Call initialization initMoralis();

Wallet Transactions API

// Get wallet transactions for Sei mainnet const getWalletTransactions = async (walletAddress: string) => { try { const response = await Moralis.EvmApi.transaction.getWalletTransactions({ address: walletAddress, chain: '0x531' // Sei mainnet }); console.log('Wallet Transactions:', response.raw); return response.raw; } catch (error) { console.error('Error fetching wallet transactions:', error); throw error; } }; // Example usage const walletAddress = '0x742d35Cc6636C0532925a3b8D62c5bceCdB8eBc8'; getWalletTransactions(walletAddress);

NFT Holders API

// Get NFTs owned by wallet address const getWalletNFTs = async (walletAddress: string) => { try { const response = await Moralis.EvmApi.nft.getWalletNFTs({ address: walletAddress, chain: '0x531' // Sei mainnet }); console.log('Wallet NFTs:', response.raw); return response.raw; } catch (error) { console.error('Error fetching wallet NFTs:', error); throw error; } }; // Get NFT owners for a specific contract const getNFTOwners = async (contractAddress: string) => { try { const response = await Moralis.EvmApi.nft.getNFTOwners({ address: contractAddress, chain: '0x531' // Sei mainnet }); console.log('NFT Owners:', response.raw); return response.raw; } catch (error) { console.error('Error fetching NFT owners:', error); throw error; } };

Transaction Details API

// Get detailed transaction information const getTransactionDetails = async (transactionHash: string) => { try { const response = await Moralis.EvmApi.transaction.getTransaction({ transactionHash: transactionHash, chain: '0x531' // Sei mainnet }); console.log('Transaction Details:', response.raw); return response.raw; } catch (error) { console.error('Error fetching transaction details:', error); throw error; } }; // Get transaction verbose (with decoded logs) const getTransactionVerbose = async (transactionHash: string) => { try { const response = await Moralis.EvmApi.transaction.getTransactionVerbose({ transactionHash: transactionHash, chain: '0x531' // Sei mainnet }); console.log('Transaction Verbose:', response.raw); return response.raw; } catch (error) { console.error('Error fetching verbose transaction:', error); throw error; } };

Token API

// Get token balances for a wallet const getTokenBalances = async (walletAddress: string) => { try { const response = await Moralis.EvmApi.token.getWalletTokenBalances({ address: walletAddress, chain: '0x531' // Sei mainnet }); console.log('Token Balances:', response.raw); return response.raw; } catch (error) { console.error('Error fetching token balances:', error); throw error; } }; // Get token transfers for a wallet const getTokenTransfers = async (walletAddress: string) => { try { const response = await Moralis.EvmApi.token.getWalletTokenTransfers({ address: walletAddress, chain: '0x531' // Sei mainnet }); console.log('Token Transfers:', response.raw); return response.raw; } catch (error) { console.error('Error fetching token transfers:', error); throw error; } }; // Get token metadata const getTokenMetadata = async (tokenAddress: string) => { try { const response = await Moralis.EvmApi.token.getTokenMetadata({ addresses: [tokenAddress], chain: '0x531' // Sei mainnet }); console.log('Token Metadata:', response.raw); return response.raw; } catch (error) { console.error('Error fetching token metadata:', error); throw error; } };

Complete Integration Example

Here’s a comprehensive example that brings everything together:

import Moralis from 'moralis'; import dotenv from 'dotenv'; dotenv.config(); class SeiMoralisService { private initialized = false; async initialize() { if (!this.initialized) { await Moralis.start({ apiKey: process.env.MORALIS_API_KEY! }); this.initialized = true; console.log('Moralis initialized for Sei blockchain'); } } async getWalletOverview(walletAddress: string) { await this.initialize(); try { // Get all wallet data in parallel const [transactions, nfts, tokenBalances, tokenTransfers] = await Promise.all([this.getWalletTransactions(walletAddress), this.getWalletNFTs(walletAddress), this.getTokenBalances(walletAddress), this.getTokenTransfers(walletAddress)]); return { address: walletAddress, transactions: transactions.result, nfts: nfts.result, tokenBalances: tokenBalances, tokenTransfers: tokenTransfers.result, summary: { totalTransactions: transactions.total, totalNFTs: nfts.total, totalTokenTypes: tokenBalances.length, totalTransfers: tokenTransfers.total } }; } catch (error) { console.error('Error getting wallet overview:', error); throw new Error('Failed to retrieve wallet overview'); } } async getWalletTransactions(walletAddress: string, options = {}) { const defaultOptions = { chain: '0x531', ...options }; return await Moralis.EvmApi.transaction.getWalletTransactions({ address: walletAddress, ...defaultOptions }); } async getWalletNFTs(walletAddress: string) { return await Moralis.EvmApi.nft.getWalletNFTs({ address: walletAddress, chain: '0x531' }); } async getTokenBalances(walletAddress: string) { const response = await Moralis.EvmApi.token.getWalletTokenBalances({ address: walletAddress, chain: '0x531' }); return response.raw; } async getTokenTransfers(walletAddress: string) { return await Moralis.EvmApi.token.getWalletTokenTransfers({ address: walletAddress, chain: '0x531' }); } async getTransactionDetails(txHash: string) { return await Moralis.EvmApi.transaction.getTransactionVerbose({ transactionHash: txHash, chain: '0x531' }); } } // Usage example const seiService = new SeiMoralisService(); async function main() { try { const walletAddress = 'your_wallet_address_here'; const overview = await seiService.getWalletOverview(walletAddress); console.log('Wallet Overview:', JSON.stringify(overview, null, 2)); } catch (error) { console.error('Main execution error:', error); } } main();

Working with Testnet

To switch to Sei testnet, simply change the chain ID in your API calls.
// Testnet configuration const TESTNET_CONFIG = { chainId: '0x530', // Sei testnet EVM chain ID rpcUrl: 'https://evm-rpc-testnet.sei-apis.com', explorer: 'https://testnet.seiscan.io' }; // Use testnet in your API calls const response = await Moralis.EvmApi.transaction.getWalletTransactions({ address: walletAddress, chain: TESTNET_CONFIG.chainId });

Additional Resources

This guide covered the fundamentals of integrating Moralis with Sei blockchain. For more advanced features and detailed documentation, explore:

Last updated on