Backend
Server code: forms that save, API routes, and logic that must not run in the browser.
In stackly.json and the CLI it is backend. It has no price of its own; see What features cost.
backend · optional · Workers
Server code: forms that save, API routes, and logic that must not run in the browser. Every optional feature except
firewall and the no-code ones needs it.
Turn it on
"features": ["backend"]
stackly features on backend
Use it
Server functions, called from components and loaders:
import { createServerFn } from "@tanstack/react-start";
function messageInput(input: unknown) {
const { name, text } = (input ?? {}) as Record<string, unknown>;
if (typeof name !== "string" || !name.trim() || name.length > 80) throw new Error("Enter your name (80 characters at most).");
if (typeof text !== "string" || !text.trim() || text.length > 2000) throw new Error("Enter a message (2,000 characters at most).");
return { name: name.trim(), text: text.trim() };
}
export const saveMessage = createServerFn({ method: "POST" })
.validator(messageInput)
.handler(async ({ data }) => {
// read env from "cloudflare:workers" here; this never runs in the browser
return { ok: true };
});
API routes, for webhooks and other callers:
// src/routes/api/hello.ts
import { createFileRoute } from "@tanstack/react-router";
export const Route = createFileRoute("/api/hello")({
server: { handlers: { GET: async () => Response.json({ hello: "world" }) } },
});
- Validate every input at runtime with
.validator(...)(this TanStack Start version has no.inputValidator); a TypeScript type checks nothing, and a validator that only casts ((d: T) => d) does not count. - Check the signed-in user on the server for anything private or admin (
sign-in), never only in the page. - Read bindings with
import { env } from "cloudflare:workers". Keep server code compatible with Workers. /api/stackly/*belongs to Stackly's kit (tasks, auth links, images, health). Leave those routes as they are.
Limits
30 seconds of CPU time and 100 database, storage and outgoing calls per request.
Locally
stackly dev runs the server code as the draft does.