Skip to main content
All CollectionsPortal Pages
Design | Cloud Code | Scripts (Legacy)
Design | Cloud Code | Scripts (Legacy)
Jason Liang avatar
Written by Jason Liang
Updated over 3 years ago

brainCloud allows developers to write custom Cloud Code routines in JavaScript, that reside and run on the brainCloud servers, allowing execution of code more securely and efficiently than if it were run client-side.

brainCloud's Cloud Code service is implemented using an embedded Mozilla Rhino Javascript engine.

Communications with the brainCloud APIs are facilitated by a bridge object that is available from all scripts. The bridge allows you to retrieve references to service proxies that allow you to make API calls into brainCloud. These service proxies are organized in alignment with brainCloud's API Modules.

Advantages of Cloud Code scripts include:

  • Better performance when calling multiple API calls in a row

  • Lower brainCloud costs (the first 3 API calls are free, and each one after is 1/2 a count)

  • More secure

  • Ability to change logic server-side without a client update

  • Ability to call out to external web services

  • Ability to perform certain operations that are not enabled in the client API

Cloud Code scripts can be called from:

  • client apps – via the Script Service APIs

  • developer-operated servers – via the Server-to-Server API

  • third-party services – via brainCloud’s WebHooks interface

  • triggered via other operations (API Hooks) – scripts can be configured to be triggered automatically as pre- or post- conditions for other API operations

  • scheduled – scripts scheduled to execute in the future

Cloud Code scripts are written using brainCloud’s web-based script editor, located in the Portal under Design | Cloud Code | Scripts.

Set JavaScript level

  • Go to Design | Core App Info | Advanced Settings page, from Cloud Code section, select JavaScript level under the dropdown menu.

Create a new script

  • Go to Design | Cloud Code | Scripts page, click [+] icon at the right top corner to open the script editor.

  • Fill in a name for your script, check script callable option(s), set a script executing timeout from Timeout dropdown menu, add a description for your script and script test parameter accordingly.

Note: There are three types of callable options for script:

  • Client -- allows scripts to be called from client apps through scriptServiceProxy or from portal API Explorer page through script service.

  • S2S -- allows scripts to be called from client-operated servers or from portal S2S Explorer script service.

  • Peer -- This option only shows on the peer service apps, which allows scripts to be called from the peer client apps scriptServiceProxy or from portal API Explorer page through scriptService RunPeerScript() API. For more information about peer service, check out the document of peer service.

  • Switch to Editor tab, write your script code, when finished, click Save.

  • Switch to Run tab, click [Quick Authenticate] button to login as the current portal login user, click Run to test your script. Check your debug Log, Request, and Response on the right side.

Import/Export scripts

  • Click the caret down icon of each script record Actions field on the server script list to manipulate this single script.

  • Click Import/Export at the right top corner of the server script list to import or export all your scripts. (click Export All Scripts, you will get a zip file like Scripts_30070.zip, which you can import to other apps directly by click Import Scripts from other apps )

Note: brainCloud cloud code supports share script now, which means you can include another script in a script by simply using the new bridge.include(“scriptname.ccjs”) operation.

For example - say you have this script:

MathFunctions script

function sumNums( num1, num2 ) {
return (num1 + num2);
}

function prodNums( num1, num2 ) {
return (num1 * num2 );
}

You can include it from another cloud code script very simply:

"use strict"; 
bridge.include("MathFunctions.ccjs");

function main() {
var response = {};
response.answer = sumNums(data.number1, data.number2);
return response;
}
main();

Did this answer your question?