Webhooks

Define custom webhooks for your Moralis Server

Webhooks allow you to write your server-side logic in your own environment with any tools you wish to use. This can be useful if you want to use a language other than JavaScript, host it yourself for improved testing capabilities, or if you require a specialized library or technology not available in the Cloud Functions. Webhooks are currently available for beforeSave, afterSave, beforeDelete, afterDelete, and Cloud functions. To specify a new webhook, you can use the Moralis Dashboard in the Webhooks section located under Core.

Note: At the current time, custom webhooks cannot be set for the special classes _User and _Installation.

Cloud Function Webhooks

A webhook request for a Cloud function will contain the following parameters:

  • master: True if the master key was used and false otherwise.

  • user: If set, this will contain the Moralis User who made the request, in our REST API format. This is not set if the master key is used.

  • installationId: If available, the installationId which made the request.

  • params: A JSON object containing the parameters passed to the function. For example: { "foo": "bar" }

  • functionName: The name of the Cloud function.

To respond to this request, send a JSON object with the key error or success set. In the case of success, send back any data your client will expect; or simply true if your client doesn’t require any data. In the case of error, the value provided should be the error message you want to return.

To create a webhook for a Cloud function, start by writing the function’s code on your own server.

You can activate a webhook from the Dashboard UI.

Once the webhook is set, you can call it from the Moralis SDK, the same way you would a normal Cloud function.

Webhooks are great when you want to use a specialized technology not available using Motalis Cloud Functions.

beforeConsume Webhooks

beforeConsume Webhooks are required when adding events that contain a large amount of data for example the Transfer(address,address,uint256) event, adding a new Sync and Watch Contract of this event without an address will synchronize millions of events per hour, making the instance unresponsive.

An example on how to use this Webhook would be to listen to all transfers event but only save to the DB if specific filters are fullfilled, like filtering than the value of the transfer is greater than X

You need to define the beforeConsume and set the tableName used on plugin Creation.

Moralis.Cloud.beforeConsume('TransfersEvent', function (object) {
if(object.value > 100000000000){
return true;
}
return false;
})

beforeSave Webhooks

For triggers, the following parameters are sent to your webhook.

  • master: True if the master key was used and false otherwise.

  • user: If set, this will contain the Moralis User who made the request, in our REST API format.

  • installationId: If available, the installationId which made the request.

  • object: For triggers, this will contain the Moralis Object, in our REST API format. For example: { "className": "TestObject", "foo": "bar" }.

  • triggerName: “beforeSave”

To respond to a beforeSave request, send a JSON object with the key error or success set. This is the same as for Cloud functions, but there’s an extra capability with beforeSave triggers. By returning an error, you will cancel the save request and the object will not be stored in Moralis. You can also return a JSON object in this following format to override the values that will be saved for the object:

📋{
"className": "AwesomeClass",
"existingColumn": "sneakyChange",
"newColumn": "sneakyAddition"
}

afterSave Webhooks

Like we’ve seen in normal Cloud Functions, it’s also possible to run some code after an object has been saved using a webhook. The parameters sent to your webhook are the same as for beforeSave triggers but we’ll repeat them here for clarity.

  • master: True if the master key was used and false otherwise.

  • user: If set, this will contain the Moralis User who made the request, in our REST API format.

  • installationId: If available, the installationId which made the request.

  • object: For triggers, this will contain the Moralis Object, in our REST API format. For example: { "className": "TestObject", "foo": "bar" }.

  • triggerName: “afterSave”

No response is required for afterSave triggers.

beforeDelete Webhooks

You also use webhooks for beforeDelete triggers. The parameters sent to your webhook are the same as for beforeSave and afterSave triggers but we’ll repeat them here for clarity.

  • master: True if the master key was used and false otherwise.

  • user: If set, this will contain the Moralis User who made the request, in our REST API format.

  • installationId: If available, the installationId which made the request.

  • object: For triggers, this will contain the Moralis Object, in our REST API format. For example: { "className": "TestObject", "foo": "bar" }.

  • triggerName: “beforeDelete”

Just like for Cloud functions, to respond to a beforeDelete request, send a JSON object with the key error or success set. Returning an error will cancel the delete and the object will remain in your database.

afterDelete Webhooks

The afterDelete trigger is also accessible via webhooks. The parameters sent to your webhook are the same as for other triggers but we’ll repeat them here for clarity.

  • master: True if the master key was used and false otherwise.

  • user: If set, this will contain the Moralis User who made the request, in our REST API format.

  • installationId: If available, the installationId which made the request.

  • object: For triggers, this will contain the Moralis Object, in our REST API format. For example: { "className": "TestObject", "foo": "bar" }.

  • triggerName: “afterDelete”

No response is required for afterDelete triggers.