Cloud Functions
Offload Compute Intensive or Security Sensitive Functions to the Server.

Defining Cloud Functions

For complex apps, sometimes you just need a bit of logic that isn’t running on a mobile device. Cloud Code makes this possible.
Cloud Code is easy to use because it’s built on the same Moralis JavaScript SDK that powers thousands of apps. The only difference is that this code runs in your Moralis Server rather than running on the user’s mobile device. When you update your Cloud Code, it becomes available to all mobile environments instantly. You don’t have to wait for a new release of your application. This lets you change app behavior on the fly and add new features faster.
Even if you’re only familiar with mobile development, we hope you’ll find Cloud Code straightforward and easy to use.
Let’s look at a slightly more complex example where Cloud Code is useful. One reason to do computation in the cloud is so that you don’t have to send a huge list of objects down to a device if you only want a little bit of information. For example, let’s say you’re writing an app that lets people review movies. A single Review object could look like:
1
{
2
"movie": "The Matrix",
3
"stars": 5,
4
"comment": "Too bad they never made any sequels."
5
}
Copied!
If you wanted to find the average number of stars for The Matrix, you could query for all of the reviews, and the average amount of stars on the device. However, this uses a lot of bandwidth when you only need a single number. With Cloud Code, we can just pass up the name of the movie, and return the average star rating.
Cloud Functions accept a JSON parameters dictionary on the request object, so we can use that to pass up the movie name. The entire Moralis JavaScript SDK is available in the cloud environment, so we can use that to query over Review objects. Together, the code to implement averageStars looks like this:
1
Moralis.Cloud.define("averageStars", async (request) => {
2
const query = new Moralis.Query("Review");
3
query.equalTo("movie", request.params.movie);
4
const results = await query.find();
5
let sum = 0;
6
for (let i = 0; i < results.length; ++i) {
7
sum += results[i].get("stars");
8
}
9
return sum / results.length;
10
});
Copied!
The only difference between using averageStars and hello is that we have to provide the parameter that will be accessed in request.params.movie when we call the Cloud Function. Read on to learn more about how Cloud Functions can be called.

Global Packages

The following packages are available globally within Cloud Function code and can be used without a require statement.

Console.log

For debugging or informational purposes it's often useful to print messages. A logger can be obtained for this purpose. It will print messages to the Moralis Dashboard in the "Logs > Info" section.
1
const logger = Moralis.Cloud.getLogger();
2
logger.info("Hello World");
Copied!

Printing Logs in Real-Time in the Console

Video explaining how to use the CLI in order to get logs in real-time.
1
moralis-admin-cli get-logs --moralisApiKey MORALIS_CLI_API_KEY --moralisApiSecret MORALIS_CLI_SECRET_KEY
Copied!
To learn more about CLI, how to install CLI and how to work with CLI please check the CLI docs using the link below.
To get started, you need to install it by running the following code in the terminal:

IDE Setup

You can write your Cloud Functions in your preferred IDE by making use of the moralis-admin-cli.
Exact time-stamp where we explain how to setup an IDE on your local machine.
To get started, you need to install it by running the following code in the terminal:
1
npm install -g moralis-admin-cli
Copied!
After you have installed the Moralis Admin CLI, you can head to the admin panel and open up the Cloud Functions on the server you want to work on.
In the lower part of the modal, there will be a code snippet that you need to run in the terminal.
The only thing you need to change is the path to the local JavaScript folder on your computer that contains the Cloud Functions.
1
moralis-admin-cli watch-cloud-folder --moralisApiKey your_api_key --moralisApiSecret your_api_secret --moralisSubdomain subdomain.moralis.io --autoSave 1 --moralisCloudfolder /path/to/cloud/folder
Copied!
After you've run the command, the cloud code will be updated automatically on the backend with each save!

Calling Cloud Functions

1
const params = { movie: "The Matrix" };
2
const ratings = await Moralis.Cloud.run("averageStars", params);
3
// ratings should be 4.5
Copied!
In general, two arguments will be passed into Cloud Functions:
  1. 1.
    request - The request object contains information about the request. The following fields are set:
  2. 2.
    params - The parameters object is sent to the function by the client.
  3. 3.
    user - The Moralis.User that is making the request. This will not be set if there was no logged-in user.
If the function is successful, the response in the client looks like this:
1
{ "result": 4.8 }
Copied!
If there is an error, the response in the client looks like this:
1
{
2
"code": 141,
3
"error": "movie lookup failed"
4
}
Copied!

