Develop a function

How to use the Skedulo CLI to develop custom functions for extensions, automations, and integrations.

Overview

The Skedulo CLI has built-in support for generating and locally debugging a function. For information about the CLI, please see the CLI documentation.

For information on the CLI commands for working with functions, please see the sked function dev and sked function generate command reference documentation.

Prerequisites

  • The Skedulo CLI is installed
  • Your environment is set up
    • NVM: the correct version of Node.js to be used in the development. This ideally match runtimeVersion property in the sked.proj.json
    • Yarn v1.22 or later
  • You are logged into a tenant (team)

Create a new function

To create a new function, do the following steps:

  1. Open a new terminal and navigate to a directory where you want your source code to reside.

  2. Run the following command:

    sked function generate --name my-first-function --outputdir .
    

Source code

The generate command creates a new directory with the name of the function. If we open this directory in our preferred IDE (VS Code), we can see the folders and files that are generated.

To open the source code in VS Code, do the following steps:

  • In your terminal window, run the command: code ./my-first-function

Generated function folders in VS Code

Most of what you see are common to Node.js rest API applications written in TypeScript. There are a couple of files specific to Skedulo:

Filename Description
state.json This file contains the metadata that represents the function to the CLI artifact and packaging commands.
sked.proj.json This file contains the specific configuration of the function, including its runtime and any settings.

Change the description

The default description is not very useful, but can be updated to better reflect what the function is for.

To update the function’s description, do the following steps:

  1. In VS Code, open the sked.proj.json file.
  2. Change the description property to your preferred value.
  3. Save the file.

Your sked.proj.json should look something like this:

{
  "type": "function",
  "version": "2",
  "name": "my-first-function",
  "description": "Description of the function",
  "runtime": "nodejs18.x",
  "settings": {}
}

Compile the function

The function template includes a couple of custom scripts. Before you try to run the code, do the following:

  1. In the terminal, change directory using: cd ./my-first-function

  2. Install dependencies by running: yarn bootstrap

  3. Compile the code by running: yarn compile

Run the function

To test the function locally, you can use the built-in development server by doing the following steps:

  1. Open a new terminal window to run our local development server.

  2. Change directory to the location of your function.

  3. Start the development server by running:

    sked function dev . -p 3000
    

The sked function dev command takes two parameters: the directory of the function to run; and the port on which to host the server.

Test the function

To test the function do the following:

  • In the original terminal window, use curl (or a similar tool) to make the following request:
    curl --location 'http://127.0.0.1:3000/ping' --header 'Authorization: Bearer <Your Tenant API Token>'
    
  • You should get the following result from the template: {"result":"pong","apiServer":"https://api.skedulo.com"}

Make changes to the function

The generated function template doesn’t do very much, so you’ll want to modify it to meet your needs.

To modify your function, do the following steps:

  1. In VS Code, make your changes to the source code as required.

  2. Compile your code using: yarn compile. Your change will automatically be reflected in the local development server.

  3. Test the function to check that it still works.

Deploy the function to your tenant

Now that you’ve got a working function, the next thing you’ll want to do is to deploy it to your tenant. There are two ways to do this, as described in the following sections.

Upsert the function using the artifact command

You can use the artifact commands to manipulate individual artifacts. It is a finer level of control compared to the package deploy command.

To upsert your function to your tenant, run this command in your terminal:

sked artifacts function upsert -f state.json

Deploy the function using the package deploy command

If you’re developing a solution with multiple components, you may want to use the package deploy command. This can be used to deploy any artifacts within a directory.

To deploy a package to your tenant, run this command in your terminal:

sked package deploy -p .

Test the deployed function

To make sure your function is working as expected, do the following steps:

  1. Get the base URL of the function you deployed by doing the following:
    • Execute the list command:
      sked artifacts function list
      
    • Locate your function in the list.
    • Copy the baseUrl column.
  2. Use curl to test your function by running:
    curl --location '<Your baseUrl>/ping'  --header 'Authorization: Bearer <Your Tenant API Token>'