Cloud Functions

Offload Compute Intensive or Security Sensitive Functions to the Server.

Define Cloud Functions

Follow the tutorial for an introduction to Cloud Functions: Tutorial

For complex apps, sometimes you 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 Dapp 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 behaviour 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.

Note: Starting June 2022 you can only write Cloud Function code through CLI/IDE (setup).

Let’s look at a slightly more complex example where Cloud Code is useful. One reason to do the 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 this:

{
  "movie": "The Matrix",
  "stars": 5,
  "comment": "Too bad they never made any sequels."
}

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:

cloud.js
Moralis.Cloud.define("averageStars", async (request) => {
  const query = new Moralis.Query("Review");
  query.equalTo("movie", request.params.movie);
  const results = await query.find();
  let sum = 0;
  for (let i = 0; i < results.length; ++i) {
    sum += results[i].get("stars");
  }
  return sum / results.length;
});

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.

⚠️ IMPORTANT - Don't create any global variable

Cloud Functions cannot have state. They can read and write data to the database but you can't create global variables like in the example below.

cloud.js
let name = "Satoshi"; // NOT ALLOWED

Moralis.Cloud.define("functionName", async (request) => {
  let age = 20; // allowed
});

The reason is that your cloud code will get load balanced across many instances of your server so that Moralis can infinitely scale your app.

Note: The different instances of your server won't share any variables defined outside the function bodies.

All instances of your server will share the same database. Therefore if you need to share data across instances it's recommended you store it in the database.

What happens if I create global variables?

You will get unexpected results. Consider the example below.

cloud.js
let count = 0; // very bad

Moralis.Cloud.define("increment", async (request) => {
  logger.info(count);
  count++;
});

// If you call the function above you may get the following results
// 0
// 0
// 0
// 1
// 1
// 2
// 1
// 3
// 0
// 2

Why is the increment function not working properly?

Because each time the request gets randomly routed to different instances of your server and each instance has its own separate count variable.

Therefore we hope you now understand why you should not use global variables 🙌

What about global constants?

Global constants are ok to use as they will just be copied to all instances and won't change.

IDE Setup

You can find instructions specifically for your server in the server's settings in the Cloud Functions tab

You can write your Cloud Functions in your preferred IDE by making use of the moralis-admin-cli.

To get started, you need to install it by running the following code in the terminal:

npm install -g moralis-admin-cli

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.

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

After you've run the command, the cloud code will be updated automatically on the backend with each save!

Legacy UI is present in the video, some things might be different

Debugging

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.

cloud.js
const logger = Moralis.Cloud.getLogger();
logger.info("Hello World");

Printing Logs in Real-Time in the Console

moralis-admin-cli get-logs --moralisApiKey MORALIS_CLI_API_KEY --moralisApiSecret MORALIS_CLI_SECRET_KEY

To learn more about CLI, how to install CLI and how to work with CLI please check the CLI docs using the link below.

pageCLI

Legacy UI is present in the video, some things might be different

Call Cloud Functions

index.js
const params = { movie: "The Matrix" };
const ratings = await Moralis.Cloud.run("averageStars", params);
// ratings should be 4.5

In general, two arguments will be passed into Cloud Functions:

  1. request - The request object contains information about the request. The following fields are set:

  2. params - The parameters object is sent to the function by the client.

  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:

{ "result": 4.8 }

If there is an error, the response in the client looks like this:

{
  "code": 141,
  "error": "movie lookup failed"
}

Call 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:

cloud.js
Moralis.Cloud.define("Hello", (request) => {
  return `Hello ${request.params.name}! Cloud functions are cool!`;
});

Then the URL would look something like the following:

https://1a2b3c4c5d6f.moralis.io:2053/server/functions/Hello?_ApplicationId=1a2b3c4c5d6f1a2b3c4c5d6f&name=CryptoChad

The URL has the following structure:

  1. Full Moralis server url.

  2. /functions/.

  3. Cloud Function Name.

  4. ?_ApplicationId=yourMoralisAppId.

  5. (optional) Cloud Function param key/value pairs: &param1=value&param2=value.

Master Key in Cloud Code

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

Examples:

cloud.js
query.find({ useMasterKey: true });
object.save(null, { useMasterKey: true });
Moralis.Object.saveAll(objects, { useMasterKey: true });

Note: The master key is accessible by default in a cloud function when you use useMasterKey:true

Important: The 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:

index.js | Node.js
const Moralis = require("moralis/node"); // Node.js
const appId = "YOUR_MORALIS_APP_ID";
const serverUrl = "YOUR_MORALIS_SERVER_URL";
const masterKey = "YOUR_MORALIS_MASTER_KEY";

Moralis.start({ serverUrl, appId, masterKey });

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.

cloud.js
Moralis.Cloud.define(
  "averageStars",
  async (request) => {
    const query = new Moralis.Query("Review");
    query.equalTo("movie", request.params.movie);
    const results = await query.find();
    let sum = 0;
    for (let i = 0; i < results.length; ++i) {
      sum += results[i].get("stars");
    }
    return sum / results.length;
  },
  {
    fields: ["movie"],
    requireUser: true,
  }
);

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.

