Functions

Serverless TypeScript functions — the core building block of Flux.

Overview

A Flux function is a TypeScript module that exports a single handler via defineFunction(). Functions are deployed as isolated serverless units, auto-scaled on demand, and exposed as HTTP endpoints via the API Gateway.

Defining a function

import { defineFunction } from "@flux/functions";

export default defineFunction({
  name: "greet",

  handler: async ({ input, ctx }) => {
    ctx.log("invoked with", input);
    return { message: `Hello, ${input.name}!` };
  },
});

The handler signature

handler: async ({ input, ctx }: HandlerArgs) => Result
ParameterTypeDescription
inputRecord<string,unknown>JSON body parsed from the HTTP request
ctxFluxContextRuntime context — logging, secrets, env, db

FluxContext API

ctx.log(...args)

Structured log that appears in flux logs with the current request ID attached.

ctx.log("processing", { user_id: input.user_id });

ctx.env

Environment variables configured for the project (set via flux secrets set).

const apiUrl = ctx.env.API_URL;
const region = ctx.env.REGION ?? "us-east-1";

ctx.secrets.get(key)

Retrieves a secret value from the encrypted secrets store.

const apiKey = await ctx.secrets.get("OPENAI_API_KEY");

ctx.db.query(params)

Execute a structured database query. See the Database guide for full reference.

const rows = await ctx.db.query({
  table: "todos",
  operation: "select",
  filters: [{ column: "done", op: "eq", value: false }],
});

flux.json — function config

Each function directory contains a flux.json that declares its metadata:

{
  "name": "create_todo",
  "runtime": "deno",
  "route": "/create_todo",
  "method": "POST"
}
FieldRequiredDescription
nameUnique function name within the project
runtime"deno" (default) or "node"
routeHTTP route path (defaults to /<name>)
methodHTTP method (defaults to POST)
asyncIf true, function runs asynchronously (fire-and-forget)

Project structure

my-project/
  functions/
    my_function/
      index.ts      ← handler
      flux.json     ← config
      package.json  ← npm dependencies for this function (optional)
  schema/
    *.sql           ← database migrations
  .env.example

Local development

# Run a function locally (watches for changes)
$ flux dev

# Invoke directly
$ flux invoke create_todo --payload '{"title":"test"}'

Deploying

# Deploy all functions in the project
$ flux deploy

# Deploy a single function (from inside its directory)
$ cd functions/create_todo
$ flux deploy

← Quickstart Next: Database →