Custom Cloud Code

This guide will teach you how to set up your own Cloud Code.

Overview

This guide will teach you how to set up your own Cloud Code.

Custom Cloud Code replaces the old Moralis-hosted Cloud Functions:

Configuration

By default the cloud code is located in src/cloud/main.ts.

Create a new file inside src/cloud/ folder and name it cloud.ts.

Now add the following to src/cloud/main.ts to import the file:

//import cloud.ts file
import "./cloud";

Adding custom cloud code

Add some cloud functions inside your newly created file src/cloud/cloud.ts:

declare const Parse: any;

Parse.Cloud.define("Hello", () => {
  return `Hello! Cloud functions are cool!`;
});

Parse.Cloud.define("SayMyName", (request: any) => {
  return `Hello ${request.params.name}! Cloud functions are cool!`;
});

Build and run

Build the project:

npm run build

And then run it locally:

npm run start

Accessing cloud code from client

Now on a Moralis v1 client, you can call your custom cloud functions using the Moralis v1 syntax:

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

const response = await Moralis.Cloud.run("Hello");
console.log(response);
const { fetch, data, error } = useMoralisCloudFunction("Hello");

<button onClick={() => fetch()}>Fetch<button>

If you don't know how to set up a Moralis v1 client, head over to Client connection.

You can also watch the following video tutorial which goes through the whole setup.

Video tutorial

Last updated