sked function dev

Runs a Connected Function locally for development and debugging.

Overview

Runs a Connected Function locally for development and debugging. The command starts a local server that runs the compiled handler at dist/handler.js in DIRECTORY, so build the function first. The server loads the handler again on each request.

To send a request to the server with your saved access token, use sked http.

The command needs a saved tenant, because the function calls that tenant’s API. If you have not logged in, run sked tenant login web first.

Usage

  $ sked function dev DIRECTORY [--json] [-a <value>] [-f <value>] [-h <value>] [-p <value>]

Examples

  $ sked function dev ./test-directory -p 3000

  $ sked function dev ./test-directory -f .envlocal

  $ sked function dev ./test-directory -p 3000 --alias my-tenant

Flags

Flag Description Required
-a, --alias= The alias of the tenant whose API the function calls (default: your default tenant) No
-f, --envFileName= The .env file to use when running this function (default: .env) No
-h, --host= The interface that the local development server listens on (default: 127.0.0.1) No
-p, --port= The port to run the local development server on (default: random) No

Global flags

Flag Description Required
--json Format output as json. No

Use an environment file

The dev server reads a .env file in DIRECTORY when it starts. Use -f to read a different file in that directory. Write one NAME=value on each line. The file is for local development only. The platform does not use it when you deploy the function. If the file is missing, for example because of a typo after -f, the server starts without it and prints no error. The command fails only when sked.proj.json declares a variable without a default.

The server sends each variable in the file to your function as a sked-setting-<name> header, with the name in lower case.

Configuration variables

Use the file to give a configuration variable a value on your machine. Your function reads the value with skedContext?.configVars?.getVariableValue("NAME"). That call returns a value only for variables that sked.proj.json declares. The server still sends any other variable in the file as a sked-setting-<name> header.

  • If sked.proj.json declares a variable without a default, the file must set that variable. The command fails if the file does not set it.
  • If sked.proj.json gives a variable a default, the server uses the default and ignores the value in the file. An empty default ("default": "") counts as no default, so the file must set that variable.

Access token

Do not set AUTH_TOKEN in the environment file. Send requests with sked http instead, so your function receives your access token without a copy in a file. This needs CLI version 5.1.4 or later.

Setting AUTH_TOKEN was the older way to give a local function a token, from before sked http could call a local function. sked http replaces it. It attaches your saved access token to each request. It puts no token in your function directory. After you log in again, it uses the new token. For the steps, see Test a function locally.

If your environment file sets AUTH_TOKEN, remove that line and restart the dev server. While AUTH_TOKEN is set, any program on your machine that can reach the port runs your function with that token. A tunnel tool that forwards to the port has the same effect. When AUTH_TOKEN is set and --host is not a loopback address, the command prints a warning at startup. The server also sends the value as the sked-setting-auth_token header, so do not log the request headers in your function.

The server passes the Authorization header of each request to your function, making sked http a more practical choice for testing.

Requests from web pages

The server rejects a request that comes from a web page and answers 403. It sends no CORS headers. Call the function with a tool such as curl or sked http.

If a browser app must call the function, send the request through a server-side proxy that removes the Origin header. Many dev-server proxies forward Origin, and the server rejects every request that has it. This applies from CLI version 5.1.4.