Quick Start
A Stof document looks like JSON with functions. Press Run:
name: 'Stof'
fn greet() -> str {
`Hello, ${self.name}!`
}
#[main]
fn main() {
pln(self.greet());
}name is a field, like in JSON. greet is a function stored on the same object, and self is that object. #[main] marks the function that runs when the document is run. Change the name and run it again.
That's the whole idea. The rest of this page runs a document from an app, which is how Stof is meant to be used.
Run It From Your App
You'll need Node.js 18 or newer. Make a project and install Stof:
mkdir stof-quickstart && cd stof-quickstart
npm init -y
npm i @formata/stof
Create index.mjs:
import { StofDoc } from '@formata/stof';
const doc = await StofDoc.new(`
price: 20
discount: 0.2
fn total(quantity: int) -> float {
self.price * quantity * (1 - self.discount)
}
`);
console.log(await doc.call('total', 3)); // 48
node index.mjs
Your app loaded a document, then called one of its functions by name and got a value back. The document runs inside your process, and it can only see itself.
Change the Data, Then the Logic
A document is data, so your app can change it while it runs. Add this to the end of index.mjs:
doc.set('discount', 0.5);
console.log(await doc.call('total', 3)); // 30
// new logic, parsed in the same way as new data
doc.parse(`
fn total(quantity: int) -> float {
const subtotal = self.price * quantity;
if (quantity < 3) return subtotal;
subtotal * (1 - self.discount)
}
`);
console.log(await doc.call('total', 1)); // 20
console.log(await doc.call('total', 3)); // 30
The second total replaced the first. It could just as well have come from a database, an API, a customer's settings, or an AI model. That's where Stof is most useful: running logic that your team didn't write, safely.
Get It Back Out
A document exports like any other data:
console.log(doc.stringify('json')); // {"price":20,"discount":0.5}
console.log(doc.record()); // the same, as a JavaScript object
Functions aren't part of JSON, so they're left out. To keep them, export the binary format instead: doc.blobify('bstf').
Where to Go Next
- Install: Python, Rust, the CLI, browsers and bundlers, and the VS Code extension.
- Using Stof in Your App: calling functions, host functions, permissions, time limits, and errors.
- Learn: the language, one topic at a time, starting with fields.
- Playground: complete, runnable examples of real uses.
- Playground: try things in your browser.