Navigation

Define a Function

On this page

Overview

You can create and configure functions in your application at any time from the Stitch UI or by importing a function configuration file.

Procedure

1

Create and Configure a New Function

To define a new server-side function from the Stitch UI:

  1. Click Functions in the left-hand navigation.

  2. Click New Function in the top right of the Functions page.

  3. On the function Settings screen, enter configuration values for the function:

    Configuration Value Description
    Function Name Required. The name of the function.
    Private If enabled, the function is hidden from client applications. You can still call a private function from JSON expressions and other functions, including incoming webhooks and triggers.
    Can Evaluate Optional. A JSON expression that determines if a function should actually execute after being called. Stitch will only execute the function code if the expression resolves to true. If you do not specify an expression, Can Evaluate defaults to true.
2

Write the Function Code

Once you’ve created and configured the new function, it’s time to write the actual javascript code that runs when you call the function. You can write the code directly in the Stitch UI using the function editor.

Note

You can use most modern (ES6+) JavaScript features in functions, including async/await, destructuring, and template literals.

From the function’s Settings page:

  1. Click the Function Editor tab.

  2. Add javascript code to the function. At minimum, the code must assign a function to the global variable exports, as in the following example:

    exports = function() {
      return "Hello, world!"
    }
    
3

Save the Function

Once you’ve written the function code, click Save from either the Function Editor or Settings tab.

After you save the function, you can begin using it immediately. For more information on using the function, see Call a Function.

1

Create and Configure a New Function

To define a new server-side function using the command line interface:

  1. Export an application configuration directory for your Stitch app.

  2. Create a new subdirectory in the /functions folder of the exported directory with the same name as the function.

    mkdir -p functions/myFunction
    
  3. Add a configuration file named config.json to the new function directory. Function configuration files have the following form:

    {
      "id": <string>,
      "name": <string>,
      "private": <boolean>,
      "can_evaluate": <JSON expression>
    }
    
    Configuration Value Description

    Function Name

    name
    Required. The name of the function.

    Private

    private
    If enabled, the function is hidden from client applications. You can still call a private function from JSON expressions and other functions, including incoming webhooks and triggers.

    Can Evaluate

    can_evaluate
    Optional. A JSON expression that determines if a function should actually execute after being called. Stitch will only execute the function code if the expression resolves to true. If you do not specify an expression, Can Evaluate defaults to true.
2

Write the Function Code

Once you’ve created and configured the new function, it’s time to write the actual javascript code that runs when you call the function.

Add a file named source.js that contains the javascript code for the function to the new function directory.

Note

You can use most modern (ES6+) JavaScript features in functions, including async/await, destructuring, and template literals.

3

Import the Function

Once you’ve configured the function and written the source code, all that’s left is to import the function.

  1. Ensure that config.json and source.js are saved.

  2. Navigate to the root of the exported application directory.

  3. Log in to MongoDB Atlas with stitch-cli:

    stitch-cli login --username="<MongoDB Cloud Username>" --api-key="<MongoDB Cloud API Key>"
    
  4. Import the directory:

    stitch-cli import
    

Now that you have imported the function, you can begin using it immediately.

For more information on using the function, see Call a Function.

←   Functions Call a Function  →