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.

API keys

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

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

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) };
}

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.

Limits

Commands are bounded on purpose: every run ends, costs little, and can be checked before it runs.

Seeing what it does

Your site's hub has a Commands section that draws each command as a flow diagram, read left to right:

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

The catalog lists every feature and what it comes with. Questions: hello@flff.fi.