Skip to content

Your first function

A function is a small piece of first-party code that the platform runs for you: an HTTP endpoint, an event subscriber or a scheduled task, without a service of your own to deploy and operate. Functions are versioned, promoted through aliases, and isolated in a sandbox (WebAssembly for Go and Rust, a JavaScript engine for TypeScript, the JVM for Java). Functions explains the model.

In this tutorial you build a hello function for the orders application, run it on your machine, deploy it to your local fcdev, and call it.

  • fcdev running (install it). It runs a function runner in-process, with a private entry on 127.0.0.1:8095.
  • The orders application (your first event created it), because a function’s address is <application>.<name>: this one will be orders.hello.
  • The toolchain for your language:

Node.js with npm. The build is esbuild, installed by the project.

fcdev fn init <dir> --lang go|rust|ts --app <application> writes a starter project. The directory name is the function name (lower-case letters, digits and hyphens).

Terminal window
fcdev fn init hello --lang ts --app orders --sdk-path ~/src/flowcatalyst/clients
cd hello && npm install

The generated function has two endpoints and reads one configuration value:

src/index.ts
import { fn } from "@flowcatalyst/fn";
const greeting = fn.config("GREETING");
fn.open("GET /healthz", () => fn.text(200, "ok"));
fn.platform("GET /hello/{name}", (req) => fn.text(200, `${greeting.get() ?? "Hello"}, ${req.params.name}`));

Every endpoint names how callers authenticate, and there is no default:

Mode Who calls Checked by the runner before your code runs
open (none in a describe document) anyone nothing: health checks, or third-party webhooks you verify yourself
platform people and services with a platform access token a valid token; your code then checks the caller’s permissions
webhook the platform’s subscriptions and schedules (POST only) the delivery signature

fcdev fn build (run in the project directory) builds with the language’s own toolchain and then reads the function’s describe document exactly as publishing will, so what builds here publishes.

Terminal window
fcdev fn dev --config GREETING=Hi

fcdev fn dev builds the project, serves it on the real function runner at 127.0.0.1:8099 with no platform behind it, and rebuilds and reloads on every save (a failed build keeps the last good one serving). It prints a curl line per endpoint. The function is served at the address local.hello:

Terminal window
curl http://127.0.0.1:8099/fn/local.hello/healthz
curl -H "Authorization: Bearer dev" http://127.0.0.1:8099/fn/local.hello/hello/Ada
# Hi, Ada

Locally any bearer token is accepted on a platform endpoint. Add --send sample.json --path /x to send a request after every reload; see develop locally with fcdev fn dev.

Stop fn dev (q and Enter), then publish the function to the fcdev you are running. The fcdev fn commands use the local credential fcdev start wrote for them.

Terminal window
fcdev fn deploy orders.hello . --create

deploy builds (for a directory), publishes a new version (version 1), waits until a runner has loaded it, and promotes it to the live alias. Here it stops short of the last step: the version declares the GREETING config key, and promotion is refused while a declared config key, secret or database has no value. Set it and promote:

Terminal window
fcdev fn set orders.hello --config GREETING=Howdy
fcdev fn promote orders.hello 1
fcdev fn status orders.hello

Publishing changes nothing that is running; promoting is the moment a version takes effect (and the moment its subscriptions and schedules are wired).

Terminal window
fcdev fn invoke orders.hello hello/Ada
# Howdy, Ada
curl http://127.0.0.1:8095/fn/orders.hello/healthz

fcdev fn invoke calls through the runner with a platform token (--anonymous sends none); it reaches live unless you name another alias or version (orders.hello@v1). For a JVM function, pass the ingestor’s runner: --runner http://127.0.0.1:8091.

The console’s Functions page now lists orders.hello with its version, alias and endpoints.