Navigation

Call a Function

On this page

  • Overview
  • Call from A Function, Trigger, or HTTP Endpoint
  • Call from a JSON Expression
  • Call from Realm CLI
  • Call from a Client Application

You can call a function from other parts of your Realm app, from a connected client application, or with Realm CLI.

The examples in this section demonstrate calling a simple function named sum that takes two arguments, adds them, and returns the result:

// sum: adds two numbers
exports = function(a, b) {
return a + b;
};

You can call a function from another function, including incoming webhooks and triggers, by accessing function context with the context.functions global variable:

For additional information, see the Function Context reference page.

// difference: subtracts b from a using the sum function
exports = function(a, b) {
return context.functions.execute("sum", a, -1 * b);
};

You can call a function from a Realm JSON Expression, including service rules, by using the %function operator:

{
"numGamesPlayed": {
"%function": {
"name": "sum",
"arguments": [
"%%root.numWins",
"%%root.numLosses"
]
}
}
}

To call a function with Realm CLI, run realm-cli function run and specify the function name and any arguments. The command returns the function result as EJSON as well as any log or error messages.

realm-cli function run \
--function=sum \
--args=1 --args=2

You can call a function from client applications that are connected with a Client SDK or over the wire protocol.

Give Feedback

On this page

  • Overview
  • Call from A Function, Trigger, or HTTP Endpoint
  • Call from a JSON Expression
  • Call from Realm CLI
  • Call from a Client Application