🖨️Connect with Node.js
Once you have your Moralis Server launched it's time to connect to it via the Moralis SDK. This guide will show you how you can do it in just a few easy steps.
Installing Moralis SDK
Run the following command to install Moralis SDK
SDK Initialization
You need to initialize Moralis SDK with the following syntax in node.js:
Create a file index.ts
and add below code:
with masterKey
you can directly access the Moralis dashboard without the need for authentication.
Note: With the master key you can use the API, RPC nodes and other features of your Moralis account using the SDK straight from your backend.
Please remember to never leak your master key because once someone gets your master key they will have full access to your Moralis account.
DB query
Saving data
To save object with data copy-paste the following code:
Create a file SaveData.ts
and add below code:
Run the following command in your terminal:
Go to your Moralis dashboard and you will see the data saved in the database:
Query
Create a file FindQuery.ts
and add below code:
Run:
In your console you will see:
For more info on DB Queries click here
Live Query
Subscribing to Queries to Get Real-Time Alerts Whenever Data in the Query Result Set Changes.
Create a file LiveQuery.ts
add the following code in your file:
Run:
In your console you will see:
For more info on Live Queries click here
Web3API use
Create a file Web3API.ts
and add the below code:
with moralisSecret
all API calls go directly to the API instead of passing through the Moralis Server.
To get moralisSecret
you need to go to account settings as shown in image below
then API and copy your moralisSecret
key
Run:
You will see the following result:
Add New Address Sync From Code
The Sync and Watch Address
plugin calls a Cloud Function called watchXxxAddressunder the hood, where "Xxx" are the chain names here. These cloud functions can also be called directly from your own code!
Create a file watchAddr.ts
and add below code:
Run:
in Terminal you will see:
The transaction data is stored in Moralis Dashboard:
By default, only new transactions made by addresses being watched by using this cloud function will be added to the database. If you also want to add historical data for addresses that you want to watch, you can add sync_historical:true
Note: The watch address functions return no value as they start a job. They are still asynchronous though! Once the promise returns the synced transactions, they should be in the XxxTransactions table for the corresponding chain.
Add New Event Sync From Code
Watch new smart contract event
Moralis Server has a special cloud function called watchContractEvent(options)
. You can call it using the master key.
Note: at the moment the events created via code won't be seen in the admin UI, you can only see them in the database, we are working on connecting the admin UI properly
Create a file watchEvent.ts
and add below code:
Run:
In the terminal you will see:
The Event data is succesfully stored in Moralis Dashboard
Last updated