Advanced Cloud Function 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."

cloud.js
Moralis.Cloud.define(
  "averageStars",
  async (request) => {
    const query = new Moralis.Query("Review");
    query.equalTo("movie", request.params.movie);
    const results = await query.find();
    let sum = 0;
    for (let i = 0; i < results.length; ++i) {
      sum += results[i].get("stars");
    }
    return sum / results.length;
  },
  {
    fields: {
      movie: {
        required: true,
        type: String,
        options: (val) => {
          return val.length < 20;
        },
        error: "Movie must be less than 20 characters",
      },
    },
    requireUserKeys: {
      accType: {
        options: "reviewer",
        error: "Only reviewers can get average stars",
      },
    },
  }
);

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.’

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 a 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.

cloud.js
const validationRules = (request) => {
  if (request.master) {
    return;
  }
  if (!request.user || request.user.id !== "masterUser") {
    throw "Unauthorized";
  }
};

Moralis.Cloud.define(
  "adminFunction",
  (request) => {
    // do admin code here, confident that request.user.id is masterUser, or masterKey is provided
  },
  validationRules
);

Moralis.Cloud.define(
  "adminFunctionTwo",
  (request) => {
    // do admin code here, confident that request.user.id is masterUser, or masterKey is provided
  },
  validationRules
);

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.

Units

Moralis units are available inside your cloud functions. In order to successfully run the units function you always need to specify a method and a value.

To wei

Convert any ether value to wei.

cloud.js
const result = await Moralis.Cloud.units({
  method: "toWei",
  value: 1,
});
return result;

Result: 1000000000000000000

From wei

Convert any wei value to ether.

cloud.js
const result = Moralis.Cloud.units({
  method: "fromWei",
  value: 1000000000000000,
});
return result;

Result: 0.001

To hex

Convert any given value to its hexadecimal representation.

cloud.js
const result = Moralis.Cloud.units({
  method: "toHex",
  value: 100,
});
return result;

Result: 64

Using decimals

Convert any given value using a custom decimals value.

cloud.js
result = Moralis.Cloud.units({
  method: "fromWei",
  value: 10000000000000,
  decimals: 10,
});
return result;

Result: 1000

Web3

Web3 functions are available within Cloud Code including the ability to call contract methods. Moralis uses the Web3.js library.

Example of how to use a custom RPC url

You can get an RPC node and its URL here.

cloud.js
Moralis.Cloud.define("run_contract_function_with_web3", async (request) => {
  web3 = new Moralis.Web3(
    new Moralis.Web3.providers.HttpProvider(
      "RPC URL"
    )
  );
  const abi = [
    {
      constant: true,
      inputs: [],
      name: "name",
      outputs: [{ name: "", type: "string" }],
      payable: false,
      stateMutability: "view",
      type: "function",
    },
  ];
  address = "0x2170Ed0880ac9A755fd29B2688956BD959F933F8";
  const contract = new web3.eth.Contract(abi, address);
  const name = await contract.methods
    .name()
    .call()
    .catch((e) => logger.error(`callName: ${e}${JSON.stringify(e, null, 2)}`));
  return name;
});

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.

const contract = new web3.eth.Contract(abi, address);

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...

cloud.js
const web3 = new Moralis.Web3(
    new Moralis.Web3.providers.HttpProvider(
      "SPEEDY NODE URL"
    )
  );
const abi = Moralis.Web3.abis.erc20;
const address = "0x....";

// create contract instance
const contract = new web3.eth.Contract(abi, address);

// get contract name
const name = await contract.methods
  .name()
  .call()
  .catch(() => "");

For more details on the Web3.js contract interface, see the web3.eth.Contract section of the Web3.js docs.

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.

IPFS

IPFS functionality is available inside your cloud functions. In order to successfully upload to IPFS you need to specify:

  • A source type: sourceType

  • A source: source

Below are the supported values of sourceType

Url

Push the content of a given URL to IPFS.

cloud.js
const result = await Moralis.Cloud.toIpfs({
  sourceType: "url",
  source:
    "https://moralis.io/wp-content/uploads/2021/06/Moralis-Glass-Favicon.svg",
});
return result;

Result: { "path": "https://ipfs.moralis.io:2053/ipfs/QmYrUVhr1f6ZpZ4jrmi7mSv5X8MGjxxrgaERWde3cFASL6" }

String

Push a string to IPFS.

cloud.js
const result = await Moralis.Cloud.toIpfs({
  sourceType: "string",
  source: "Moralis rules <3",
});
return result;

Result: { "path": "https://ipfs.moralis.io:2053/ipfs/QmeYY26fCN4t2Apo9Nqix5ZDhJwcjoyDVLLz85TLkoiqpn" }

Object

Push an object to IPFS.

cloud.
const result = await Moralis.Cloud.toIpfs({
  sourceType: "object",
  source: {
    type: "Monster",
    lp: 100,
    spells: {
      base: ["spell one", "spell two"],
      locked: ["spell three"],
    },
  },
});
return result;

