Skip to content

Write a function

This guide takes you from an empty directory to a function that has a health check, a platform-authenticated endpoint and a webhook endpoint that reacts to an event. Read Functions first if you want the model (addresses, versions, aliases and the trust model); this page is the practical part.

A function is first-party code: you write it as an operator of the platform, for your own customers’ benefit. The sandbox it runs in isolates faults, not hostile authors.

Language Artifact Runs on Pick it when
TypeScript one bundled script the runner’s shared JavaScript engine the logic is mapping and glue, and your team already writes TypeScript
Go a WebAssembly module (GOOS=wasip1) the runner’s WebAssembly engine you want plain net/http handlers; expect a 7 to 8 MB artifact
Rust a WebAssembly module (wasm32-unknown-unknown) the runner’s WebAssembly engine latency or density matter: about 0.14 MB, tens of microseconds per call
Java 25 a fat jar the ingestor runtime, not the runner the function needs JVM libraries; one instance serves every call, so it must be thread-safe

All four declare the same things and answer the same way. A function is a JVM function or not from its first version: publishing a jar for a function that has Wasm or JavaScript versions (or the reverse) is refused with RUNTIME_MISMATCH. Go cannot use TinyGo today (its http.ServeMux does not match method patterns), and fcdev fn build always uses the standard Go toolchain.

Terminal window
fcdev fn init --lang go --app billing invoice-pdf # or --lang rust / --lang ts
cd invoice-pdf
go mod tidy # npm install for ts

--app is the owning application’s code; the function’s address will be billing.invoice-pdf (lower-case letters, digits and hyphens in the name). fcdev fn init writes Go, Rust and TypeScript projects; there is no scaffold for Java, which is an ordinary Maven project (below).

Every endpoint names exactly one auth mode, and there is no default:

Declared with Who calls it What the runner checks before your code runs
Webhook subscriptions, schedules and dispatch jobs the platform’s delivery signature
Platform people and services holding a platform token a valid access token; your code then checks permissions and client access
Open anyone nothing; verify third-party signatures yourself

A webhook endpoint accepts POST only. Paths follow Go’s routing patterns (/orders/{id}, /files/{rest...}) in every language.

import { fn } from "@flowcatalyst/fn";
const greeting = fn.config("GREETING");
fn.open("GET /healthz", () => fn.json(200, { status: "ok" }));
fn.platform("GET /hello/{name}", (req) =>
fn.json(200, {
message: `${greeting.get() ?? "Hello"}, ${req.params.name}`,
caller: req.caller.principal?.id,
}),
);
fn.webhook("POST /events/greeting", async (req) => {
const ev = req.json() as { id?: string };
await fn.emit({
type: "hello:greeting:greeting:sent",
dedupId: `greeting-sent-${ev.id}`, // derived from the delivered event
data: { ok: true },
});
return fn.status(202);
});
fn.emits("hello:greeting:greeting:sent");

The complete, tested versions of the Go and Rust examples are clients/fn-go/examples/hello and clients/fn-rust/examples/hello in the platform repository.

A Platform endpoint receives a principal. Check what the caller may do in your own code: the runner only proved the token valid.

  • Go: fn.CallerFrom(ctx).Principal() then p.HasPermission("billing:invoice:view"), p.CanAccessClient(id), p.CanAccessClientCode("acme").
  • TypeScript: req.caller.principal with the same methods in camel case.
  • Rust: ctx.caller().principal().
  • Java: the Caller is a Principal with hasPermission, canAccessClient, canAccessClientCode, canAccessApplication and canAccessApplicationCode.

Permission matching treats * as a segment wildcard, exactly as the platform does.

A webhook endpoint is called by the messaging pipeline, so how you answer decides what happens next.

Your answer What the platform does
any 2xx delivered
429 with Retry-After (fn.Retry(w, d), fn.retry(5), Result.retry(d)) tries again after the delay, without spending an attempt
422 with FlowCatalyst-Outcome: reject (fn.Reject(w, reason), fn.reject(reason), Result.reject(reason)) gives up at once: retrying cannot help
anything else, a crash or a timeout a failed attempt, retried until the subscription’s or schedule’s limit

Use the retry answer for platform trouble (an emit returned a retryable error), and reject for input that will never be valid. A function that is past its concurrency limit answers 429 by itself, and deliveries treat that as a deferral too. See Delivery for the platform side.

A function may only emit event types it declared with Emits, and only types of its own application. Every event needs a dedup id: derive it from what you are handling (the delivered event’s id, or the invocation id as a fallback) so a redelivery emits once. A random dedup id would turn every retry into a new event.

Config and secrets, databases, outbound HTTP, dispatching work and calling other applications are all declared and bounded; each has its own guide:

Every call has a deadline (30 seconds by default), and it reaches databases, HTTP and emits too: when the time is up, everything the call started stops.

Unit-test handlers on the host with the SDK’s fake host; no Wasm runtime or platform is needed.

  • Go: go test ./.... fn.InvokeForTest(fn.TestRequest{...}) runs a request through your router and fn.DescribeForTest() returns the describe document (neither compiles for wasip1).
  • Rust: cargo test. testing::invoke, testing::describe, TestRequest, host_ok and host_error do the same. Keep registration in a plain fn register(app: &mut App) shared with export!(register).
  • TypeScript: npm test (plain node --test works; the SDK falls back to TextEncoder when the engine’s natives are absent).
  • Java: FakeFunctionContext, FakeRequest, RecordingEvents and RecordingDispatcher in io.flowcatalyst.function.testing.

Then run the real thing locally with fcdev fn dev.

Terminal window
fcdev fn build # builds with your toolchain, then reads the describe as publishing will
fcdev fn deploy billing.invoice-pdf . --create # publish, wait until loaded, promote live

A function that builds with fcdev fn build publishes. For Java, build a fat jar with Maven, marking the function API (io.flowcatalyst.ingestor:function-api) and the framework (framework-http) as provided, and deploy the jar: fcdev fn deploy hello.greeting target/function-hello-0.1.0.jar --create. The runtime is chosen from the .jar extension. A jar cannot run under fcdev fn run or fcdev fn dev. See Publish, promote and use aliases for the rest.