All Collections
Portal-X Pages
Design | Cloud Code | WebHooks
Design | Cloud Code | WebHooks

WebHooks is used to invoke a cloud code script from the external servers

Jason Liang avatar
Written by Jason Liang
Updated over a week ago

WebHooks are used to trigger a cloud code script from the external servers.

An excellent example of utilizing WebHook is the Forgot Password functionality.

To get started, create a cloud code script that you want the webhook URL to trigger, for the example of the Forgot Password functionality, you can create a script as follows.

"use strict";


function main() {
var response = {};

bridge.logDebugJson("Script inputs", data);

var userEmail = data.parameters.email;

response.stringResponse = "webhook is processing user email reseting..." + data.parameters.email;

// validating the user email
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(userEmail)) {

// checking if the email is existed in brainCloud users
var context = {
"pagination": {
"rowsPerPage": 20,
"pageNumber": 1
},
"searchCriteria": {
"emailAddress": userEmail
},
"sortCriteria": {
"playerName": 1
}
};

var userProxy = bridge.getUserServiceProxy();

var postResult = userProxy.sysGetPage(context);
if (postResult.data.results.count > 0) {
var userProfileID = postResult.data.results.items[0].profileId;

// sending password reset email
postResult = userProxy.sysSendPasswordResetEmail(userProfileID);
if (postResult.status == 200) {
response.stringResponse = "password reset email has been sent to user's email:" + userEmail;
} else {
response.stringResponse = "system error with sending email...";
}
} else {
response.stringResponse = "user doesn't exist with the email you provided";
}

} else {
response.stringResponse = "user's email is invalid.";
}

return response;
}

main();

Then, create a new webhook and link the created script to the Cloud Code Script field, a webhook URL will be generated and ready for use.

  • Note that When the WebHook has been configured to Enforce Secret in Header, a header named "x-bc-secret" with the value of the generated Endpoint Secret here must be included in the HTTP call from the caller's request.

  • For security reasons, you can optionally whitelist the IP Ranges which can make this request.

Now, that you have set up the WebHook from brainCloud, you can test it from your app or other request-sending tools, if you are using Unity, you can define the WebHook URL as a variable, and link it to your forgotEmail object (could be a link or button in your app) onClick event, making it trigger this URL when the user clicks with Application.OpenURL command. Grab the user's email and append it as an email parameter to the URL as follows.

const string passwordResetWebhookUrl = "https://api.braincloudservers.com/webhook/12832/forgotPassword/6ac675bb-f8b4-495f-ac6e-1f0658bfe09c";
Application.OpenURL(passwordResetWebhookUrl + "?email=" + userEmail);

Run your app to test, you should get a similar pop-up window as follows once a user clicks the forgotEmail object,

The user is expected to receive the reset password email at the email address they have provided.

To learn more about writing a WebHook script, refer to our doc site here.

Did this answer your question?