Call a Function


You can call a function from other parts of your Stitch application or from a connected client application.


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;

Call from Another Function

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);

Call from a JSON Expression

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

  "numGamesPlayed": {
    "%function": {
      "name": "sum",
      "arguments": [

Call from a Client Application

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

To execute a function from a JavaScript Client SDK, use the callFunction() method.

const client = Stitch.defaultAppClient;
client.callFunction("sum", [3, 4]).then(result => {
    console.log(result) // Output: 7

To execute a function from the Android Client SDK, use the callFunction() method.

StitchAppClient client = Stitch.getDefaultAppClient()
client.callFunction("sum", Arrays.asList(3, 4), BsonValue.class)
      .addOnCompleteListener(new OnCompleteListener<BsonValue>() {
        public void onComplete(@NonNull final Task<BsonValue> task) {
          if (task.isSuccessful()) {
            Log.d("stitch", String.format("%s", task.getResult())); // Output: 7
          } else {
            Log.e("stitch", "Error calling function:", task.getException());

To execute a function from the iOS Client SDK, use the callFunction(withName:withArgs:_:) method.

let client = Stitch.defaultAppClient!
client.callFunction(withName: "sum", withArgs: [3, 4]) { (result: StitchResult<String>) in
    switch result {
    case .success(let sum):
    case .failure(let error):
        print("Failed to calculate sum: \(String(describing: error))")