Result: { "path": "https://ipfs.moralis.io:2053/ipfs/QmWowsJ74rYCHUYhag83Puky1qZTSsdj3n7bT2ejE2NvCJ" }

Base64 Binary

Push a base64 file to IPFS.

cloud.js
const result = await Moralis.Cloud.toIpfs({
  sourceType: "base64Binary",
  source:
    "/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoGCBETExcTEhETGBcYFxkaGBkZGRkZGRcZGBcZGBcaGBUaISsjGh0oHxkaJDUkKCwuMjIyGSM3PDcxOysxMi4BCwsLDw4PHRERHTMmISgxMTs0OzsxMzUzLjIzMzM5LjYxOzExMTM7NjEzMS4xOzEzMTEzMTExMTEuMTExMTExO//AABEIAPYAzQMBIgACEQEDEQH/xAAcAAEAAQUBAQAAAAAAAAAAAAAABwIDBAUGCAH/xABQEAABAwIDBAYGBwUDBg8AAAABAAIDBBEFEiEGMUFhBxMiUXGBFDJScpGhCCNCYoKxsjOSosHRU8LhJEOTo7PwFRYXJTQ1VGNkc3TD0tPx/8QAGgEBAAIDAQAAAAAAAAAAAAAAAAQFAQIDBv/EAC0RAAICAgAEBQMDBQAAAAAAAAABAgMEEQUSITEyQVFhcRMikTOBsRQVI8HR/9oADAMBAAIRAxEAPwCZkREAREQBERAEREAWPXVccLHSSyNYxou5ziAGjmSshQztXXOxiqdGJHNoKd+XsmxqJW+sQfZG4Hu3anQDJx3pSqJ3mHB6Zz+HWuaT36tj3NHcXn8K0Muze0FWc1VXFgO9rpXWHhFEMg8rLr8P6qBgjhY1jBua0WHie88ysj03mgOFj6M6yPtRYgwO3i3WN194L4+i2mou1FVyygcGy9aBbuimH5NXd+m809N5oDkME6YqyF/V19M19iA4tBjkb3ksPZJ5WapW2V2qosQYXU0ocR6zHdl7L+0w8OYuOa4LaPC6asZlmYMwHZkGj2eDuI5HRRPWQ1OG1IySOZIw5mSMNszb6EcjaxaeYN0B6yRcX0XbaMxOEh4DaiMASsG5wO6Rg9k8RwPlftEAREQBERAEREAREQBERA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Yk7mgDfcBWXDKZTyI8vl1ZwyZRjW9kMYbhNTUG0FPNLY2PVxufbxLRp5reQ9HWMu3UEn4nMb+pwXpPCMOipomQwMDI2ABrR+ZO8k7yTqSs5e2PPnmUdF2N/wDYv9dB/wDYq/8Aksxu3/RB4ddD/wDNel0QHl2q6PMYjvmoJTb2CyT5McbrQ1+F1EH7anmj9+N7P1AL2AqHsBFiAQeB1HwQHlbYC3psd/vW8cpU5Qv0WyxzZChkzSspYmTtBLHsbkObKQM2Wwd3a3Wkp5btB7x8+K83xyLU4z8tNFtw9pwcfczg5Hu0WOHKrOvPyntE7kKgUlmDbXuSTYAC7nHuaOJVpzzcMY0ue42a0cT3nuA4ldJgmDti+skIdKRq7g37rBwHPeVMwOGzyZbfSPqRsi+Na9zDwzAy+z6kab2xb2jm8/aPLcOa6JjAAAAABuA0A8lWi9jRj10R5YLRT2WSm9yPqIi7mgREQBERAfFh19BDMLSRtdbv3jwcNQiLWSTXUym0+hpqjZf+yne3k4B48jofmViO2equEkJ5kPHyRFAtwMdvwIlRyrV5mRR7MXN5pczfYaMoPi65JHhZdHDC1jQxjQ0AaACwHkiKTjUV1L7Fo5W2Ssf3PZeREUg4hERAEREBSVzeL7PnMXwPa0uN3McDlJ4kEeqfIoij5NULa9TWzpTZKMtpnNVVUYiWvaLj2TcfMBZ+E0stSA5hjY08XXc4eDQAPmiKhqwqHbpxLe22Sr2mdRhGERwXIu553vdq48h3DkFskRejhCMI6itFLOTk9s+r6iLcwEREB//Z",
});
return result;

Result: { "path": "https://ipfs.moralis.io:2053/ipfs/QmaZRSn8cHKUN5LsvuY6M8a5LzV76uFKeZ9khthPj2rHhw" }

Base64

Push a base64 string to IPFS.

cloud.js
const result = await Moralis.Cloud.toIpfs({
  sourceType: "base64",
  source: "TW9yYWxpcyBydWxlcyA8Mw==",
});
return result;

Result: { "path": "https://ipfs.moralis.io:2053/ipfs/QmeYY26fCN4t2Apo9Nqix5ZDhJwcjoyDVLLz85TLkoiqpn" }

Tutorial

Introduction to Cloud functions:

Legacy UI is present in the video, some things might be different (you can no longer write Cloud code in the UI)

Last updated