StediDOCS
GuidesAPI Reference
This product is in Developer Preview

Functions

Getting Started

Functions are Stedi’s tool for creating and running custom business logic at scale, offering developers a serverless compute environment for running code in response to HTTP events. With Functions, anyone with basic knowledge of JavaScript can write custom business logic in a simple browser interface, deploy it in seconds, and scale massively according to demand – all without having to manage the complexity of a typical development experience.

Using Terminal

Functions can be created, updated and tested within Terminal.

Authoring functions is enabled by a web-based code editor that supports JavaScript and TypeScript, syntax highlighting and intelligent code completion.

Creating a function

To create a function, click on the Functions tab in the navigation menu. Then, click on the Create function button located in the top right corner of the page.

Creating Function

You will be prompted to choose a name for your function. The function name should be unique within the context of a given account.

To finish creating your function, click the Create button.

Creating Function

Authoring a function

You can use the following general syntax when creating a function

module.exports = (input: unknown, context: Context) => {
  return "hello world";
};
  • input contains the value that was specified as the Function input
  • context is an object containing the account-specific data:
    • accountId - current account ID

Moreover, the following variables are available in context object, if a function is invoked by a workflow:

  • executionId
  • workflowId
  • workflowVersion
  • workflowName

Each function must adhere to the following rules:

  • cannot perform asynchronous operations (including network requests and filesystem operations)
  • cannot disable eslint using /* eslint-disable */ directive
  • must contain valid JavaScript or TypeScript code
  • must export a single function, higher-order function, or variable

An example of a correctly defined Stedi function in TypeScript:

type MyInputType = {};

module.exports = (input: MyInputType) => {
  return Math.random();
};

An example of a correctly defined Stedi function in JavaScript:

module.exports = (input) => {
  return Math.random();
};

Dependencies

Not all built-in modules are available within the Function. Current list of available modules is:

External modules available within the Function runtime (without bundling) are:

  • lodash - A modern JavaScript utility library, version 4.17.21
  • uuid - - A unique ID generator library, version 8.3.2

Testing a function

To test a function, use the Test button located in the top right corner of the Function editor page.

Function Test Success

On this view you can also define test events to simulate real world events without impacting currently deployed production code.

Function Define Test Event 2nd part

The test results will be displayed in a dropdown located above the Function editor.

Function Test Success

Deploying a function using Formation

Formation is Stedi's infrastructure as code product for automated provisioning and management of platform configuration. With Formation you can automate the deployment of Functions.

Minimal example of a Formation file deploying a single Function with code located at myStediFunction.js:

Resources:
  MyStediFunction:
    Type: "Function"
    Properties:
      Name: "MyStediFunction"
      Code: !ReadFile ./myStediFunction.js

To deploy functions which are using 3rd party dependencies, please use !Compile helper function instead of !ReadFile:

Resources:
  MyStediFunctionWithDependencies:
    Type: "Function"
    Properties:
      Name: "MyStediFunctionWithDependencies"
      Code: !Compile ./heavyFunction.ts

Formation will automatically take care of inlining the dependencies and compiling the code.

Using the API

The Functions API is a JSON REST API. Please consult Functions HTTP Reference for the endpoints that are available.

Example

The following function extracts the first transaction from the first transaction group from the 204 JEDI load tender input.

import { get } from "lodash";

module.exports = (input: string, context: Context) => {
  const { accountId } = context;

  const jedi = JSON.parse(input);
  const input204 = jedi.functionalGroups[0].transactions[0].payload;

  return { loop: get(input204, ["detail", "loop-0300"]), accountId };
};

Example test input:

{
  "functionalGroups": [
    {
      "transactions": [
        {
          "payload": {
            "detail": {
              "loop-0300": "loop-content"
            }
          }
        }
      ]
    }
  ]
}

Working with big files

To be able to invoke or test a function with a payload larger than 5MB, use the input_url parameter. The input_url parameter must point to a downloadable resource. The document cannot exceed 16MB.

Similarly, if the output of the function exceeds 5MB, use output_url. After the execution, the function will automatically issue a PUT request against provided URL uploading the invocation result.

Caveats

  • Functions are meant to have no side-effects and should not modify the input or output parameters. Calling other Stedi services, other functions, or making network requests is not allowed. If you need to make an HTTP request, wrap a combination of Function (which prepares a payload), and HTTP Action in a Workflow.
  • Functions have a maximum execution time of 5 seconds
  • Functions have a limit of 256 KB of code
  • Functions have a limit of 5 MB of input data
  • Functions must have a unique name within an account
  • Functions are using Node 14 as a runtime

API Reference

Getting StartedUsing TerminalCreating a functionAuthoring a functionDependenciesTesting a functionDeploying a function using FormationUsing the APIExampleWorking with big filesCaveats