Using the Master Key in Cloud Code

Set useMasterKey:true in the requests that require the master key.

Examples:

1
query.find({useMasterKey:true});
2
object.save(null,{useMasterKey:true});
3
Moralis.Object.saveAll(objects,{useMasterKey:true});
Copied!
Note that master key is accessible by default in a cloud function when you use useMasterKey:true
Important: Master key should not be used on your frontend code as it would be accessible in the user's browser.
In case if you are using master key from another backend server that you are controlling you can use the following code to intialize the master key:
1
const Moralis = require("moralis/node"); // Node.js
2
const appId = "YOUR_MORALIS_APP_ID";
3
const serverUrl = "YOUR_MORALIS_SERVER_URL";
4
const masterKey = "YOUR_MORALIS_MASTER_KEY";
5
6
Moralis.start({ serverUrl, appId, masterKey });
Copied!

Implementing Cloud Function Validation

It’s important to make sure the parameters required for a Cloud Function are provided and are in the necessary format. you can specify a validator function or object which will be called prior to your Cloud Function.
Let’s take a look at the averageStars example. If you wanted to make sure that request.params.movie is provided, and that averageStars can only be called by logged-in users, you could add a validator object to the function.
1
Moralis.Cloud.define("averageStars", async (request) => {
2
const query = new Moralis.Query("Review");
3
query.equalTo("movie", request.params.movie);
4
const results = await query.find();
5
let sum = 0;
6
for (let i = 0; i < results.length; ++i) {
7
sum += results[i].get("stars");
8
}
9
return sum / results.length;
10
},{
11
fields : ['movie'],
12
requireUser: true
13
});
Copied!
If the rules specified in the validator object aren’t met, the Cloud Function won’t run. This means that you can confidently build your function, knowing that request.params.movie is defined, as well as request.user.

More Advanced Validation

Often, not only is it important that request.params.movie is defined, but also that it's the correct data type. You can do this by providing an Object to the fields parameter in the "Validator."
1
Moralis.Cloud.define("averageStars", async (request) => {
2
const query = new Moralis.Query("Review");
3
query.equalTo("movie", request.params.movie);
4
const results = await query.find();
5
let sum = 0;
6
for (let i = 0; i < results.length; ++i) {
7
sum += results[i].get("stars");
8
}
9
return sum / results.length;
10
},{
11
fields : {
12
movie : {
13
required: true,
14
type: String,
15
options: val => {
16
return val.length < 20;
17
},
18
error: "Movie must be less than 20 characters"
19
}
20
},
21
requireUserKeys: {
22
accType : {
23
options: 'reviewer',
24
error: 'Only reviewers can get average stars'
25
}
26
}
27
});
Copied!
This function will only run if:
  • request.params.movie is defined.
  • request.params.movie is a String.
  • request.params.movie is less than 20 characters.
  • request.user is defined.
  • request.user.get('accType') is defined.
  • request.user.get('accType') is equal to ‘reviewer.’
However, the requested user could set ‘accType’ to 'reviewer' and then recall the function. Here, you could provide validation on a Moralis.User beforeSave trigger. beforeSave validators have a few additional options available, to help you make sure your data is secure.
1
Moralis.Cloud.beforeSave(Moralis.User, () => {
2
// any additional beforeSave logic here
3
}, {
4
fields: {
5
accType: {
6
default: 'viewer',
7
constant: true
8
},
9
},
10
});
Copied!
This means that the field accType on Moralis.User will be ‘viewer’ on signup, and will be unchangeable, unless masterKey is provided.
The full range of built-in validation options are:
  • requireMaster: Whether the function requires a masterKey to run.
  • requireUser: Whether the function requires a request.user to run.
  • validateMasterKey: Whether the validator should run on masterKey (defaults to false).
  • fields: An Array or Object of fields that are required on the request.
  • requireUserKeys: An Array of fields to be validated on request.user.
The full range of built-in validation options on .fields are:
  • type: The type of the request.params[field] or request.object.get(field).
  • default: What the field should default to if it’s null.
  • required: Whether the field is required.
  • options: A singular option, an array of options, or custom function of allowed values for the field.
  • constant: Whether the field is immutable.
  • error: A custom error message if validation fails.
