Backend function reference
What a backend function receives and can reach: the event, ctx.secrets, ctx.fetch, ctx.entity, ctx.email, ctx.ai, ctx.subscribers and ctx.log, return shapes, responses and running as the website.
On this page
A backend function is the body of an async JavaScript function. event and ctx are already in scope; whatever you return goes back to the caller.
event
| Field | Meaning |
|---|---|
event.<anything> | What the caller sent: the payload from fn.invoke, the API body, or the webhook delivery |
event.user | The signed-in user who called it, as { id, email, display_name, role }, or null |
event.ip | The caller's address |
event.request | The HTTP request: method, path, query, headers (lower-cased) and raw_body (the exact text the sender signed) |
event.api_key | { key_id, name } of the API key used, when called over the API |
The platform sets user, ip, request and api_key; a caller cannot supply them.
ctx
| Member | Does |
|---|---|
ctx.secrets.get(NAME) | A stored secret the function declared. Asking for one it did not declare throws |
await ctx.fetch(url, init) | An outbound request to a host the function declared. Resolves to { status, ok, headers, text, json }; json is a property, not a method |
ctx.entity("Name") | The website's tables, with the caller's permissions (or the website's, when running as the website) |
await ctx.email.send({ to, subject, body }) | Send email from the website. Needs a signed-in caller, or a function that runs as the website |
await ctx.email.notifyOwner({ subject, message }) | Email the website's owner |
await ctx.ai.invoke({ prompt, response_json_schema }) | Call AI. Resolves to { ok, response } |
await ctx.subscribers.list({ form, after, limit }) | One page of the people subscribed through the website's sign-up forms: { subscribers, next }. Only in a function that runs as the website |
ctx.user, ctx.app | The caller (as event.user), and { id, name, slug, url } for the website |
ctx.now() | The current time as an ISO string |
ctx.log(...) | A line in the run's log. console.log works too |
A refused call throws an error whose code names the reason, such as sign_in_required or no_such_entity.
Return shapes from ctx.entity
| Call | Resolves to |
|---|---|
.list(options) | { ok, total, records } |
.filter(conditions, options) | An array of rows |
.get(id) | { ok, record } |
.create(data) | { ok, id, record } |
.update(id, data) | { ok, record } |
.archive(id), .delete(id) | { ok } (the same soft delete) |
What a function returns
Whatever you return reaches the caller as result. An object with its own status, body or headers key is read as a response instead: status sets the HTTP status and body is what comes back. So put data that has its own status field inside body. A public function that also returns a content-type header answers with exactly that body, which some webhook providers need to check an address.
What the builder sets on a function
| Setting | Values |
|---|---|
| Who can call it | public, users (default), admin, internal |
| Outside hosts | The hostnames it may call. Enforced |
| Secrets | The stored secrets it may read. Enforced |
| Runs as | the caller (default) or the website. Running as the website reads and writes every table regardless of who called |
| On or off | A new function starts off; the builder switches it on once tested |
A complete example
// stripe_payments: a webhook handler. Visibility public, secret STRIPE_WEBHOOK_SECRET declared.
const secret = ctx.secrets.get("STRIPE_WEBHOOK_SECRET");
const sig = event.request.headers["stripe-signature"];
if (!verifyStripeSignature(event.request.raw_body, sig, secret)) {
return { ok: false, error: "bad_signature" };
}
const evt = JSON.parse(event.request.raw_body);
const seen = await ctx.entity("StripeEvent").filter({ stripe_id: evt.id });
if (seen.length) return { ok: true, duplicate: true };
await ctx.entity("StripeEvent").create({ stripe_id: evt.id, type: evt.type });
if (evt.type === "payment_intent.succeeded") {
const orderId = evt.data.object.metadata.order_id;
await ctx.entity("Order").update(orderId, { status: "paid" });
ctx.log("order paid", orderId);
}
return { ok: true };Calling one
- From a page:
await fn.invoke("name", payload). - From your server:
POST /api/functions/<name>with a secret key. See Calling backend functions. - From a schedule or a data change: an automation.
- From another company's server: a webhook at
/api/webhooks/<name>.
What went wrong? Pick any that apply.
That didn't send. Try again in a moment.