Moralis JS SDK Setup

Basic and advanced Moralis SDK initialization.

Basic Initialization

Install Moralis

Install the package via npm:

npm install moralis
yarn add moralis
pnpm add moralis

Initialize Moralis

The easiest way to integrate the Moralis SDK into your JavaScript project is through the npm module.

After your dependency is added, you must initialize Moralis via the start method:

import Moralis from 'moralis';

Moralis.start({
  apiKey: '<YOUR_API_KEY>',
});

After that, you can use any Moralis functionalities defined in our NodeJS SDK reference.

Configuration

You can set the configuration for your Moralis instance when you call Moralis.start(config). For example:

Moralis.start({
  apiKey: "YOUR_API_KEY",
  formatEvmAddress: 'checksum',
  formatEvmChainId: 'decimal',
  logLevel: 'verbose'
})

Below, you can find the possible options for the configuration:

OptionDescriptionDefaultRequired
apiKeyYour secret Moralis ApiKeynullyes
formatEvmAddressFormat style for evm addresses. Possible values: 'lowercase', 'checksum''lowercase'no
formatEvmChainIdFormat style for chains. Possible values: 'decimal', 'hex''hex'no
logLevelLevel of detail for log messages. Possible values: 'verbose', 'debug', 'info', 'warning', 'error', 'off''info'no

Advanced Setup

📘

Advanced Use Case

Use the advanced setup if you want more control over what modules to add to Moralis. In most cases, you won't need to do this (and can use the umbrella moralis package as described above, as this is easier to use).

Install Moralis

Instead of installing the moralis umbrella package, you can need to install the packages you want to use. Please note that you always need to install the @moralisweb3/core package.

Available packages:

  • @moralisweb3/core
  • @moralisweb3/auth
  • @moralisweb3/evm-api
  • @moralisweb3/sol-api
  • @moralisweb3/evm-utils
  • @moralisweb3/sol-utils

For example:

npm i @moralisweb3/core @moralisweb3/evm-api
yarn add @moralisweb3/core @moralisweb3/evm-api
pnpm add @moralisweb3/core @moralisweb3/evm-api

Set Up Moralis

To set up Moralis, you must register the modules to the core package at the top of your code (before any interaction with Moralis):

import MoralisCore from '@moralisweb3/core';
import MoralisEvmApi from '@moralisweb3/evm-api';

const core = MoralisCore.create();
// Register all imported modules to the @moralisweb3/core module
core.registerModules([MoralisEvmApi]);

Then, initialize the app similarly to when using the umbrella moralis package. You only need to provide a configuration that is required by the packages. So, if you don't include an API package, you might not need to include the apiKey:

core.start({
  apiKey: '<YOUR_API_KEY>',
  // ...and any other configuration
});

Now you can use any functionality from the installed modules. The only difference is that you need to call in your code:

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

const evmApi = core.getModule<MoralisEvmApi>(MoralisEvmApi.moduleName);
evmApi.block.getBlock({
    chain: EvmChain.ETHEREUM,
    blockNumberOrHash: "",
});

Instead of:

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

Moralis.EvmApi.block.getBlock({
    chain: EvmChain.ETHEREUM,
    blockNumberOrHash: "",
});

Of course, you are free to combine the modules in a single object and use that in your dapp:

// moralis.ts
import { MoralisCore } from '@moralisweb3/core';
import EvmApi from '@moralisweb3/evm-api';

const core = MoralisCore.create();
const evmApi = EvmApi.create(core);
core.registerModules([evmApi]);

export const Moralis = {
  EvmApi: evmApi,
};

// app.ts
import { Moralis } from './moralis/';
import { EvmChain } from '@moralisweb3/evm-utils';

Moralis.EvmApi.block.getBlock({
    chain: EvmChain.ETHEREUM,
    blockNumberOrHash: "",
});

Did this page help you?