You can also pass a function to the Validator. This can help you apply reoccurring logic to your Cloud Code.
1
const validationRules = request => {
2
if (request.master) {
3
return;
4
}
5
if (!request.user || request.user.id !== 'masterUser') {
6
throw 'Unauthorized';
7
}
8
}
9
10
Moralis.Cloud.define('adminFunction', request => {
11
// do admin code here, confident that request.user.id is masterUser, or masterKey is provided
12
},validationRules)
13
14
Moralis.Cloud.define('adminFunctionTwo', request => {
15
// do admin code here, confident that request.user.id is masterUser, or masterKey is provided
16
},validationRules)
Copied!

SOME CONSIDERATIONS TO BE AWARE OF

  • The validation function will run prior to your Cloud Code Functions. You can use async and promises here, but try to keep the validation as simple and fast as possible so your cloud requests resolve quickly.
  • As previously mentioned, cloud validator objects will not validate if a master key is provided, unless validateMasterKey:true is set. However, if you set your validator to a function, the function will always run.

Calling via REST API

Cloud Functions can be called directly using a simple GET request. Add your Moralis App Id and any Cloud Function arguments as query parameters to your Moralis server URL. Say you had the following Cloud Function:
1
Moralis.Cloud.define("Hello", (request) => {
2
return `Hello ${request.params.name}! Cloud functions are cool!`
3
});
Copied!
Then the URL would look something like the following:
1
https://1a2b3c4c5d6f.moralis.io:2053/server/functions/Hello?_ApplicationId=1a2b3c4c5d6f1a2b3c4c5d6f&name=CryptoChad
Copied!
The URL has the following structure:
  1. 1.
    Full Morlis server url.
  2. 2.
    /functions/.
  3. 3.
    Cloud Function Name.
  4. 4.
    ?_ApplicationId=yourMoralisAppId.
  5. 5.
    (optional) Cloud Function param key/value pairs: &param1=value&param2=value.

Web3

Web3 functions are available within Cloud Code including the ability to call contract methods. Moralis uses the Web3.js library.
1
// get a web3 instance for a specific chain
2
const web3 = Moralis.web3ByChain("0x1"); // mainnet
Copied!
Ask for a web3 object by supplying the chainId for the blockchain you wish to connect to. The following is a list of the currently supported chains.
Note: the web3 instance returned by Moralis.web3ByChain() cannot sign transactions. There is a way to sign a transaction using a private key, but this is NOT recommended for security reasons.
Chain Name
ChainId
Ethereum (Mainnet)
0x1
Ropsten (Ethereum Testnet)
0x3
Rinkeby
0x4
Goerli (Ethereum Testnet)
0x5
Kovan
0x2a
Binance Smart Chain (Mainnet)
0x38
Binance Smart Chain (Testnet)
0x61
Matic (Mainnet)
0x89
Mumbai (Matic Testnet)
0x13881
Local Dev (Ganache, Hardhat)
0x539

Contracts

Once you have a web3 instance, you can use it to make contract calls by constructing a contract instance with the ABI and contract address.
1
const contract = new web3.eth.Contract(abi, address);
Copied!
For convenience, Moralis bundles the Openzepplin ABI for ERC20, ERC721, and ERC1155.
  • Moralis.Web3.abis.erc20
  • Moralis.Web3.abis.erc721
  • Moralis.Web3.abis.erc1155
Bringing it all together...
1
const web3 = Moralis.web3ByChain("0x38"); // BSC
2
const abi = Moralis.Web3.abis.erc20;
3
const address = "0x...."
4
5
// create contract instance
6
const contract = new web3.eth.Contract(abi, address);
7
8
// get contract name
9
const name = await contract.methods
10
.name()
11
.call()
12
.catch(() => "");
Copied!
For more details on the Web3.js contract interface, see the web3.eth.Contract section of the Web3.js docs.
The Web3 instance returned by Moralis.web3ByChain() cannot sign transactions. In the near future, it may be possible to do this with custom plugins. For now, if you need to make on-chain contract interactions, consider doing them on the frontend or create a NodeJS backend where you have access to Truffle's HdWalletProvider.

Contract ABI

To find the ABI for a contract already published on Ethereum's Mainnet you can look on Etherscan by searching for the contract address and looking in the "Contract" tab. There are similar block explorers for other chains where the ABI is published. For instance, you can go to BscScan for Binance Smart Chain.
For your own contracts, the ABI can be found in the build directory after compiling the contract
  • Truffle: /truffle/build/contracts/myContract.json.
  • Hardhat: /artifacts/contracts/myContract.sol/myContract.json.
Last modified 25d ago