How it works · TM-12
Your site's own REST API.
Do you have a REST API?
Some Silicon Valley nerds asked us whether we offer our customers a REST API. Of course we do. Your site's brain makes whatever REST API you want.
Tell it what your inventory system, your app or your spreadsheet needs to do with your site: add a product, look up an order, publish a post. It builds those endpoints on your site, checks what they're sent, and writes down how to call them. You make a key, and your code calls your site. There's no general-purpose flff API to learn, because the API is the one your business needs.
The rest of this page covers the details.
Endpoints
An endpoint is a command: a short list of steps the brain writes into your site's _u/admin/commands.json, run by your site's own server.
- The address:
https://your-site/_f/actions/NAME, on your own domain too. - The method: POST, with JSON, a form, or multipart with files. A command can also answer GET with query fields, for a feed.
- Fields are checked before anything runs:
- types: text, integer, number, boolean, email, json, file;
- rules: required, minimum and maximum, a fixed list of options, a pattern, how many links a text may hold, and your own message when one fails.
- The answer is JSON:
{"ok": true, …}with whatever the command answers, or{"ok": false, "error": "…"}. - Status codes:
- 400: a field didn't pass;
- 401: a key that isn't one of the site's, or was revoked;
- 403: a key that may not call this command;
- 409: the command stopped itself with its own message (sold out, closed);
- 429: too many calls in a minute;
- 502: something it depends on failed.
- Who can call it:
- a command marked
"api": trueanswers your API keys only; "public": trueanswers anyone, such as your site's own forms;- a command can be both;
- the rest run only from your site's admin.
- a command marked
API keys
- You make them. In your site's hub, under API keys: give the key a name ("Inventory sync") and pick the commands it may call, or all of them, now and later.
- Shown once. The key (
flffk_…) appears once, with a curl line to try it. Your site's server keeps only a fingerprint (hash) of it, never the key itself. - Sent as a bearer token:
Authorization: Bearer flffk_…. - Revoked at once. The list shows when each key was last used. Revoking one stops it on the next call.
- Its own limit: 120 calls a minute per key. Public callers get 20 a minute per address, or 60 for a signed-in member.
- Who did what: a command knows which key called it (
{{api.key}}), so the rows it writes can say so. - Owners only. Only the site's owner manages keys. Neither the site's brain nor AI apps connected to flff ever see them.
An example
You ask: "Let my inventory system add products to the shop through an API." The brain writes a command like this:
"add_product": {
"api": true,
"fields": {
"sku": {"type": "text", "required": true, "pattern": "^[A-Z0-9-]{3,20}$"},
"name": {"type": "text", "required": true, "max": 120},
"price": {"type": "number", "required": true, "min": 0}
},
"steps": [
{"set": {"cents": "round(price * 100)"}},
{"sql": "INSERT INTO products (sku, name, cents, by) VALUES (:sku, :name, :cents, :api.key)",
"as": "p"},
{"respond": {"id": "{{p.id}}", "sku": "{{sku}}"}}
]
}
You make a key for it, and your system calls:
curl -H "Authorization: Bearer flffk_…" -H "Content-Type: application/json" \
-d '{"sku": "MUG-01", "name": "Blue mug", "price": 14}' \
https://your-site/_f/actions/add_product
{"ok": true, "id": 42, "sku": "MUG-01"}
What a command can do
- sql: read and write the site's own database (SQLite, with JSON functions and recursive queries).
- http: call another service over https, at hosts the site declares. Keys for it are kept in the site's secrets and go only into that request's headers or body.
- email and notify: tell the site's people, by email or in the Inbox.
- prompt: hand the site's brain a job to do in the background.
- page: write or remove a page from a template, so a command can be your site's content API (publish a post from your own tool). Every change is in the site's history.
- respond and redirect: the answer.
- require: stop with a message unless a condition holds.
- when: any step can carry a condition, and runs only when it holds.
- script: call a JavaScript function, for logic that's easier written as code (below).
An API command can't read your mailbox or post into chat conversations: those stay in commands you run yourself. It can reach your devices once you approve it.
Branches, loops and errors
- if:
{"if": "total > 5000", "then": [...], "else": [...]} - each:
{"each": "orders.rows", "item": "o", "do": [...]}runs steps once per item, up to 500 items. - call:
{"call": "send_receipt", "with": {"order": "{{o.id}}"}}runs another of the site's commands, with its fields checked, and its answer becomes a value. Write a piece once and use it from several commands. - set:
{"set": {"tax": "round(subtotal * 0.0825, 2)"}}names a value from an expression.- Operators: + − × ÷ and remainder, with + also joining text; comparisons; and, or, not; contains, matches, in.
- Functions: round, floor, ceil, abs, min, max, sum, join, first, last, upper, lower, trim, text, number, replace, slice, len, now, today.
- on_error: on any step, steps to run if it fails instead of stopping: refund the payment, tell someone, mark the order failed. The error's message is there to use.
JavaScript functions
Some logic is easier written as code: pricing rules, parsing, reshaping another service's answer. For that, the brain writes a JavaScript function, and a step calls it:
{"script": "pricing.js", "fn": "quote", "with": {"cart": "{{cart}}", "zip": "{{zip}}"}, "as": "q"}
// _u/admin/scripts/pricing.js
import { taxOf } from './tax.js';
export async function quote({ cart, zip }) {
const rows = flff.sql(
"SELECT id, cents FROM products WHERE id IN (SELECT value FROM json_each(?))",
[cart.map(l => l.id)]);
const rate = flff.http({
url: "https://api.taxes.example/rate?zip=" + zip,
headers: { Authorization: "Bearer " + flff.secret("TAX_KEY") } });
const subtotal = cart.reduce(
(t, l) => t + rows.find(r => r.id === l.id).cents * l.qty, 0);
return { subtotal, tax: taxOf(subtotal, rate.rate) };
}
- In and out: the function gets one argument, the values in
withas JSON. It returns any JSON value, or a promise of one, and that becomes the step's value (q.subtotalabove) for the steps after it. - The language: modern JavaScript (ES2023: async/await, classes, destructuring, modules). Scripts live in
_u/admin/scripts/and can import each other. - The sandbox: QuickJS compiled to WebAssembly, running inside your site's own server.
- No network, no files, no environment variables.
- Up to 64 MB of memory.
- The command's time, 20 seconds at most.
- The way out is the
flffobject. Its calls follow the same rules as steps:flff.sql(query, params): the site's database.flff.http(request): declared hosts, over https.flff.call(command, fields): another of the site's commands.flff.log(…): a line in the site's log, which is also whereconsole.loggoes.
- Secrets stay secret:
flff.secret("NAME")is a stand-in that becomes the real key only as the request leaves, in its headers or body. The script never sees the key, and text a visitor sends can't name one. - The same limits on what it reaches: a public command's script can't call a command that writes pages or uses your devices.
- Where to see it: the hub's Commands section lists the scripts, opens them in the editor, and draws each script step in the command's diagram.
Your devices
Commands can reach the hardware in a room you look after, such as a temperature sensor in the server rack room or a relay on its air conditioning, through fluffrl on a computer there.
- Serial ports: read, write, or send and read the answer (an Arduino, a relay board, a sensor).
{"device": {"op": "exchange", "port": "COM3", "data": "TEMP?", "until": "\n"}, "as": "t"} - Local commands: programs you set up in fluffrl, with fixed arguments and checked values. A command gets back the exit code and the output.
{"device": {"op": "command", "command": "read_temp", "values": {"sensor": "inlet"}}, "as": "t"} - Watching: a scheduled command reads the rack room's temperature every five minutes and switches the AC's relay on above 30 °C: a device step, an
if, another device step. No brain and no credit are used each time. - Behind your interface: a public command (a button on a page) or an API command (your monitoring system, with a key) can use the devices too, so the room has its own small API:
GET /_f/actions/rack_temp,POST /_f/actions/ac. - You approve it first: a public or API command that reaches a device runs only once you approve it in your site's hub, under Devices. The approval covers the command as it stands, with every command it calls. If either changes, it asks you again.
- Still under your switches: in fluffrl you choose which ports and commands are on, whether a port is read-only, and whether each write asks you first. Calls from outside are limited to 30 a minute for the site.
Limits
Commands are bounded on purpose: every run ends, costs little, and can be checked before it runs.
- Steps: up to 40 in each list, and up to 500 step runs in one call, loops and calls included.
- Outside calls: up to 50 in one call.
- Nesting: blocks and calls go up to 6 deep, so a command that calls itself stops with a clear message.
- Time: about 30 seconds per call; a script gets up to 20 of them. Longer work goes to the brain (prompt) or a scheduled task.
- Scripts: 64 MB of memory each, up to 64 files of 256 KB.
- Hosts: outgoing calls go over https, to the hosts the site declares, and never to private addresses.
- Checked before it runs: the site checks every command: names that don't exist, secrets that aren't set, hosts not declared, a public command doing something only you may do (including through a command it calls).
Seeing what it does
Your site's hub has a Commands section that draws each command as a flow diagram, read left to right:
- what starts it, with its fields;
- each step as a block, with conditions as badges;
- gates where it can stop, branches and loops as boxes, calls you can open;
- error handling as a red lane;
- wires showing which values feed which steps.
Click a block to see it as written. To change one, ask the brain.
Documentation
The brain documents what it builds where you'll find it: an admin page with each endpoint's address, fields, an example request and its answer. If your developers want an OpenAPI file, ask for one.
Calls the other way: hooks
Other services can call your site too. A hook is an address on your site, with steps like a command's, that checks the caller's signature (Stripe, GitHub, Shopify, Square) before anything runs. Each event runs once even if it's sent twice. That's how a payment marks an order paid.
What it comes with
- Every Site plan: commands, secrets and API keys.
- Devices: fluffrl on a computer by them.
- The Business backend: adds the database, hooks and roles.
The catalog lists every feature and what it comes with. Questions: hello@flff.fi.