Navigation

Functions

Introduction

Stitch Functions allow you to define and execute server-side logic for your application. You can call functions from your client applications as well as from other functions and in JSON expressions throughout Stitch.

Functions are written in modern JavaScript (ES6+) and execute in a serverless manner. When you call a function, you can dynamically access components of the current application as well as information about the request to execute the function and the logged in user that sent the request.

By default, Stitch Functions have no Node.js modules available for import. If you would like to make use of any such modules, you can upload external dependencies to make them available to import into your Stitch Functions.

Use Cases

You can use functions to handle low-latency, short-running connection logic and other server-side interactions. Functions are particularly useful when you want to work with multiple services, behave dynamically based on the current user, or abstract away implementation details from your client applications. You can upload external Node.js packages and import them into your functions to streamline data processing and reuse shared code.

Stitch also uses functions internally for incoming webhooks and triggers. The functions that you create for these components are regular Stitch functions that take specific arguments depending on the service.

Behavior

  • Stitch automatically encodes values returned from functions as Extended JSON.
  • Functions do not continue to execute after they return. You cannot use functions with asynchronous callbacks or event listeners. If you need to do asynchronous work in a function, use a promise.

Constraints

  • Function runtime is limited to 60 seconds.
  • Function memory usage is limited to 256MB.
  • Functions and uploaded npm modules currently do not support the following ES6+ features:
    • New Global Object Types (i.e. Map, WeakMap, Set, WeakSet, Symbol, Proxy)
    • New Math, Number, String, Array, and Object APIs (e.g. Array.prototype.fill, Array.prototype.includes, Object.assign)
  • Uploaded npm modules should be transpiled to ES5. Since transpiling to ES5 using Babel is commonplace in the Node.js community, most modules should be available in a transpiled format.
  • Functions and uploaded npm modules do not support any Node.js built-in modules.

Concepts

Active User

The active user of a Function is the user account for which Stitch executes the Function. This user is also the active user for any other Functions or service actions called from within the Function. By default, the active user is the user that called the Function.

You can dynamically reference the active user of a Function in the Function’s source code with context.user as well as in service action rules with the %%user expansion. Stitch resolves these references to the active user’s user object.

System Functions

A System Function is a Function where the active user is a system user instead of an application user. System functions have full access to MongoDB CRUD and Aggregation APIs and are not affected by any rules, roles, or permissions.

You can configure a Function to run as a system user by enabling its Run As System configuration option. You can determine if a Function is executing as a system user at runtime by calling the context.runningAsSystem() method.

External Dependencies

An external dependency is an external library that includes logic you’d rather not implement yourself, such as string parsing, convenience functions for array manipulations, and data structure or algorithm implementations.

Stitch allows you to upload external dependencies from the npm repository and then import those dependencies into your functions using standard JavaScript module syntax. To get started with external dependencies, check out the following guides:

Create Your Own Modules

Though most npm modules are written by third parties, you can also create and publish your own npm modules to house logic specific to your application. You can make your modules available to the Node.js community or reserve them for private use. For more information, check out npm’s guide on Contributing packages to the registry.

Guides

Guide Description
Define a Function Learn how to write and add new functions to your Stitch application.
Upload an External Dependency Learn how to make Node.js packages available for import in your Stitch functions.
Call a Function Learn how to execute functions that you’ve defined from a client application or from another Stitch component.
Import an External Dependency Learn how to import and call uploaded Node.js packages within a Stitch function.
Access Function Context Learn how to access runtime information and interfaces to other components of your application from within a function.

Reference Documentation

Subject Description
Function Context Includes detailed information about specific application context modules as well as examples of runtime data objects.
Utility Packages Includes detailed information about global utility packages.