Create and deploy functions
Please contact us if you need access to this feature.
You can create and deploy functions using Stedi CLI. We also have a range of pre-made templates available on GitHub.
Stedi CLI
Install the CLI
Install the latest version of the Stedi CLI. Requires Node.js version 18.16.0 or later.
If you’ve installed the CLI previously, be sure to update it to the latest version using the same command.
Init a project
Initialize and name a project.
After initialization, change directories to the newly created directory and install dependencies.
Create a function
Create your first function. The new-function
command generates a basic Stedi Function placeholder, along with a test to help you get started. The --guided
flag prompts you to select which Stedi event you want your function to consume.
Verify the setup by running the test for your newly created function. Out of the box, the test is expected to fail. Generated functions use the AVA test runner for Node.js.
Deploy
Use an existing Stedi API key or create a new one, and update the provided .env
file in your project root directory.
The CLI requires this key to deploy your functions. You can reuse an existing API key; you don’t need a separate key for Functions.
Deploy the function to your Stedi account.
You can view your new function in the Functions UI and run test invocations. To redeploy changes, run the npm run deploy
command again.
We also recommend checking your code into source control.
Function handler body
The following example shows the default handler.ts
.
The event
parameter contains the request JSON payload that is passed in during the function invocation. Functions can accept any data shape. When a function is bound to handle events from other Stedi products, the events have a predefined schema that tells you what fields you can expect.
The provided typescript types (e.g. BucketsFileCreatedEvent
) describe what shape is expected for each event type when using a function with an event-binding. You don’t need to use TypeScript to use Stedi Functions, but we recommend it.
The handler
can also accept a second parameter called context
, which contains information about the execution environment where the function is running. It’s not usually used, so it’s not included in the default template.
Using third-party libraries
You can import libraries from npm
to use within your functions. The following example code requires the Marked library to translate Markdown to HTML. Install the Marked library.
Import the Marked library into your template.
Your project comes with esbuild preinstalled and configured to create a single file that includes all of the dependencies (bundling).
When you run npm run test
or npm run deploy
, the necessary library code from the local node_modules
will be included in the output file markdown/index.js
.