Deploy Stedi Functions with the CLIOctober 16, 2022

Get the files: See the matching GitHub repository to get all the files for this tutorial

In this tutorial we will show you how to deploy code to Stedi Functions from the command line. Here’s what we’ll cover.

  • Deploying your JavaScript code using the Stedi CLI.
  • Calling your uploaded function using the Stedi CLI.
  • Storing your Stedi API key safely.
  • Including third-party libraries.
  • Testing your code locally.

The example code for this tutorial will make a call to an external web API. That’s not what this tutorial is about, so we won’t explain it in depth. Still, if you want to know how to do it, you’ll at least have an example. There are some other things we won’t cover here.


Here’s what you need.

In addition, you need to have the following tools available on your machine:

Step 1: Clone the repository and navigate to the project directory

git clone
cd deploy-functions-with-the-cli

The directory has the following contents.

index.jsThe JavaScript code we will upload to Stedi Functions.
test.jsA script that allows you to test the function locally.
event.jsonData we can use to the test the function.
package.jsonMetadata about this project as an npm package.
readme.mdThe instructions you’re currently reading.

Step 2: Run the function locally

The code in index.js calls the Wikipedia API and retrieves an article. In order to do that, it needs the Axios library. Axios is already listed in package.json, so you can install it with:

npm ci

The input to the function contains the topic you wish to retrieve from Wikipedia. event.json contains an example. You can test the function locally by running test.js, which in turn calls handler() in index.js.

node test.js

The output should be a Wikipedia article about EDI and starts something like this:

Electronic data interchange (EDI) is the concept of businesses electronically communicating information that was traditionally communicated on paper, such as purchase orders and invoices. Technical standards for EDI exist to facilitate parties transacting such instruments without having to make special arrangements.

Step 3: Install the Stedi CLI

npm install @stedi/cli --save-dev

If all goes well, the following command should output usage information and the Stedi CLI version.

npx stedi help

Step 4: Create and configure an API key

You need to specify your API key when calling Stedi Functions using the CLI. You do this by setting the environment variable STEDI_API_KEY.

export STEDI_API_KEY=<your API key here>

You can test whether you successfully set your API key by running the following command:

 npx stedi functions list-functions

Assuming you haven’t created any functions yet, the response should be:

  "items": []

Step 5: Build, package, and deploy

npx esbuild index.js --bundle --minify --platform=node --outfile=build/index.js

This takes the code from index.js and all its dependencies and writes them to a single file. This also works if your project has multiple source files; they will be bundled automatically. It will only include files index.js depends on, so test.js will not be part of it.

cd build
zip package index.js
cd ..

The ZIP file you upload to Stedi Functions must have index.js at the root. That’s why you must run zip from within the build directory.

npx stedi functions create-function --function-name wikipedia --package build/

This creates a new function with the name wikipedia. Once the function exists, you can update the code as follows:

npx stedi functions update-function --function-name wikipedia --package build/

Step 6: Invoke the Function

npx stedi functions invoke-function --function-name wikipedia --payload file://event.json

The contents of events.json is sent to the function as its event parameter. The result of this command should be the same as when you ran the function locally: a Wikipedia article on EDI.